From f32cda83a377065ba5e3cf4a14ffbd6d6c7c3f3f Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Wed, 10 Apr 2024 11:40:07 +0200 Subject: [PATCH 1/2] Completing tests. --- lib/LongitudeOne/Geo/WKB/Parser.php | 22 +++++++++- lib/LongitudeOne/Geo/WKB/Reader.php | 28 ++++++++++-- quality/php-stan/phpstan-baseline.neon | 5 +++ .../LongitudeOne/Geo/WKB/Tests/ParserTest.php | 43 ++++++++++++++++--- .../LongitudeOne/Geo/WKB/Tests/ReaderTest.php | 27 ++++++++++++ 5 files changed, 115 insertions(+), 10 deletions(-) diff --git a/lib/LongitudeOne/Geo/WKB/Parser.php b/lib/LongitudeOne/Geo/WKB/Parser.php index 6b5df84..ead14c1 100644 --- a/lib/LongitudeOne/Geo/WKB/Parser.php +++ b/lib/LongitudeOne/Geo/WKB/Parser.php @@ -80,7 +80,15 @@ public function __construct($input = null) if (null !== $input) { if (!is_string($input)) { - trigger_error('Since longitudeone/geo-wkb-parser 1.0: using non-string parameter for Reader constructor deprecated.', E_USER_DEPRECATED); + trigger_error( + sprintf('%s: Since longitudeone/geo-wkb-parser 2.1, Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', + static::class, + gettype($input), + __FILE__, + __LINE__ + ), + E_USER_DEPRECATED + ); } $this->reader->load((string) $input); @@ -99,6 +107,18 @@ public function __construct($input = null) public function parse($input = null): array { if (null !== $input) { + if (!is_string($input)) { + trigger_error( + sprintf('%s: Since longitudeone/geo-wkb-parser 2.1, Argument 1 passed to parse() must be of the type string, %s given, called in %s on line %d', + static::class, + gettype($input), + __FILE__, + __LINE__ + ), + E_USER_DEPRECATED + ); + } + $this->reader->load((string) $input); } diff --git a/lib/LongitudeOne/Geo/WKB/Reader.php b/lib/LongitudeOne/Geo/WKB/Reader.php index 50ea847..268a873 100644 --- a/lib/LongitudeOne/Geo/WKB/Reader.php +++ b/lib/LongitudeOne/Geo/WKB/Reader.php @@ -48,7 +48,19 @@ class Reader public function __construct($input = null) { if (null !== $input) { - $this->load($input); + if (!is_string($input)) { + trigger_error( + sprintf( + '%s: Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', + static::class, + gettype($input), + __FILE__, + __LINE__ + ), + E_USER_DEPRECATED + ); + } + $this->load((string) $input); } } @@ -112,7 +124,12 @@ public function readByteOrder(): int */ public function readDouble(): float { - trigger_error(static::class.': Method readDouble is deprecated, use readFloat instead.', E_USER_DEPRECATED); + trigger_error( + sprintf('%s: Since longitudeone/geo-wkb-parser 1.0, method readDoubles is deprecated, use readFloats instead.', + static::class, + ), + E_USER_DEPRECATED + ); return $this->readFloat(); } @@ -127,7 +144,12 @@ public function readDouble(): float */ public function readDoubles(int $count): array { - trigger_error(static::class.': Method readDoubles is deprecated, use readFloats instead.', E_USER_DEPRECATED); + trigger_error( + sprintf('%s: Since longitudeone/geo-wkb-parser 1.0, method readDoubles is deprecated, use readFloats instead.', + static::class, + ), + E_USER_DEPRECATED + ); return $this->readFloats($count); } diff --git a/quality/php-stan/phpstan-baseline.neon b/quality/php-stan/phpstan-baseline.neon index 9ab7431..92c454d 100644 --- a/quality/php-stan/phpstan-baseline.neon +++ b/quality/php-stan/phpstan-baseline.neon @@ -14,3 +14,8 @@ parameters: message: "#^Parameter \\#1 \\$input of class LongitudeOne\\\\Geo\\\\WKB\\\\Parser constructor expects string\\|null, float\\|string given\\.$#" count: 1 path: ../../tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php + + - + message: "#^Parameter \\#1 \\$input of method LongitudeOne\\\\Geo\\\\WKB\\\\Parser\\:\\:parse\\(\\) expects string\\|null, float\\|string given\\.$#" + count: 1 + path: ../../tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php diff --git a/tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php b/tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php index 9cac280..14f4453 100644 --- a/tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php +++ b/tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php @@ -26,6 +26,20 @@ */ class ParserTest extends TestCase { + private Parser $parser; + + public function setUp(): void + { + parent::setUp(); + $this->parser = new Parser(); + } + + public function tearDown(): void + { + unset($this->parser); + parent::tearDown(); + } + /** * @return \Generator, message:string}, null, void> */ @@ -4180,18 +4194,26 @@ public static function wkbGeometryType(): \Generator public function testBadBinaryData(string|float $value, string $exception, string $message): void { self::expectException($exception); - - if ('/' === $message[0]) { - self::expectExceptionMessageMatches($message); - } else { - self::expectExceptionMessage($message); - } + self::expectMessage($message); $parser = new Parser($value); $parser->parse(); } + /** + * @param class-string $exception + * + * @dataProvider badBinaryData + */ + public function testBadBinaryDataWithPreparedParser(string|float $value, string $exception, string $message): void + { + self::expectException($exception); + self::expectMessage($message); + + $this->parser->parse($value); + } + #[DataProvider('wkbGeometryType')] public function testGeometryType(int $expected, int $actual): void { @@ -4306,4 +4328,13 @@ public function testReusedParser(): void $this->assertEquals($testData['expected'], $actual); } } + + private function expectMessage(string $message): void + { + if ('/' === $message[0]) { + self::expectExceptionMessageMatches($message); + } else { + self::expectExceptionMessage($message); + } + } } diff --git a/tests/LongitudeOne/Geo/WKB/Tests/ReaderTest.php b/tests/LongitudeOne/Geo/WKB/Tests/ReaderTest.php index 59fb8aa..c983e9f 100644 --- a/tests/LongitudeOne/Geo/WKB/Tests/ReaderTest.php +++ b/tests/LongitudeOne/Geo/WKB/Tests/ReaderTest.php @@ -214,6 +214,33 @@ public function testBad(?string $value, array $methods, string $exception, strin } } + public function testDeprecation(): void + { + $reader = new Reader(); + + $value = '0040411D70A3D70A3D'; + $value = pack('H*', $value); + + $reader->load($value); + + $reader->readByteOrder(); + + $result = $reader->readDouble(); + + self::assertEquals(34.23, $result); + + $value = '0040411D70A3D70A3D40411D70A3D70A3D'; + $value = pack('H*', $value); + + $reader->load($value); + + $reader->readByteOrder(); + + $result = $reader->readDoubles(2); + + $this->assertEquals([34.23, 34.23], $result); + } + /** * @param array{0:string, 1:float|int|null, 2:array|int|float|null}[] $methods * From 925d99b0a17c3c57f4512f5f4488e5ce344f6e81 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Wed, 10 Apr 2024 12:01:25 +0200 Subject: [PATCH 2/2] Using trigger_deprecation from symfony/deprecations-contract. --- composer.json | 3 +- lib/LongitudeOne/Geo/WKB/Parser.php | 20 +++++------ lib/LongitudeOne/Geo/WKB/Reader.php | 34 ++++++++----------- .../php-mess-detector/codeclimate-ruleset.xml | 6 ++-- quality/php-mess-detector/ruleset.xml | 4 +-- quality/php-mess-detector/test-ruleset.xml | 2 +- 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index a057af5..3d3264b 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "php": "^8.1" }, "require-dev": { - "phpunit/phpunit": "^10.5.16" + "phpunit/phpunit": "^10.5.16", + "symfony/deprecation-contracts": "^3.0" }, "autoload": { "psr-0": { diff --git a/lib/LongitudeOne/Geo/WKB/Parser.php b/lib/LongitudeOne/Geo/WKB/Parser.php index ead14c1..911d4e2 100644 --- a/lib/LongitudeOne/Geo/WKB/Parser.php +++ b/lib/LongitudeOne/Geo/WKB/Parser.php @@ -80,14 +80,14 @@ public function __construct($input = null) if (null !== $input) { if (!is_string($input)) { - trigger_error( - sprintf('%s: Since longitudeone/geo-wkb-parser 2.1, Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', - static::class, + trigger_deprecation( + 'longitudeone/geo-wkb-parser', + '2.1', + sprintf('Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', gettype($input), __FILE__, __LINE__ - ), - E_USER_DEPRECATED + ) ); } @@ -108,14 +108,14 @@ public function parse($input = null): array { if (null !== $input) { if (!is_string($input)) { - trigger_error( - sprintf('%s: Since longitudeone/geo-wkb-parser 2.1, Argument 1 passed to parse() must be of the type string, %s given, called in %s on line %d', - static::class, + trigger_deprecation( + 'longitudeone/geo-wkb-parser', + '2.1', + sprintf('Argument 1 passed to parse() must be of the type string, %s given, called in %s on line %d', gettype($input), __FILE__, __LINE__ - ), - E_USER_DEPRECATED + ) ); } diff --git a/lib/LongitudeOne/Geo/WKB/Reader.php b/lib/LongitudeOne/Geo/WKB/Reader.php index 268a873..afe2af1 100644 --- a/lib/LongitudeOne/Geo/WKB/Reader.php +++ b/lib/LongitudeOne/Geo/WKB/Reader.php @@ -49,15 +49,13 @@ public function __construct($input = null) { if (null !== $input) { if (!is_string($input)) { - trigger_error( - sprintf( - '%s: Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', - static::class, - gettype($input), - __FILE__, - __LINE__ - ), - E_USER_DEPRECATED + trigger_deprecation( + 'longitudeone/geo-wkb-parser', + '2.1', + 'Argument 1 passed to __construct() must be of the type string, %s given, called in %s on line %d', + gettype($input), + __FILE__, + __LINE__ ); } $this->load((string) $input); @@ -124,11 +122,10 @@ public function readByteOrder(): int */ public function readDouble(): float { - trigger_error( - sprintf('%s: Since longitudeone/geo-wkb-parser 1.0, method readDoubles is deprecated, use readFloats instead.', - static::class, - ), - E_USER_DEPRECATED + trigger_deprecation( + 'longitudeone/geo-wkb-parser', + '1.0', + 'Method readDouble is deprecated, use readFloat instead.' ); return $this->readFloat(); @@ -144,11 +141,10 @@ public function readDouble(): float */ public function readDoubles(int $count): array { - trigger_error( - sprintf('%s: Since longitudeone/geo-wkb-parser 1.0, method readDoubles is deprecated, use readFloats instead.', - static::class, - ), - E_USER_DEPRECATED + trigger_deprecation( + 'longitudeone/geo-wkb-parser', + '1.0', + 'Method readDoubles is deprecated, use readFloats instead.' ); return $this->readFloats($count); diff --git a/quality/php-mess-detector/codeclimate-ruleset.xml b/quality/php-mess-detector/codeclimate-ruleset.xml index 70e2b2b..4827d74 100644 --- a/quality/php-mess-detector/codeclimate-ruleset.xml +++ b/quality/php-mess-detector/codeclimate-ruleset.xml @@ -16,7 +16,7 @@ - + @@ -24,8 +24,8 @@ - - + + diff --git a/quality/php-mess-detector/ruleset.xml b/quality/php-mess-detector/ruleset.xml index 52ea8e6..6682659 100644 --- a/quality/php-mess-detector/ruleset.xml +++ b/quality/php-mess-detector/ruleset.xml @@ -18,8 +18,8 @@ - - + + diff --git a/quality/php-mess-detector/test-ruleset.xml b/quality/php-mess-detector/test-ruleset.xml index 4a52032..22638c5 100644 --- a/quality/php-mess-detector/test-ruleset.xml +++ b/quality/php-mess-detector/test-ruleset.xml @@ -13,7 +13,7 @@ - +