Skip to content

Commit 2ec710d

Browse files
committed
Make CircleParser more robust
1 parent 438ceb7 commit 2ec710d

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Not released yet.
1010
* Raised the minimum MediaWiki version from 1.35 to 1.40
1111
* Added compatibility with MediaWiki 1.44
1212
* Fixed GeoJSON error by ignoring GeoJSON descriptions that are objects
13+
* Fixed fatal error when providing an invalid radius for circles
1314

1415
## Maps 11.0.1
1516

src/WikitextParsers/CircleParser.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public function __construct( $geocoder = null ) {
3737
*/
3838
public function parse( $value ): Circle {
3939
$metaData = explode( $this->metaDataSeparator, $value );
40-
$circleData = explode( ':', array_shift( $metaData ) );
4140

42-
$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] );
41+
$circle = $this->buildCircle( array_shift( $metaData ) );
4342

4443
if ( $metaData !== [] ) {
4544
$circle->setTitle( array_shift( $metaData ) );
@@ -72,6 +71,27 @@ public function parse( $value ): Circle {
7271
return $circle;
7372
}
7473

74+
private function buildCircle( string $circleWikitext ): Circle {
75+
$circleData = explode( ':', $circleWikitext );
76+
77+
return new Circle(
78+
$this->stringToLatLongValue( $circleData[0] ),
79+
$this->extractRadius( $circleData )
80+
);
81+
}
82+
83+
private function extractRadius( array $circleData ): float {
84+
if ( array_key_exists( 1, $circleData ) ) {
85+
$radius = (float)$circleData[1];
86+
87+
if ( $radius > 0 ) {
88+
return $radius;
89+
}
90+
}
91+
92+
return 1;
93+
}
94+
7595
private function stringToLatLongValue( string $location ): LatLongValue {
7696
$latLong = $this->geocoder->geocode( $location );
7797

tests/Integration/Parsers/CircleParserTest.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,41 @@
1919
class CircleParserTest extends TestCase {
2020

2121
public function testGivenCoordinateAndRadius_parserReturnsCircle() {
22-
$parser = new CircleParser( new CoordinateFriendlyGeocoder( new NullGeocoder() ) );
23-
24-
$circle = $parser->parse( '57.421,23.90625:32684.605182' );
25-
26-
$this->assertInstanceOf( Circle::class, $circle );
22+
$circle = $this->newCircleParser()->parse( '57.421,23.90625:32684.605182' );
2723

2824
$expectedLatLong = new LatLongValue( 57.421, 23.90625 );
2925
$this->assertTrue( $expectedLatLong->equals( $circle->getCircleCentre() ) );
3026

3127
$this->assertSame( 32684.605182, $circle->getCircleRadius() );
3228
}
3329

34-
public function testGivenTitleAndText_circleHasProvidedMetaData() {
35-
$parser = new CircleParser( new CoordinateFriendlyGeocoder( new NullGeocoder() ) );
36-
37-
$circle = $parser->parse( '57.421,23.90625:32684.605182~title~text' );
30+
private function newCircleParser(): CircleParser {
31+
return new CircleParser( new CoordinateFriendlyGeocoder( new NullGeocoder() ) );
32+
}
3833

39-
$this->assertInstanceOf( Circle::class, $circle );
34+
public function testGivenTitleAndText_circleHasProvidedMetaData() {
35+
$circle = $this->newCircleParser()->parse( '57.421,23.90625:32684.605182~title~text' );
4036

4137
$this->assertSame( 'title', $circle->getTitle() );
4238
$this->assertSame( 'text', $circle->getText() );
4339
}
4440

41+
public function testGivenNoRadius_radiusIsOne() {
42+
$circle = $this->newCircleParser()->parse( '42,42' );
43+
44+
$this->assertSame( 1.0, $circle->getCircleRadius() );
45+
}
46+
47+
public function testGivenNegative_radiusIsOne() {
48+
$circle = $this->newCircleParser()->parse( '42,42:-5' );
49+
50+
$this->assertSame( 1.0, $circle->getCircleRadius() );
51+
}
52+
53+
public function testGivenInvalid_radiusIsOne() {
54+
$circle = $this->newCircleParser()->parse( '42,42:foo' );
55+
56+
$this->assertSame( 1.0, $circle->getCircleRadius() );
57+
}
58+
4559
}

0 commit comments

Comments
 (0)