Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/58877
Change subject: Remove usage of ValueParsers Result interface ...................................................................... Remove usage of ValueParsers Result interface This requires https://gerrit.wikimedia.org/r/#/c/58867/ Change-Id: I5f95e706c3612d0c4552c2e8814f2e0f02cb8aa6 --- M includes/Geocoders.php M includes/parsers/DistanceParser.php M includes/parsers/LineParser.php M includes/parsers/LocationParser.php M includes/parsers/PolygonParser.php M includes/parsers/WmsOverlayParser.php M tests/phpunit/parsers/DistanceParserTest.php M tests/phpunit/parsers/LineParserTest.php M tests/phpunit/parsers/LocationParserTest.php M tests/phpunit/parsers/WmsOverlayParserTest.php 10 files changed, 56 insertions(+), 78 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Maps refs/changes/77/58877/1 diff --git a/includes/Geocoders.php b/includes/Geocoders.php index d39b52b..eb3e69f 100644 --- a/includes/Geocoders.php +++ b/includes/Geocoders.php @@ -4,6 +4,7 @@ use DataValues\GeoCoordinateValue; use MWException; +use ValueParsers\ParseException; /** * Class for geocoder functionality of the Maps extension. @@ -129,13 +130,11 @@ public static function attemptToGeocode( $coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true ) { if ( $checkForCoords ) { $coordinateParser = new \ValueParsers\GeoCoordinateParser( new \ValueParsers\ParserOptions() ); - $parseResult = $coordinateParser->parse( $coordsOrAddress ); - if ( $parseResult->isValid() ) { - $value = $parseResult->getValue(); - assert( $value instanceof GeoCoordinateValue ); - return $value; - } else { + try { + return $coordinateParser->parse( $coordsOrAddress ); + } + catch ( ParseException $parseException ) { return self::geocode( $coordsOrAddress, $geoservice, $mappingService ); } } else { diff --git a/includes/parsers/DistanceParser.php b/includes/parsers/DistanceParser.php index 2681df4..da07e63 100644 --- a/includes/parsers/DistanceParser.php +++ b/includes/parsers/DistanceParser.php @@ -2,7 +2,7 @@ namespace Maps; -use ValueParsers\Result; +use ValueParsers\ParseException; use ValueParsers\StringValueParser; /** @@ -41,17 +41,17 @@ * * @param string $value * - * @return Result + * @return float + * @throws ParseException */ public function stringParse( $value ) { $distance = \MapsDistanceParser::parseDistance( $value ); if ( $distance === false ) { - return $this->newErrorResult( 'Not a distance' ); + throw new ParseException( 'Not a distance' ); } - else { - return Result::newSuccess( $distance ); - } + + return $distance; } } diff --git a/includes/parsers/LineParser.php b/includes/parsers/LineParser.php index 92bb4f7..07232e7 100644 --- a/includes/parsers/LineParser.php +++ b/includes/parsers/LineParser.php @@ -3,7 +3,6 @@ namespace Maps; use DataValues\GeoCoordinateValue; -use ValueParsers\Result; use ValueParsers\StringValueParser; /** @@ -48,7 +47,7 @@ * * @param string $value * - * @return Result + * @return Line */ public function stringParse( $value ) { $parts = explode( $this->metaDataSeparator , $value ); @@ -59,7 +58,7 @@ $this->handleCommonParams( $parts, $line ); - return Result::newSuccess( $line ); + return $line; } /** @@ -87,14 +86,7 @@ } } else { - $parseResult = $coordinateParser->parse( $coordinateString ); - - if ( $parseResult->isValid() ) { - $coordinates[] = $parseResult->getValue(); - } - else { - // TODO - } + $coordinates[] = $coordinateParser->parse( $coordinateString ); } } diff --git a/includes/parsers/LocationParser.php b/includes/parsers/LocationParser.php index 47c0337..53a1815 100644 --- a/includes/parsers/LocationParser.php +++ b/includes/parsers/LocationParser.php @@ -4,9 +4,8 @@ use DataValues\GeoCoordinateValue; use MWException; -use ValueParsers\Error; use ValueParsers\GeoCoordinateParser; -use ValueParsers\Result; +use ValueParsers\ParseException; use ValueParsers\StringValueParser; /** @@ -47,7 +46,7 @@ * * @param string $value * - * @return Result + * @return Location * @throws MWException */ public function stringParse( $value ) { @@ -57,11 +56,7 @@ $coordinates = $this->getCoordinates( array_shift( $metaData ) ); - if ( $coordinates instanceof Error ) { - return Result::newError( $coordinates ); - } - - $location = new \Maps\Location( $coordinates ); + $location = new Location( $coordinates ); if ( $metaData !== array() ) { $location->setTitle( array_shift( $metaData ) ); @@ -75,7 +70,7 @@ $location->setIcon( array_shift( $metaData ) ); } - return Result::newSuccess( $location ); + return $location; } /** @@ -83,14 +78,15 @@ * * @param string $location * - * @return GeoCoordinateValue|Error + * @return GeoCoordinateValue + * @throws ParseException */ protected function getCoordinates( $location ) { if ( $this->supportGeocoding && \Maps\Geocoders::canGeocode() ) { $location = \Maps\Geocoders::attemptToGeocode( $location ); if ( $location === false ) { - return $this->newErrorResult( 'Geocoding failed' )->getError(); + throw new ParseException( 'Failed to parse or geocode' ); } assert( $location instanceof GeoCoordinateValue ); @@ -98,15 +94,7 @@ } $parser = new GeoCoordinateParser( new \ValueParsers\ParserOptions() ); - $parseResult = $parser->parse( $location ); - - if ( !$parseResult->isValid() ) { - return $parseResult->getError(); - } - - $location = $parseResult->getValue(); - assert( $location instanceof GeoCoordinateValue ); - return $location; + return $parser->parse( $location ); } } diff --git a/includes/parsers/PolygonParser.php b/includes/parsers/PolygonParser.php index 5b926af..58d335c 100644 --- a/includes/parsers/PolygonParser.php +++ b/includes/parsers/PolygonParser.php @@ -2,7 +2,6 @@ namespace Maps; -use ValueParsers\Result; use ValueParsers\StringValueParser; /** @@ -41,7 +40,7 @@ * * @param string $value * - * @return Result + * @return Polygon */ public function stringParse( $value ) { // TODO diff --git a/includes/parsers/WmsOverlayParser.php b/includes/parsers/WmsOverlayParser.php index accb3af..bf46788 100644 --- a/includes/parsers/WmsOverlayParser.php +++ b/includes/parsers/WmsOverlayParser.php @@ -4,7 +4,7 @@ use MWException; use ValueParsers\GeoCoordinateParser; -use ValueParsers\Result; +use ValueParsers\ParseException; use ValueParsers\StringValueParser; /** @@ -42,22 +42,21 @@ * * @param string $value * - * @return Result + * @return WmsOverlay */ protected function stringParse( $value ) { $separator = " "; $metaData = explode($separator, $value); if ( count( $metaData ) >= 2 ) { - $wmsOverlay = new \Maps\WmsOverlay( $metaData[0], $metaData[1] ); + $wmsOverlay = new WmsOverlay( $metaData[0], $metaData[1] ); if ( count( $metaData ) == 3) { $wmsOverlay->setWmsStyleName( $metaData[2] ); } - return Result::newSuccess( $wmsOverlay ); - - } else { - return $this->newErrorResult( "Need at least two parameters, url to WMS server and map layer name" ); + return $wmsOverlay; } + + throw new ParseException( 'Need at least two parameters, url to WMS server and map layer name' ); } } diff --git a/tests/phpunit/parsers/DistanceParserTest.php b/tests/phpunit/parsers/DistanceParserTest.php index 37a9e24..2686bf6 100644 --- a/tests/phpunit/parsers/DistanceParserTest.php +++ b/tests/phpunit/parsers/DistanceParserTest.php @@ -2,8 +2,6 @@ namespace Maps\Test; -use ValueParsers\Result; - /** * Unit tests for the Maps\DistanceParser class. * @@ -36,13 +34,13 @@ class DistanceParserTest extends \ValueParsers\Test\StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 3.0 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -54,10 +52,10 @@ ); foreach ( $valid as $value => $expected ) { - $argLists[] = array( (string)$value, Result::newSuccess( $expected ) ); + $argLists[] = array( (string)$value, $expected ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/tests/phpunit/parsers/LineParserTest.php b/tests/phpunit/parsers/LineParserTest.php index e4cd57d..c93afeb 100644 --- a/tests/phpunit/parsers/LineParserTest.php +++ b/tests/phpunit/parsers/LineParserTest.php @@ -2,8 +2,8 @@ namespace Maps\Test; +use DataValues\GeoCoordinateValue; use Maps\Line; -use ValueParsers\Result; /** * Unit tests for the Maps\LineParser class. @@ -37,13 +37,13 @@ class LineParserTest extends \ValueParsers\Test\StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 3.0 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array(); @@ -80,15 +80,15 @@ foreach ( $values as $value ) { $input[] = implode( ',', $value ); - $output[] = new \DataValues\GeoCoordinateValue( $value[0], $value[1] ); + $output[] = new GeoCoordinateValue( $value[0], $value[1] ); } $input = implode( ':', $input ); - $argLists[] = array( $input, Result::newSuccess( new Line( $output ) ) ); + $argLists[] = array( $input, new Line( $output ) ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/tests/phpunit/parsers/LocationParserTest.php b/tests/phpunit/parsers/LocationParserTest.php index 6542c4f..0ef7c7d 100644 --- a/tests/phpunit/parsers/LocationParserTest.php +++ b/tests/phpunit/parsers/LocationParserTest.php @@ -2,7 +2,8 @@ namespace Maps\Test; -use ValueParsers\Result; +use DataValues\GeoCoordinateValue; +use Maps\Location; /** * Unit tests for the Maps\LocationParser class. @@ -36,13 +37,13 @@ class LocationParserTest extends \ValueParsers\Test\StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 3.0 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -56,16 +57,16 @@ ); foreach ( $valid as $value => $expected ) { - $expected = new \Maps\Location( new \DataValues\GeoCoordinateValue( $expected[0], $expected[1] ) ); - $argLists[] = array( (string)$value, Result::newSuccess( $expected ) ); + $expected = new Location( new GeoCoordinateValue( $expected[0], $expected[1] ) ); + $argLists[] = array( (string)$value, $expected ); } - $location = new \Maps\Location( new \DataValues\GeoCoordinateValue( 4, 2 ) ); + $location = new Location( new GeoCoordinateValue( 4, 2 ) ); $location->setTitle( 'Title' ); $location->setText( 'some description' ); - $argLists[] = array( '4,2~Title~some description', Result::newSuccess( $location ) ); + $argLists[] = array( '4,2~Title~some description', $location ); - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } /** diff --git a/tests/phpunit/parsers/WmsOverlayParserTest.php b/tests/phpunit/parsers/WmsOverlayParserTest.php index 852f89a..d90f5ca 100644 --- a/tests/phpunit/parsers/WmsOverlayParserTest.php +++ b/tests/phpunit/parsers/WmsOverlayParserTest.php @@ -2,7 +2,7 @@ namespace Maps\Test; -use ValueParsers\Result; +use Maps\WmsOverlay; /** * Unit tests for the Maps\WmsOverlayParser class. @@ -36,13 +36,13 @@ class WmsOverlayParserTest extends \ValueParsers\Test\StringValueParserTest { /** - * @see ValueParserTestBase::parseProvider + * @see ValueParserTestBase::validInputProvider * * @since 3.0 * * @return array */ - public function parseProvider() { + public function validInputProvider() { $argLists = array(); $valid = array( @@ -53,14 +53,16 @@ ); foreach ( $valid as $value => $expected ) { - $expectedOverlay = new \Maps\WmsOverlay( $expected[0], $expected[1] ); + $expectedOverlay = new WmsOverlay( $expected[0], $expected[1] ); + if ( count( $expected ) == 3 ) { $expectedOverlay->setWmsStyleName( $expected[2] ); } - $argLists[] = array( (string)$value, Result::newSuccess( $expectedOverlay ) ); + + $argLists[] = array( (string)$value, $expectedOverlay ); } - return array_merge( $argLists, parent::parseProvider() ); + return $argLists; } -- To view, visit https://gerrit.wikimedia.org/r/58877 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5f95e706c3612d0c4552c2e8814f2e0f02cb8aa6 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Maps Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits