Skip to content

Commit

Permalink
Merge pull request #4 from longitude-one/deprecation-feature
Browse files Browse the repository at this point in the history
Deprecation features added
  • Loading branch information
Alexandre-T authored Apr 10, 2024
2 parents 8f2b4f1 + 925d99b commit 4886874
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 21 additions & 1 deletion lib/LongitudeOne/Geo/WKB/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_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__
)
);
}

$this->reader->load((string) $input);
Expand All @@ -99,6 +107,18 @@ public function __construct($input = null)
public function parse($input = null): array
{
if (null !== $input) {
if (!is_string($input)) {
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__
)
);
}

$this->reader->load((string) $input);
}

Expand Down
24 changes: 21 additions & 3 deletions lib/LongitudeOne/Geo/WKB/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ class Reader
public function __construct($input = null)
{
if (null !== $input) {
$this->load($input);
if (!is_string($input)) {
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);
}
}

Expand Down Expand Up @@ -112,7 +122,11 @@ public function readByteOrder(): int
*/
public function readDouble(): float
{
trigger_error(static::class.': Method readDouble is deprecated, use readFloat instead.', E_USER_DEPRECATED);
trigger_deprecation(
'longitudeone/geo-wkb-parser',
'1.0',
'Method readDouble is deprecated, use readFloat instead.'
);

return $this->readFloat();
}
Expand All @@ -127,7 +141,11 @@ 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_deprecation(
'longitudeone/geo-wkb-parser',
'1.0',
'Method readDoubles is deprecated, use readFloats instead.'
);

return $this->readFloats($count);
}
Expand Down
6 changes: 3 additions & 3 deletions quality/php-mess-detector/codeclimate-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
<rule ref="rulesets/codesize.xml">
<!-- Exclude the ExcessiveClassLength rule, because there are never too many tests in ParserTest -->
<exclude name="ExcessiveClassLength"/>
<!-- Exclude the ExcessiveMethodLength rule, because there are never too many tests in provider -->
<!-- Exclude the ExcessiveMethodLength rule, because there are never too many tests in providers -->
<exclude name="ExcessiveMethodLength"/>
<!-- Exclude the TooManyPublicMethods rule, because there are never too many tests -->
<exclude name="TooManyPublicMethods"/>
<exclude name="ExcessiveClassComplexity"/>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<!-- Set the maximum class complexity to 70 because of Parser, but Parser needs to be simplified -->
<property name="maximum" value="70"/>
<!-- Set the maximum class complexity to 71 because of Parser, but Parser needs to be simplified -->
<property name="maximum" value="71"/>
</properties>
</rule>
<!-- Import the entire naming rule set -->
Expand Down
4 changes: 2 additions & 2 deletions quality/php-mess-detector/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<!-- Set the maximum class complexity to 70 because of Parser, but Parser need to be simplified -->
<property name="maximum" value="70"/>
<!-- Set the maximum class complexity to 71 because of Parser, but Parser need to be simplified -->
<property name="maximum" value="71"/>
</properties>
</rule>
<!-- Import the entire naming rule set -->
Expand Down
2 changes: 1 addition & 1 deletion quality/php-mess-detector/test-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<rule ref="rulesets/codesize.xml">
<!-- Exclude the ExcessiveClassLength rule, because there are never too many tests in ParserTest -->
<exclude name="ExcessiveClassLength"/>
<!-- Exclude the ExcessiveMethodLength rule, because there are never too many tests in provider -->
<!-- Exclude the ExcessiveMethodLength rule, because there are never too many tests in providers -->
<exclude name="ExcessiveMethodLength"/>
<!-- Exclude the TooManyPublicMethods rule, because there are never too many tests -->
<exclude name="TooManyPublicMethods"/>
Expand Down
5 changes: 5 additions & 0 deletions quality/php-stan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 37 additions & 6 deletions tests/LongitudeOne/Geo/WKB/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{value:string|float, exception:class-string<ExceptionInterface>, message:string}, null, void>
*/
Expand Down Expand Up @@ -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<ExceptionInterface> $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
{
Expand Down Expand Up @@ -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);
}
}
}
27 changes: 27 additions & 0 deletions tests/LongitudeOne/Geo/WKB/Tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>|int|float|null}[] $methods
*
Expand Down

0 comments on commit 4886874

Please sign in to comment.