From ea5c313f4db6d01921d4b073673f3ca676bb5952 Mon Sep 17 00:00:00 2001 From: mmalac Date: Fri, 18 Jul 2025 10:55:43 +0200 Subject: [PATCH 01/16] refactor: switch properties to `readonly` where applicable and integrate Rector for automated refactoring Signed-off-by: mmalac --- composer.json | 3 + composer.lock | 120 +++++++++++++++++- rector.php | 15 +++ src/ClassMethodsHydrator.php | 2 +- src/Filter/MethodMatchFilter.php | 4 +- src/Filter/NumberOfParameterFilter.php | 2 +- .../CompositeNamingStrategy.php | 2 +- src/Strategy/BackedEnumStrategy.php | 2 +- src/Strategy/BooleanStrategy.php | 4 +- src/Strategy/CollectionStrategy.php | 4 +- src/Strategy/DateTimeFormatterStrategy.php | 8 +- .../DateTimeImmutableFormatterStrategy.php | 2 +- src/Strategy/ExplodeStrategy.php | 2 +- src/Strategy/HydratorStrategy.php | 4 +- src/Strategy/NullableStrategy.php | 2 +- src/Strategy/ScalarTypeStrategy.php | 2 +- src/Strategy/StrategyChain.php | 4 +- 17 files changed, 159 insertions(+), 23 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 6dc30726..3d909727 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "phpbench/phpbench": "^1.3.1", "phpunit/phpunit": "^10.5.38", "psalm/plugin-phpunit": "^0.19.0", + "rector/rector": "^2.1.0", "vimeo/psalm": "^5.26.1" }, "suggest": { @@ -69,6 +70,8 @@ ], "cs-check": "phpcs", "cs-fix": "phpcbf", + "rector": "rector -n -vv", + "rector:fix": "rector -vv", "static-analysis": "psalm --shepherd --stats", "test": "phpunit --colors=always", "test-coverage": "phpunit --colors=always --coverage-clover clover.xml" diff --git a/composer.lock b/composer.lock index 9d896e41..53e427ca 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0e03eca46a3c8e847190e5aee34912c7", + "content-hash": "fdad460db3968a663ffc353cf12cf8e5", "packages": [ { "name": "brick/varexporter", @@ -2293,6 +2293,64 @@ }, "time": "2025-02-19T13:28:12+00:00" }, + { + "name": "phpstan/phpstan", + "version": "2.1.18", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "ee1f390b7a70cdf74a2b737e554f68afea885db7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ee1f390b7a70cdf74a2b737e554f68afea885db7", + "reference": "ee1f390b7a70cdf74a2b737e554f68afea885db7", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2025-07-17T17:22:31+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "10.1.16", @@ -2882,6 +2940,66 @@ }, "time": "2024-09-11T13:17:53+00:00" }, + { + "name": "rector/rector", + "version": "2.1.2", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "40a71441dd73fa150a66102f5ca1364c44fc8fff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/40a71441dd73fa150a66102f5ca1364c44fc8fff", + "reference": "40a71441dd73fa150a66102f5ca1364c44fc8fff", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0", + "phpstan/phpstan": "^2.1.18" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "suggest": { + "ext-dom": "To manipulate phpunit.xml via the custom-rule command" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "homepage": "https://getrector.com/", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/2.1.2" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2025-07-17T19:30:06+00:00" + }, { "name": "sebastian/cli-parser", "version": "2.0.1", diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..954cc56e --- /dev/null +++ b/rector.php @@ -0,0 +1,15 @@ +withPhpSets(php81: true) + ->withPaths([ + __DIR__ . '/src', + __DIR__ . '/test', + ]); + +return $config; \ No newline at end of file diff --git a/src/ClassMethodsHydrator.php b/src/ClassMethodsHydrator.php index 34a3ae08..7c17d385 100644 --- a/src/ClassMethodsHydrator.php +++ b/src/ClassMethodsHydrator.php @@ -54,7 +54,7 @@ class ClassMethodsHydrator extends AbstractHydrator implements HydratorOptionsIn */ private $extractionMethodsCache = []; - private FilterInterface $optionalParametersFilter; + private readonly FilterInterface $optionalParametersFilter; /** * Define if extract values will use camel case or name with underscore diff --git a/src/Filter/MethodMatchFilter.php b/src/Filter/MethodMatchFilter.php index e59ce514..4e94f3bc 100644 --- a/src/Filter/MethodMatchFilter.php +++ b/src/Filter/MethodMatchFilter.php @@ -17,11 +17,11 @@ public function __construct( /** * The method to exclude */ - private string $method, + private readonly string $method, /** * Either an exclude or an include */ - private bool $exclude = true + private readonly bool $exclude = true ) { } diff --git a/src/Filter/NumberOfParameterFilter.php b/src/Filter/NumberOfParameterFilter.php index 1e2d3279..c741e789 100644 --- a/src/Filter/NumberOfParameterFilter.php +++ b/src/Filter/NumberOfParameterFilter.php @@ -20,7 +20,7 @@ public function __construct( /** * The number of parameters being accepted */ - private int $numberOfParameters = 0 + private readonly int $numberOfParameters = 0 ) { } diff --git a/src/NamingStrategy/CompositeNamingStrategy.php b/src/NamingStrategy/CompositeNamingStrategy.php index f1fec75f..02d6ca50 100644 --- a/src/NamingStrategy/CompositeNamingStrategy.php +++ b/src/NamingStrategy/CompositeNamingStrategy.php @@ -13,7 +13,7 @@ final class CompositeNamingStrategy implements NamingStrategyInterface /** @var NamingStrategyInterface[] */ private array $namingStrategies = []; - private NamingStrategyInterface $defaultNamingStrategy; + private readonly NamingStrategyInterface $defaultNamingStrategy; /** * @param NamingStrategyInterface[] $strategies indexed by the name they translate diff --git a/src/Strategy/BackedEnumStrategy.php b/src/Strategy/BackedEnumStrategy.php index 12059da9..03b6e75a 100644 --- a/src/Strategy/BackedEnumStrategy.php +++ b/src/Strategy/BackedEnumStrategy.php @@ -19,7 +19,7 @@ final class BackedEnumStrategy implements StrategyInterface { /** @var class-string */ - private string $enumClass; + private readonly string $enumClass; /** * @param class-string $enumClass diff --git a/src/Strategy/BooleanStrategy.php b/src/Strategy/BooleanStrategy.php index e6f1a5b0..fa7ae7f3 100644 --- a/src/Strategy/BooleanStrategy.php +++ b/src/Strategy/BooleanStrategy.php @@ -18,9 +18,9 @@ */ final class BooleanStrategy implements StrategyInterface { - private int|string $trueValue; + private readonly int|string $trueValue; - private int|string $falseValue; + private readonly int|string $falseValue; /** * @param int|string $trueValue diff --git a/src/Strategy/CollectionStrategy.php b/src/Strategy/CollectionStrategy.php index ff46646c..0b5b88a7 100644 --- a/src/Strategy/CollectionStrategy.php +++ b/src/Strategy/CollectionStrategy.php @@ -17,12 +17,12 @@ final class CollectionStrategy implements StrategyInterface { - private string $objectClassName; + private readonly string $objectClassName; /** * @throws Exception\InvalidArgumentException */ - public function __construct(private HydratorInterface $objectHydrator, string $objectClassName) + public function __construct(private readonly HydratorInterface $objectHydrator, string $objectClassName) { if (! class_exists($objectClassName)) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/Strategy/DateTimeFormatterStrategy.php b/src/Strategy/DateTimeFormatterStrategy.php index 13550bee..baea3f0a 100644 --- a/src/Strategy/DateTimeFormatterStrategy.php +++ b/src/Strategy/DateTimeFormatterStrategy.php @@ -24,7 +24,7 @@ final class DateTimeFormatterStrategy implements StrategyInterface * during hydration). These include `!` at the beginning of the string and * `|` at the end. */ - private string $extractionFormat; + private readonly string $extractionFormat; /** * @param bool $dateTimeFallback try to parse with DateTime when createFromFormat fails @@ -34,12 +34,12 @@ public function __construct( /** * Format to use during hydration. */ - private string $format = DateTime::RFC3339, - private ?DateTimeZone $timezone = null, + private readonly string $format = DateTime::RFC3339, + private readonly ?DateTimeZone $timezone = null, /** * Whether or not to allow hydration of values that do not follow the format exactly. */ - private bool $dateTimeFallback = false + private readonly bool $dateTimeFallback = false ) { $extractionFormat = preg_replace('/(?format); if (null === $extractionFormat) { diff --git a/src/Strategy/DateTimeImmutableFormatterStrategy.php b/src/Strategy/DateTimeImmutableFormatterStrategy.php index 2503fb87..00808719 100644 --- a/src/Strategy/DateTimeImmutableFormatterStrategy.php +++ b/src/Strategy/DateTimeImmutableFormatterStrategy.php @@ -11,7 +11,7 @@ final class DateTimeImmutableFormatterStrategy implements StrategyInterface { - public function __construct(private DateTimeFormatterStrategy $dateTimeStrategy) + public function __construct(private readonly DateTimeFormatterStrategy $dateTimeStrategy) { } diff --git a/src/Strategy/ExplodeStrategy.php b/src/Strategy/ExplodeStrategy.php index 37b5c67e..f49df6dd 100644 --- a/src/Strategy/ExplodeStrategy.php +++ b/src/Strategy/ExplodeStrategy.php @@ -24,7 +24,7 @@ final class ExplodeStrategy implements StrategyInterface * @param non-empty-string $delimiter String that the values will be split upon * @param int|null $explodeLimit Explode limit */ - public function __construct(string $delimiter = ',', private ?int $explodeLimit = null) + public function __construct(string $delimiter = ',', private readonly ?int $explodeLimit = null) { $this->setValueDelimiter($delimiter); } diff --git a/src/Strategy/HydratorStrategy.php b/src/Strategy/HydratorStrategy.php index 491f0a1d..48ad5ed8 100644 --- a/src/Strategy/HydratorStrategy.php +++ b/src/Strategy/HydratorStrategy.php @@ -16,13 +16,13 @@ final class HydratorStrategy implements StrategyInterface { - private string $objectClassName; + private readonly string $objectClassName; /** * @throws Exception\InvalidArgumentException */ public function __construct( - private HydratorInterface $objectHydrator, + private readonly HydratorInterface $objectHydrator, string $objectClassName ) { if (! class_exists($objectClassName)) { diff --git a/src/Strategy/NullableStrategy.php b/src/Strategy/NullableStrategy.php index a3241ac4..940b1b1b 100644 --- a/src/Strategy/NullableStrategy.php +++ b/src/Strategy/NullableStrategy.php @@ -8,7 +8,7 @@ final class NullableStrategy implements StrategyInterface { - public function __construct(private StrategyInterface $strategy, private bool $treatEmptyAsNull = false) + public function __construct(private readonly StrategyInterface $strategy, private readonly bool $treatEmptyAsNull = false) { } diff --git a/src/Strategy/ScalarTypeStrategy.php b/src/Strategy/ScalarTypeStrategy.php index c5c1d356..d940271d 100644 --- a/src/Strategy/ScalarTypeStrategy.php +++ b/src/Strategy/ScalarTypeStrategy.php @@ -36,7 +36,7 @@ public static function createToBoolean(): self return new self(self::TYPE_BOOLEAN); } - private function __construct(private string $type) + private function __construct(private readonly string $type) { } diff --git a/src/Strategy/StrategyChain.php b/src/Strategy/StrategyChain.php index 5370cb1f..450458f3 100644 --- a/src/Strategy/StrategyChain.php +++ b/src/Strategy/StrategyChain.php @@ -16,14 +16,14 @@ final class StrategyChain implements StrategyInterface * * @var StrategyInterface[] */ - private array $extractionStrategies; + private readonly array $extractionStrategies; /** * Strategy chain for hydration * * @var StrategyInterface[] */ - private array $hydrationStrategies; + private readonly array $hydrationStrategies; /** * @param StrategyInterface[] $extractionStrategies From 7f653d48a3295af1f94df3e3a442506c8fd59c8c Mon Sep 17 00:00:00 2001 From: mmalac Date: Sat, 19 Jul 2025 21:19:27 +0200 Subject: [PATCH 02/16] refactor: simplify property initialization, adjust docblocks and event listener setup, and update Rector/psalm configurations Signed-off-by: mmalac --- composer.json | 2 ++ psalm-baseline.xml | 11 ----------- rector.php | 3 +++ src/Aggregate/AggregateHydrator.php | 2 +- src/Aggregate/HydratorListener.php | 4 ++-- .../CamelCaseToUnderscoreFilter.php | 8 ++++++-- .../UnderscoreNamingStrategy/PcreReplacement.php | 5 +---- .../UnderscoreToCamelCaseFilter.php | 8 ++++---- src/StandaloneHydratorPluginManager.php | 6 +----- src/Strategy/BackedEnumStrategy.php | 6 +----- src/Strategy/NullableStrategy.php | 6 ++++-- test/Aggregate/HydratorListenerTest.php | 2 +- test/TestAsset/HydratorClosureStrategyEntity.php | 10 +--------- test/TestAsset/HydratorStrategyEntityB.php | 10 +--------- 14 files changed, 28 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index 3d909727..46da2316 100644 --- a/composer.json +++ b/composer.json @@ -73,6 +73,8 @@ "rector": "rector -n -vv", "rector:fix": "rector -vv", "static-analysis": "psalm --shepherd --stats", + "static-analysis:b": "psalm --update-baseline", + "static-analysis:clear": "psalm --clear-cache", "test": "phpunit --colors=always", "test-coverage": "phpunit --colors=always --coverage-clover clover.xml" }, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8d0a93e8..3896f0d6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -158,10 +158,6 @@ - - - - @@ -182,13 +178,6 @@ - - - - - - - replacement]]> diff --git a/rector.php b/rector.php index 954cc56e..c75b0046 100644 --- a/rector.php +++ b/rector.php @@ -10,6 +10,9 @@ ->withPaths([ __DIR__ . '/src', __DIR__ . '/test', + ]) + ->withSkip([ + \Rector\Php80\Rector\Ternary\GetDebugTypeRector::class, ]); return $config; \ No newline at end of file diff --git a/src/Aggregate/AggregateHydrator.php b/src/Aggregate/AggregateHydrator.php index 214e05fc..a7ffe698 100644 --- a/src/Aggregate/AggregateHydrator.php +++ b/src/Aggregate/AggregateHydrator.php @@ -53,7 +53,7 @@ public function hydrate(array $data, object $object) */ public function setEventManager(EventManagerInterface $eventManager): void { - $eventManager->setIdentifiers([self::class, static::class]); + $eventManager->setIdentifiers([self::class, self::class]); $this->eventManager = $eventManager; } diff --git a/src/Aggregate/HydratorListener.php b/src/Aggregate/HydratorListener.php index 7a000fd9..daffff20 100644 --- a/src/Aggregate/HydratorListener.php +++ b/src/Aggregate/HydratorListener.php @@ -24,8 +24,8 @@ public function __construct(protected HydratorInterface $hydrator) */ public function attach(EventManagerInterface $events, $priority = 1): void { - $this->listeners[] = $events->attach(HydrateEvent::EVENT_HYDRATE, [$this, 'onHydrate'], $priority); - $this->listeners[] = $events->attach(ExtractEvent::EVENT_EXTRACT, [$this, 'onExtract'], $priority); + $this->listeners[] = $events->attach(HydrateEvent::EVENT_HYDRATE, $this->onHydrate(...), $priority); + $this->listeners[] = $events->attach(ExtractEvent::EVENT_EXTRACT, $this->onExtract(...), $priority); } /** diff --git a/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php b/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php index edda7d2f..c710dc1f 100644 --- a/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php +++ b/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php @@ -73,8 +73,12 @@ private function getLowerFunction(): callable return $this->hasMbStringSupport() ? static fn($value): string => // ignore unicode characters w/ strtolower - mb_strtolower($value, 'UTF-8') + mb_strtolower((string) $value, 'UTF-8') : static fn($value) => - preg_replace_callback('#([A-Z])#', static fn($matches): string => strtolower($matches[1]), $value); + preg_replace_callback( + '#([A-Z])#', + static fn($matches): string => strtolower($matches[1]), + (string) $value + ); } } diff --git a/src/NamingStrategy/UnderscoreNamingStrategy/PcreReplacement.php b/src/NamingStrategy/UnderscoreNamingStrategy/PcreReplacement.php index f5db80d9..0ac60b3e 100644 --- a/src/NamingStrategy/UnderscoreNamingStrategy/PcreReplacement.php +++ b/src/NamingStrategy/UnderscoreNamingStrategy/PcreReplacement.php @@ -13,15 +13,12 @@ class PcreReplacement { /** @var callable */ public $replacement; - /** @var non-empty-string */ - public string $pattern; /** * @param non-empty-string $pattern */ - public function __construct(string $pattern, callable $replacement) + public function __construct(public string $pattern, callable $replacement) { $this->replacement = $replacement; - $this->pattern = $pattern; } } diff --git a/src/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilter.php b/src/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilter.php index 348df50c..a15e1e62 100644 --- a/src/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilter.php +++ b/src/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilter.php @@ -54,7 +54,7 @@ private function getPatternAndReplacement(string $pregQuotedSeparator): PcreRepl ? $this->getUnicodePatternAndReplacement($pregQuotedSeparator) : new PcreReplacement( '#(' . $pregQuotedSeparator . ')([\S]{1})#', - static fn($matches): string => strtoupper($matches[2]) + static fn($matches): string => strtoupper((string) $matches[2]) ); } @@ -63,19 +63,19 @@ private function getUnicodePatternAndReplacement(string $pregQuotedSeparator): P return $this->hasMbStringSupport() ? new PcreReplacement( '#(' . $pregQuotedSeparator . ')(\P{Z}{1})#u', - static fn($matches): string => mb_strtoupper($matches[2], 'UTF-8') + static fn($matches): string => mb_strtoupper((string) $matches[2], 'UTF-8') ) : new PcreReplacement( '#(' . $pregQuotedSeparator . ')' . '([^\p{Z}\p{Ll}]{1}|[a-zA-Z]{1})#u', - static fn($matches): string => strtoupper($matches[2]) + static fn($matches): string => strtoupper((string) $matches[2]) ); } private function getLcFirstFunction(): callable { return $this->hasMbStringSupport() - ? static fn($value): string => mb_strtolower($value[0], 'UTF-8') . substr($value, 1) + ? static fn($value): string => mb_strtolower((string) $value[0], 'UTF-8') . substr((string) $value, 1) : 'lcfirst'; } } diff --git a/src/StandaloneHydratorPluginManager.php b/src/StandaloneHydratorPluginManager.php index 59924839..9035c201 100644 --- a/src/StandaloneHydratorPluginManager.php +++ b/src/StandaloneHydratorPluginManager.php @@ -97,10 +97,6 @@ private function resolveName(string $name): ?string return $name; } - if (isset($this->aliases[$name])) { - return $this->aliases[$name]; - } - - return $this->aliases[strtolower($name)] ?? null; + return $this->aliases[$name] ?? $this->aliases[strtolower($name)] ?? null; } } diff --git a/src/Strategy/BackedEnumStrategy.php b/src/Strategy/BackedEnumStrategy.php index 03b6e75a..72cfb281 100644 --- a/src/Strategy/BackedEnumStrategy.php +++ b/src/Strategy/BackedEnumStrategy.php @@ -18,15 +18,11 @@ */ final class BackedEnumStrategy implements StrategyInterface { - /** @var class-string */ - private readonly string $enumClass; - /** * @param class-string $enumClass */ - public function __construct(string $enumClass) + public function __construct(private readonly string $enumClass) { - $this->enumClass = $enumClass; } /** diff --git a/src/Strategy/NullableStrategy.php b/src/Strategy/NullableStrategy.php index 940b1b1b..4410cc0e 100644 --- a/src/Strategy/NullableStrategy.php +++ b/src/Strategy/NullableStrategy.php @@ -8,8 +8,10 @@ final class NullableStrategy implements StrategyInterface { - public function __construct(private readonly StrategyInterface $strategy, private readonly bool $treatEmptyAsNull = false) - { + public function __construct( + private readonly StrategyInterface $strategy, + private readonly bool $treatEmptyAsNull = false + ) { } /** diff --git a/test/Aggregate/HydratorListenerTest.php b/test/Aggregate/HydratorListenerTest.php index e78b4252..4d368eac 100644 --- a/test/Aggregate/HydratorListenerTest.php +++ b/test/Aggregate/HydratorListenerTest.php @@ -37,7 +37,7 @@ public function testAttach(): void $this->logicalOr(HydrateEvent::EVENT_HYDRATE, ExtractEvent::EVENT_EXTRACT), $this->logicalAnd( $this->callback('is_callable'), - $this->logicalOr([$this->listener, 'onHydrate'], [$this->listener, 'onExtract']) + $this->logicalOr($this->listener->onHydrate(...), $this->listener->onExtract(...)) ) ); diff --git a/test/TestAsset/HydratorClosureStrategyEntity.php b/test/TestAsset/HydratorClosureStrategyEntity.php index 71e62285..2ca2c206 100644 --- a/test/TestAsset/HydratorClosureStrategyEntity.php +++ b/test/TestAsset/HydratorClosureStrategyEntity.php @@ -9,19 +9,11 @@ #[AllowDynamicProperties] class HydratorClosureStrategyEntity { - /** @var mixed */ - public $field1; - - /** @var mixed */ - public $field2; - /** * @param mixed $field1 * @param mixed $field2 */ - public function __construct($field1 = null, $field2 = null) + public function __construct(public $field1 = null, public $field2 = null) { - $this->field1 = $field1; - $this->field2 = $field2; } } diff --git a/test/TestAsset/HydratorStrategyEntityB.php b/test/TestAsset/HydratorStrategyEntityB.php index 1876a587..ea9a9ad4 100644 --- a/test/TestAsset/HydratorStrategyEntityB.php +++ b/test/TestAsset/HydratorStrategyEntityB.php @@ -6,20 +6,12 @@ class HydratorStrategyEntityB { - /** @var mixed */ - private $field1; - - /** @var mixed */ - private $field2; - /** * @param mixed $field1 * @param mixed $field2 */ - public function __construct($field1, $field2) + public function __construct(private $field1, private $field2) { - $this->field1 = $field1; - $this->field2 = $field2; } /** @return mixed */ From 8e257273f2f6ed599b000ed4a9fe199abfd303f1 Mon Sep 17 00:00:00 2001 From: mmalac Date: Sat, 19 Jul 2025 22:38:58 +0200 Subject: [PATCH 03/16] refactor: enforce strict typing Signed-off-by: mmalac --- psalm-baseline.xml | 25 -------------- rector.php | 3 ++ src/AbstractHydrator.php | 2 +- src/Aggregate/AggregateHydrator.php | 2 +- src/ArraySerializableHydrator.php | 2 +- src/ClassMethodsHydrator.php | 8 ++--- src/DelegatingHydrator.php | 2 +- src/Filter/FilterEnabledInterface.php | 2 +- src/HydrationInterface.php | 2 +- src/Iterator/HydratingIteratorInterface.php | 2 +- src/Iterator/HydratingIteratorIterator.php | 2 +- .../CamelCaseToUnderscoreFilter.php | 2 +- src/ObjectPropertyHydrator.php | 2 +- src/ReflectionHydrator.php | 2 +- src/Strategy/BackedEnumStrategy.php | 5 ++- src/Strategy/BooleanStrategy.php | 33 +++---------------- src/Strategy/ClosureStrategy.php | 4 +-- src/Strategy/CollectionStrategy.php | 4 +-- src/Strategy/DateTimeFormatterStrategy.php | 4 +-- .../DateTimeImmutableFormatterStrategy.php | 4 +-- src/Strategy/DefaultStrategy.php | 14 +++----- src/Strategy/ExplodeStrategy.php | 6 ++-- src/Strategy/HydratorStrategy.php | 5 ++- src/Strategy/NullableStrategy.php | 4 +-- src/Strategy/ScalarTypeStrategy.php | 13 ++------ src/Strategy/SerializableStrategy.php | 4 +-- src/Strategy/StrategyChain.php | 4 +-- src/Strategy/StrategyInterface.php | 4 +-- test/DelegatingHydratorTest.php | 2 +- .../IdentityNamingStrategyTest.php | 12 ++----- .../CamelCaseToUnderscoreFilterTest.php | 18 ++-------- .../UnderscoreToCamelCaseFilterTest.php | 20 +++-------- test/ObjectPropertyHydratorTest.php | 3 +- ...daloneHydratorPluginManagerFactoryTest.php | 2 +- test/Strategy/BooleanStrategyTest.php | 16 --------- test/Strategy/CollectionStrategyTest.php | 2 +- .../DateTimeFormatterStrategyTest.php | 6 +--- test/Strategy/ExplodeStrategyTest.php | 3 -- test/TestAsset/ArraySerializable.php | 4 +-- .../ClassMethodsMagicMethodSetter.php | 3 +- test/TestAsset/HydratorStrategy.php | 16 +++------ .../HydratorStrategyContextAware.php | 18 +++------- test/TestAsset/ObjectProperty.php | 3 +- test/TestAsset/ReflectionFilter.php | 12 +++---- test/TestAsset/SimpleEntity.php | 11 ++----- test/TestAsset/User.php | 5 +-- 46 files changed, 85 insertions(+), 237 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3896f0d6..b2bdeeec 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -226,14 +226,10 @@ - - - - @@ -242,14 +238,10 @@ - - - - @@ -298,32 +290,18 @@ - - - - - - - - - - - - - - @@ -617,8 +595,6 @@ - - @@ -700,7 +676,6 @@ - diff --git a/rector.php b/rector.php index c75b0046..7c6e8c90 100644 --- a/rector.php +++ b/rector.php @@ -11,6 +11,9 @@ __DIR__ . '/src', __DIR__ . '/test', ]) + ->withPreparedSets( + typeDeclarations: true, + ) ->withSkip([ \Rector\Php80\Rector\Ternary\GetDebugTypeRector::class, ]); diff --git a/src/AbstractHydrator.php b/src/AbstractHydrator.php index 46f0fb27..5d4c7fc0 100644 --- a/src/AbstractHydrator.php +++ b/src/AbstractHydrator.php @@ -190,7 +190,7 @@ public function getFilter(): Filter\FilterInterface * @param string $name Index in the composite * @param callable|Filter\FilterInterface $filter */ - public function addFilter(string $name, $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void + public function addFilter(string $name, mixed $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void { $this->getCompositeFilter()->addFilter($name, $filter, $condition); } diff --git a/src/Aggregate/AggregateHydrator.php b/src/Aggregate/AggregateHydrator.php index a7ffe698..e10ecbe4 100644 --- a/src/Aggregate/AggregateHydrator.php +++ b/src/Aggregate/AggregateHydrator.php @@ -41,7 +41,7 @@ public function extract(object $object): array /** * {@inheritDoc} */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { $event = new HydrateEvent($this, $object, $data); $this->getEventManager()->triggerEvent($event); diff --git a/src/ArraySerializableHydrator.php b/src/ArraySerializableHydrator.php index 859f0831..9bb5410f 100644 --- a/src/ArraySerializableHydrator.php +++ b/src/ArraySerializableHydrator.php @@ -75,7 +75,7 @@ public function extract(object $object): array * @throws Exception\BadMethodCallException For an $object not implementing exchangeArray() or populate(). * @throws Exception\RuntimeException If a part of $data could not be hydrated. */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { $replacement = []; foreach ($data as $key => $value) { diff --git a/src/ClassMethodsHydrator.php b/src/ClassMethodsHydrator.php index 7c17d385..0933ffb4 100644 --- a/src/ClassMethodsHydrator.php +++ b/src/ClassMethodsHydrator.php @@ -44,7 +44,7 @@ class ClassMethodsHydrator extends AbstractHydrator implements HydratorOptionsIn * * @var string[]|bool[] */ - private $hydrationMethodsCache = []; + private array $hydrationMethodsCache = []; /** * A map of extraction methods to property name to be used during extraction, indexed @@ -52,7 +52,7 @@ class ClassMethodsHydrator extends AbstractHydrator implements HydratorOptionsIn * * @var null[]|string[][] */ - private $extractionMethodsCache = []; + private array $extractionMethodsCache = []; private readonly FilterInterface $optionalParametersFilter; @@ -221,7 +221,7 @@ private function identifyAttributeName(object $object, string $method): string * * {@inheritDoc} */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { $objectClass = $object::class; @@ -248,7 +248,7 @@ public function hydrate(array $data, object $object) /** * {@inheritDoc} */ - public function addFilter(string $name, $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void + public function addFilter(string $name, mixed $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void { $this->resetCaches(); parent::addFilter($name, $filter, $condition); diff --git a/src/DelegatingHydrator.php b/src/DelegatingHydrator.php index ce6d7c22..7ff8b87c 100644 --- a/src/DelegatingHydrator.php +++ b/src/DelegatingHydrator.php @@ -15,7 +15,7 @@ public function __construct(protected ContainerInterface $hydrators) /** * {@inheritdoc} */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { return $this->getHydrator($object)->hydrate($data, $object); } diff --git a/src/Filter/FilterEnabledInterface.php b/src/Filter/FilterEnabledInterface.php index cd7bc203..16902d89 100644 --- a/src/Filter/FilterEnabledInterface.php +++ b/src/Filter/FilterEnabledInterface.php @@ -27,7 +27,7 @@ interface FilterEnabledInterface extends FilterProviderInterface * @param string $name Index in the composite * @param callable|FilterInterface $filter */ - public function addFilter(string $name, $filter, int $condition = FilterComposite::CONDITION_OR): void; + public function addFilter(string $name, mixed $filter, int $condition = FilterComposite::CONDITION_OR): void; /** * Check whether a specific filter exists at key $name or not diff --git a/src/HydrationInterface.php b/src/HydrationInterface.php index bb91316d..c1d5f320 100644 --- a/src/HydrationInterface.php +++ b/src/HydrationInterface.php @@ -17,5 +17,5 @@ interface HydrationInterface * @psalm-return T * @template T of object */ - public function hydrate(array $data, object $object); + public function hydrate(array $data, object $object): object; } diff --git a/src/Iterator/HydratingIteratorInterface.php b/src/Iterator/HydratingIteratorInterface.php index f0745056..43c1698a 100644 --- a/src/Iterator/HydratingIteratorInterface.php +++ b/src/Iterator/HydratingIteratorInterface.php @@ -22,7 +22,7 @@ interface HydratingIteratorInterface extends Iterator * * @param class-string|TPrototype $prototype */ - public function setPrototype($prototype): void; + public function setPrototype(string|object $prototype): void; /** * Sets the hydrator to use during iteration. diff --git a/src/Iterator/HydratingIteratorIterator.php b/src/Iterator/HydratingIteratorIterator.php index 30e7e051..9c531cff 100644 --- a/src/Iterator/HydratingIteratorIterator.php +++ b/src/Iterator/HydratingIteratorIterator.php @@ -46,7 +46,7 @@ public function __construct(HydratorInterface $hydrator, Iterator $data, $protot * @throws InvalidArgumentException If $prototype is a string, but refers to * a non-existent class. */ - public function setPrototype($prototype): void + public function setPrototype(string|object $prototype): void { if (is_object($prototype)) { $this->prototype = $prototype; diff --git a/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php b/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php index c710dc1f..c000eacb 100644 --- a/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php +++ b/src/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilter.php @@ -74,7 +74,7 @@ private function getLowerFunction(): callable ? static fn($value): string => // ignore unicode characters w/ strtolower mb_strtolower((string) $value, 'UTF-8') - : static fn($value) => + : static fn($value): ?string => preg_replace_callback( '#([A-Z])#', static fn($matches): string => strtolower($matches[1]), diff --git a/src/ObjectPropertyHydrator.php b/src/ObjectPropertyHydrator.php index 7dba9328..81188e9f 100644 --- a/src/ObjectPropertyHydrator.php +++ b/src/ObjectPropertyHydrator.php @@ -54,7 +54,7 @@ public function extract(object $object): array * * {@inheritDoc} */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { $properties = &self::$skippedPropertiesCache[$object::class] ?? null; diff --git a/src/ReflectionHydrator.php b/src/ReflectionHydrator.php index 97187434..30254051 100644 --- a/src/ReflectionHydrator.php +++ b/src/ReflectionHydrator.php @@ -42,7 +42,7 @@ public function extract(object $object): array * * {@inheritDoc} */ - public function hydrate(array $data, object $object) + public function hydrate(array $data, object $object): object { $reflProperties = self::getReflProperties($object); foreach ($data as $key => $value) { diff --git a/src/Strategy/BackedEnumStrategy.php b/src/Strategy/BackedEnumStrategy.php index 72cfb281..8797a7b3 100644 --- a/src/Strategy/BackedEnumStrategy.php +++ b/src/Strategy/BackedEnumStrategy.php @@ -28,7 +28,7 @@ public function __construct(private readonly string $enumClass) /** * @inheritDoc */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): int|string { if (! $value instanceof $this->enumClass) { throw new InvalidArgumentException(sprintf( @@ -42,10 +42,9 @@ public function extract($value, ?object $object = null) } /** - * @param mixed $value * @return T */ - public function hydrate($value, ?array $data) + public function hydrate(mixed $value, ?array $data = null): BackedEnum { if ($value instanceof $this->enumClass) { return $value; diff --git a/src/Strategy/BooleanStrategy.php b/src/Strategy/BooleanStrategy.php index fa7ae7f3..30cdcfa5 100644 --- a/src/Strategy/BooleanStrategy.php +++ b/src/Strategy/BooleanStrategy.php @@ -18,43 +18,18 @@ */ final class BooleanStrategy implements StrategyInterface { - private readonly int|string $trueValue; - - private readonly int|string $falseValue; - - /** - * @param int|string $trueValue - * @param int|string $falseValue - * @throws InvalidArgumentException - */ - public function __construct($trueValue, $falseValue) + public function __construct(private readonly int|string $trueValue, private readonly int|string $falseValue) { - if (! is_int($trueValue) && ! is_string($trueValue)) { - throw new InvalidArgumentException(sprintf( - 'Unable to instantiate BooleanStrategy. Expected int or string as $trueValue. %s was given', - is_object($trueValue) ? $trueValue::class : gettype($trueValue) - )); - } - - if (! is_int($falseValue) && ! is_string($falseValue)) { - throw new InvalidArgumentException(sprintf( - 'Unable to instantiate BooleanStrategy. Expected int or string as $falseValue. %s was given', - is_object($falseValue) ? $falseValue::class : gettype($falseValue) - )); - } - - $this->trueValue = $trueValue; - $this->falseValue = $falseValue; } /** * Converts the given value so that it can be extracted by the hydrator. * - * @param bool $value The original value. + * @param bool $value The original value. * @throws InvalidArgumentException * @return int|string Returns the value that should be extracted. */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): int|string { if (! is_bool($value)) { throw new InvalidArgumentException(sprintf( @@ -73,7 +48,7 @@ public function extract($value, ?object $object = null) * @throws InvalidArgumentException * @return bool Returns the value that should be hydrated. */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): bool { if (is_bool($value)) { return $value; diff --git a/src/Strategy/ClosureStrategy.php b/src/Strategy/ClosureStrategy.php index 602e64ac..5354c1d2 100644 --- a/src/Strategy/ClosureStrategy.php +++ b/src/Strategy/ClosureStrategy.php @@ -60,7 +60,7 @@ public function __construct(?callable $extractFunc = null, ?callable $hydrateFun * * {@inheritDoc} */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { $func = $this->extractFunc; return $func @@ -73,7 +73,7 @@ public function extract($value, ?object $object = null) * * {@inheritDoc} */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { $func = $this->hydrateFunc; return $func diff --git a/src/Strategy/CollectionStrategy.php b/src/Strategy/CollectionStrategy.php index 0b5b88a7..7d506a63 100644 --- a/src/Strategy/CollectionStrategy.php +++ b/src/Strategy/CollectionStrategy.php @@ -40,7 +40,7 @@ public function __construct(private readonly HydratorInterface $objectHydrator, * @throws Exception\InvalidArgumentException * @return mixed Returns the value that should be extracted. */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): array { if (! is_array($value)) { throw new Exception\InvalidArgumentException(sprintf( @@ -69,7 +69,7 @@ public function extract($value, ?object $object = null) * @throws Exception\InvalidArgumentException * @return object[] Returns the value that should be hydrated. */ - public function hydrate($value, ?array $data = null) + public function hydrate($value, ?array $data = null): array { if (! is_array($value)) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/Strategy/DateTimeFormatterStrategy.php b/src/Strategy/DateTimeFormatterStrategy.php index baea3f0a..ff9857b0 100644 --- a/src/Strategy/DateTimeFormatterStrategy.php +++ b/src/Strategy/DateTimeFormatterStrategy.php @@ -62,7 +62,7 @@ public function __construct( * will be returned unmodified; otherwise, it will be extracted to a * string. */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { if ($value instanceof DateTimeInterface) { return $value->format($this->extractionFormat); @@ -81,7 +81,7 @@ public function extract($value, ?object $object = null) * @throws Exception\InvalidArgumentException If $value is not null, not a * string, nor a DateTimeInterface. */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { if ($value === '' || $value === null || $value instanceof DateTimeInterface) { return $value; diff --git a/src/Strategy/DateTimeImmutableFormatterStrategy.php b/src/Strategy/DateTimeImmutableFormatterStrategy.php index 00808719..c91f59b6 100644 --- a/src/Strategy/DateTimeImmutableFormatterStrategy.php +++ b/src/Strategy/DateTimeImmutableFormatterStrategy.php @@ -25,7 +25,7 @@ public function __construct(private readonly DateTimeFormatterStrategy $dateTime * will be returned unmodified; otherwise, it will be extracted to a * string. */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { return $this->dateTimeStrategy->extract($value, $object); } @@ -40,7 +40,7 @@ public function extract($value, ?object $object = null) * @throws Exception\InvalidArgumentException If $value is not null, not a * string, nor a DateTimeInterface. */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { if ($value instanceof DateTimeImmutable) { return $value; diff --git a/src/Strategy/DefaultStrategy.php b/src/Strategy/DefaultStrategy.php index 2565f82c..d2ff30c5 100644 --- a/src/Strategy/DefaultStrategy.php +++ b/src/Strategy/DefaultStrategy.php @@ -7,23 +7,17 @@ class DefaultStrategy implements StrategyInterface { /** - * Converts the given value so that it can be extracted by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be extracted. + * @inheritDoc */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { return $value; } /** - * Converts the given value so that it can be hydrated by the hydrator. - * - * @param mixed $value The original value. - * @return mixed Returns the value that should be hydrated. + * @inheritDoc */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { return $value; } diff --git a/src/Strategy/ExplodeStrategy.php b/src/Strategy/ExplodeStrategy.php index f49df6dd..33df8e13 100644 --- a/src/Strategy/ExplodeStrategy.php +++ b/src/Strategy/ExplodeStrategy.php @@ -46,11 +46,11 @@ private function setValueDelimiter(string $delimiter): void * * Split a string by delimiter * - * @param string|null $value + * @param mixed $value * @return string[] * @throws Exception\InvalidArgumentException */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { if (null === $value) { return []; @@ -80,7 +80,7 @@ public function hydrate($value, ?array $data = null) * @return string|null * @throws Exception\InvalidArgumentException For non-array $value values. */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): ?string { if (! is_array($value)) { throw new Exception\InvalidArgumentException(sprintf( diff --git a/src/Strategy/HydratorStrategy.php b/src/Strategy/HydratorStrategy.php index 48ad5ed8..65035438 100644 --- a/src/Strategy/HydratorStrategy.php +++ b/src/Strategy/HydratorStrategy.php @@ -42,7 +42,7 @@ public function __construct( * @return mixed Returns the value that should be extracted. * @throws Exception\InvalidArgumentException */ - public function extract($value, ?object $object = null) + public function extract($value, ?object $object = null): array { if (! $value instanceof $this->objectClassName) { throw new Exception\InvalidArgumentException( @@ -60,11 +60,10 @@ public function extract($value, ?object $object = null) /** * @param mixed $value The original value. * @param null|array $data (optional) The original data for context. - * @return object|string|null * @throws ReflectionException * @throws Exception\InvalidArgumentException */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): object|string|null { if ( $value === '' diff --git a/src/Strategy/NullableStrategy.php b/src/Strategy/NullableStrategy.php index 4410cc0e..aba51e6a 100644 --- a/src/Strategy/NullableStrategy.php +++ b/src/Strategy/NullableStrategy.php @@ -19,7 +19,7 @@ public function __construct( * * {@inheritDoc} */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { if ($value === null) { return null; @@ -37,7 +37,7 @@ public function extract($value, ?object $object = null) * * {@inheritDoc} */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { if ($value === null) { return null; diff --git a/src/Strategy/ScalarTypeStrategy.php b/src/Strategy/ScalarTypeStrategy.php index d940271d..25fd971a 100644 --- a/src/Strategy/ScalarTypeStrategy.php +++ b/src/Strategy/ScalarTypeStrategy.php @@ -40,21 +40,12 @@ private function __construct(private readonly string $type) { } - /** - * @param mixed $value - * @return mixed - */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { return $value; } - /** - * @param mixed $value - * @param array|null $data - * @psalm-return null|scalar - */ - public function hydrate($value, ?array $data) + public function hydrate(mixed $value, ?array $data = null): null|int|float|string|bool { if ($value === null) { return null; diff --git a/src/Strategy/SerializableStrategy.php b/src/Strategy/SerializableStrategy.php index ca461c0b..c1bb8ecf 100644 --- a/src/Strategy/SerializableStrategy.php +++ b/src/Strategy/SerializableStrategy.php @@ -17,7 +17,7 @@ public function __construct(private readonly SerializerAdapter $serializer) * * {@inheritDoc} */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): string { return $this->serializer->serialize($value); } @@ -27,7 +27,7 @@ public function extract($value, ?object $object = null) * * {@inheritDoc} */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { return $this->serializer->unserialize($value); } diff --git a/src/Strategy/StrategyChain.php b/src/Strategy/StrategyChain.php index 450458f3..87141c3b 100644 --- a/src/Strategy/StrategyChain.php +++ b/src/Strategy/StrategyChain.php @@ -43,7 +43,7 @@ public function __construct(iterable $extractionStrategies) /** * {@inheritDoc} */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { foreach ($this->extractionStrategies as $strategy) { $value = $strategy->extract($value, $object); @@ -55,7 +55,7 @@ public function extract($value, ?object $object = null) /** * {@inheritDoc} */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { foreach ($this->hydrationStrategies as $strategy) { $value = $strategy->hydrate($value, $data); diff --git a/src/Strategy/StrategyInterface.php b/src/Strategy/StrategyInterface.php index 73900e2a..968b8200 100644 --- a/src/Strategy/StrategyInterface.php +++ b/src/Strategy/StrategyInterface.php @@ -13,7 +13,7 @@ interface StrategyInterface * @param null|object $object (optional) The original object for context. * @return mixed Returns the value that should be extracted. */ - public function extract($value, ?object $object = null); + public function extract(mixed $value, ?object $object = null): mixed; /** * Converts the given value so that it can be hydrated by the hydrator. @@ -22,5 +22,5 @@ public function extract($value, ?object $object = null); * @param null|array $data The original data for context. * @return mixed Returns the value that should be hydrated. */ - public function hydrate($value, ?array $data); + public function hydrate(mixed $value, ?array $data = null): mixed; } diff --git a/test/DelegatingHydratorTest.php b/test/DelegatingHydratorTest.php index 8e8a5678..a673fae2 100644 --- a/test/DelegatingHydratorTest.php +++ b/test/DelegatingHydratorTest.php @@ -22,7 +22,7 @@ class DelegatingHydratorTest extends TestCase * @var ContainerInterface|MockObject * @psalm-var ContainerInterface&MockObject */ - protected $hydrators; + protected MockObject $hydrators; /** @var ArrayObject */ protected $object; diff --git a/test/NamingStrategy/IdentityNamingStrategyTest.php b/test/NamingStrategy/IdentityNamingStrategyTest.php index 9a4bdfce..72ad5bc3 100644 --- a/test/NamingStrategy/IdentityNamingStrategyTest.php +++ b/test/NamingStrategy/IdentityNamingStrategyTest.php @@ -12,22 +12,16 @@ #[CoversClass(IdentityNamingStrategy::class)] class IdentityNamingStrategyTest extends TestCase { - /** - * @param string $name - */ #[DataProvider('getTestedNames')] - public function testHydrate($name): void + public function testHydrate(string $name): void { $namingStrategy = new IdentityNamingStrategy(); $this->assertSame($name, $namingStrategy->hydrate($name)); } - /** - * @param string $name - */ #[DataProvider('getTestedNames')] - public function testExtract($name): void + public function testExtract(string $name): void { $namingStrategy = new IdentityNamingStrategy(); @@ -39,7 +33,7 @@ public function testExtract($name): void * * @return string[][] */ - public static function getTestedNames() + public static function getTestedNames(): array { return [ 'foo' => ['foo'], diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php index 04d0ad57..d77e1d90 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php @@ -15,12 +15,8 @@ #[CoversClass(CamelCaseToUnderscoreFilter::class)] class CamelCaseToUnderscoreFilterTest extends TestCase { - /** - * @param string $string - * @param string $expected - */ #[DataProvider('nonUnicodeProvider')] - public function testFilterUnderscoresNonUnicodeStrings($string, $expected): void + public function testFilterUnderscoresNonUnicodeStrings(string $string, string $expected): void { $filter = new CamelCaseToUnderscoreFilter(); @@ -34,12 +30,8 @@ public function testFilterUnderscoresNonUnicodeStrings($string, $expected): void $this->assertEquals($expected, $filtered); } - /** - * @param string $string - * @param string $expected - */ #[DataProvider('unicodeProvider')] - public function testFilterUnderscoresUnicodeStrings($string, $expected): void + public function testFilterUnderscoresUnicodeStrings(string $string, string $expected): void { if (! extension_loaded('mbstring')) { $this->markTestSkipped('Extension mbstring not available'); @@ -53,12 +45,8 @@ public function testFilterUnderscoresUnicodeStrings($string, $expected): void $this->assertEquals($expected, $filtered); } - /** - * @param string $string - * @param string $expected - */ #[DataProvider('unicodeProviderWithoutMbStrings')] - public function testFilterUnderscoresUnicodeStringsWithoutMbStrings($string, $expected): void + public function testFilterUnderscoresUnicodeStringsWithoutMbStrings(string $string, string $expected): void { $filter = new CamelCaseToUnderscoreFilter(); diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php index 808a26cd..dbc5f1f1 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php @@ -15,12 +15,8 @@ #[CoversClass(UnderscoreToCamelCaseFilter::class)] class UnderscoreToCamelCaseFilterTest extends TestCase { - /** - * @param string $string - * @param string $expected - */ #[DataProvider('nonUnicodeProvider')] - public function testFilterCamelCasesNonUnicodeStrings($string, $expected): void + public function testFilterCamelCasesNonUnicodeStrings(string $string, string $expected): void { $filter = new UnderscoreToCamelCaseFilter(); @@ -60,12 +56,8 @@ public static function nonUnicodeProvider(): array ]; } - /** - * @param string $string - * @param string $expected - */ #[DataProvider('unicodeProvider')] - public function testFilterCamelCasesUnicodeStrings($string, $expected): void + public function testFilterCamelCasesUnicodeStrings(string $string, string $expected): void { if (! extension_loaded('mbstring')) { $this->markTestSkipped('Extension mbstring not available'); @@ -112,14 +104,10 @@ public static function unicodeProvider(): array ]; } - /** - * @param string $string - * @param string $expected - */ #[DataProvider('unicodeWithoutMbStringsProvider')] public function testFilterCamelCasesUnicodeStringsWithoutMbStrings( - $string, - $expected + string $string, + string $expected ): void { $filter = new UnderscoreToCamelCaseFilter(); diff --git a/test/ObjectPropertyHydratorTest.php b/test/ObjectPropertyHydratorTest.php index 0396cb21..4a9b86e0 100644 --- a/test/ObjectPropertyHydratorTest.php +++ b/test/ObjectPropertyHydratorTest.php @@ -17,8 +17,7 @@ class ObjectPropertyHydratorTest extends TestCase { use HydratorTestTrait; - /** @var ObjectPropertyHydrator */ - private $hydrator; + private ObjectPropertyHydrator $hydrator; /** * {@inheritDoc} diff --git a/test/StandaloneHydratorPluginManagerFactoryTest.php b/test/StandaloneHydratorPluginManagerFactoryTest.php index c14e2c99..8df31086 100644 --- a/test/StandaloneHydratorPluginManagerFactoryTest.php +++ b/test/StandaloneHydratorPluginManagerFactoryTest.php @@ -26,7 +26,7 @@ class StandaloneHydratorPluginManagerFactoryTest extends TestCase private StandaloneHydratorPluginManagerFactory $factory; /** @var ContainerInterface&MockObject */ - private ContainerInterface|MockObject $container; + private MockObject $container; protected function setUp(): void { diff --git a/test/Strategy/BooleanStrategyTest.php b/test/Strategy/BooleanStrategyTest.php index 75b500e4..bc314826 100644 --- a/test/Strategy/BooleanStrategyTest.php +++ b/test/Strategy/BooleanStrategyTest.php @@ -23,22 +23,6 @@ public function testConstructorWithValidString(): void $this->assertInstanceOf(BooleanStrategy::class, new BooleanStrategy('true', 'false')); } - public function testExceptionOnWrongTrueValueInConstructor(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expected int or string as $trueValue.'); - - new BooleanStrategy(true, 0); - } - - public function testExceptionOnWrongFalseValueInConstructor(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Expected int or string as $falseValue.'); - - new BooleanStrategy(1, false); - } - public function testExtractString(): void { $hydrator = new BooleanStrategy('true', 'false'); diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index 510d4527..16b6e2c4 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -260,7 +260,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void /** * @return HydratorInterface&MockObject */ - private function createHydratorMock() + private function createHydratorMock(): MockObject { return $this->createMock(HydratorInterface::class); } diff --git a/test/Strategy/DateTimeFormatterStrategyTest.php b/test/Strategy/DateTimeFormatterStrategyTest.php index a7da9276..bf2df6cb 100644 --- a/test/Strategy/DateTimeFormatterStrategyTest.php +++ b/test/Strategy/DateTimeFormatterStrategyTest.php @@ -83,12 +83,8 @@ public function testCanExtractAnyDateTimeInterface(): void $strategy->extract($dateImmutableMock); } - /** - * @param string $format - * @param string $expectedValue - */ #[DataProvider('formatsWithSpecialCharactersProvider')] - public function testAcceptsCreateFromFormatSpecialCharacters($format, $expectedValue): void + public function testAcceptsCreateFromFormatSpecialCharacters(string $format, string $expectedValue): void { $strategy = new DateTimeFormatterStrategy($format); $hydrated = $strategy->hydrate($expectedValue); diff --git a/test/Strategy/ExplodeStrategyTest.php b/test/Strategy/ExplodeStrategyTest.php index 0d06fb1f..c0e3fd5a 100644 --- a/test/Strategy/ExplodeStrategyTest.php +++ b/test/Strategy/ExplodeStrategyTest.php @@ -76,7 +76,6 @@ public function testHydrateWithInvalidScalarType(): void . ' array provided instead' ); - /** @psalm-suppress InvalidArgument */ $strategy->hydrate([]); } @@ -90,7 +89,6 @@ public function testHydrateWithInvalidObjectType(): void . ' stdClass provided instead' ); - /** @psalm-suppress InvalidArgument */ $strategy->hydrate(new stdClass()); } @@ -116,7 +114,6 @@ public function testHydration(mixed $value, string $delimiter, array $expected): { $strategy = new ExplodeStrategy($delimiter); - /** @psalm-suppress MixedArgument */ self::assertSame($expected, $strategy->hydrate($value)); } diff --git a/test/TestAsset/ArraySerializable.php b/test/TestAsset/ArraySerializable.php index c59fef9e..d64e705d 100644 --- a/test/TestAsset/ArraySerializable.php +++ b/test/TestAsset/ArraySerializable.php @@ -23,10 +23,8 @@ public function __construct() /** * Exchange internal values from provided array - * - * @return void */ - public function exchangeArray(array $array) + public function exchangeArray(array $array): void { $this->data = $array; } diff --git a/test/TestAsset/ClassMethodsMagicMethodSetter.php b/test/TestAsset/ClassMethodsMagicMethodSetter.php index 68372499..b46194c8 100644 --- a/test/TestAsset/ClassMethodsMagicMethodSetter.php +++ b/test/TestAsset/ClassMethodsMagicMethodSetter.php @@ -15,9 +15,8 @@ class ClassMethodsMagicMethodSetter /** * @param string $method - * @param array $args */ - public function __call($method, $args) + public function __call($method, array $args) { if (strlen($method) > 3 && strtolower(substr($method, 3)) === 'foo') { $this->foo = $args[0]; diff --git a/test/TestAsset/HydratorStrategy.php b/test/TestAsset/HydratorStrategy.php index 8388a8e5..e864740c 100644 --- a/test/TestAsset/HydratorStrategy.php +++ b/test/TestAsset/HydratorStrategy.php @@ -25,9 +25,9 @@ public function __construct() /** * @param array $value - * @return mixed + * @return list */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): array { $result = []; foreach ($value as $instance) { @@ -36,11 +36,7 @@ public function extract($value, ?object $object = null) return $result; } - /** - * @param mixed $value - * @return mixed - */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { $result = $value; if (is_array($value)) { @@ -52,11 +48,7 @@ public function hydrate($value, ?array $data = null) return $result; } - /** - * @param mixed $field1 - * @return mixed - */ - private function findEntity($field1) + private function findEntity(mixed $field1): mixed { $result = null; foreach ($this->simulatedStorageDevice as $entity) { diff --git a/test/TestAsset/HydratorStrategyContextAware.php b/test/TestAsset/HydratorStrategyContextAware.php index 2aa0c079..45b2686f 100644 --- a/test/TestAsset/HydratorStrategyContextAware.php +++ b/test/TestAsset/HydratorStrategyContextAware.php @@ -8,27 +8,17 @@ class HydratorStrategyContextAware extends DefaultStrategy { - /** @var object */ - public $object; + public object $object; - /** @var mixed */ - public $data; + public mixed $data; - /** - * @param array $value - * @return mixed - */ - public function extract($value, ?object $object = null) + public function extract(mixed $value, ?object $object = null): mixed { $this->object = $object; return $value; } - /** - * @param mixed $value - * @return mixed - */ - public function hydrate($value, ?array $data = null) + public function hydrate(mixed $value, ?array $data = null): mixed { $this->data = $data; return $value; diff --git a/test/TestAsset/ObjectProperty.php b/test/TestAsset/ObjectProperty.php index 601d419f..08c6fe71 100644 --- a/test/TestAsset/ObjectProperty.php +++ b/test/TestAsset/ObjectProperty.php @@ -21,8 +21,7 @@ class ObjectProperty /** @var string */ public $quo; - /** @var string */ - protected $quin; + protected string $quin; public function __construct() { diff --git a/test/TestAsset/ReflectionFilter.php b/test/TestAsset/ReflectionFilter.php index 67ff3136..c4e83ade 100644 --- a/test/TestAsset/ReflectionFilter.php +++ b/test/TestAsset/ReflectionFilter.php @@ -6,17 +6,13 @@ class ReflectionFilter { - /** @var string */ - protected $foo; + protected string $foo; - /** @var string */ - protected $bar; + protected string $bar; - /** @var string */ - protected $blubb; + protected string $blubb; - /** @var string */ - protected $quo; + protected string $quo; public function __construct() { diff --git a/test/TestAsset/SimpleEntity.php b/test/TestAsset/SimpleEntity.php index 290bd148..09283c99 100644 --- a/test/TestAsset/SimpleEntity.php +++ b/test/TestAsset/SimpleEntity.php @@ -13,9 +13,8 @@ class SimpleEntity /** * @param mixed $value - * @return void */ - public function setValue($value) + public function setValue($value): void { $this->value = $value; } @@ -30,10 +29,8 @@ public function getValue() /** * Exchange internal values from provided array - * - * @return void */ - public function exchangeArray(array $array) + public function exchangeArray(array $array): void { if (array_key_exists('value', $array)) { $this->setValue($array['value']); @@ -42,10 +39,8 @@ public function exchangeArray(array $array) /** * Return an array representation of the object - * - * @return array */ - public function getArrayCopy() + public function getArrayCopy(): array { return ['value' => $this->getValue()]; } diff --git a/test/TestAsset/User.php b/test/TestAsset/User.php index 76f2df82..ae7e398f 100644 --- a/test/TestAsset/User.php +++ b/test/TestAsset/User.php @@ -8,10 +8,7 @@ final class User { private ?string $name = null; - /** - * @return string - */ - public function name() + public function name(): ?string { return $this->name; } From a542b6c35f8e1b485c5886198159b63c8b0d7204 Mon Sep 17 00:00:00 2001 From: mmalac Date: Sun, 20 Jul 2025 21:10:31 +0200 Subject: [PATCH 04/16] ci: add Rector job workflow Signed-off-by: mmalac --- .github/workflows/continuous-integration.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 26c58025..baac9881 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -6,6 +6,26 @@ on: branches: tags: + +env: + default_php: 8.1 + jobs: ci: uses: laminas/workflow-continuous-integration/.github/workflows/continuous-integration.yml@1.x + + rector: + runs-on: ubuntu-latest + name: Run Rector on PHP + steps: + - uses: actions/checkout@v4.2.2 + - uses: shivammathur/setup-php@2.32.0 + with: + php-version: ${{ env.default_php }} + tools: composer + + - name: Install dependencies + run: composer install --no-progress --prefer-dist + + - name: Run Rector + run: composer rector \ No newline at end of file From d8f2fc22f0e0be0d529a5d3093d06b5bd56dab55 Mon Sep 17 00:00:00 2001 From: mmalac Date: Mon, 21 Jul 2025 21:57:24 +0200 Subject: [PATCH 05/16] refactor: replace `gettype` and `::class` checks with `get_debug_type` across strategies and tests Signed-off-by: mmalac --- psalm-baseline.xml | 37 ---------------------- rector.php | 5 +-- src/HydratorPluginManager.php | 5 ++- src/Strategy/BooleanStrategy.php | 7 ++-- src/Strategy/CollectionStrategy.php | 9 +++--- src/Strategy/DateTimeFormatterStrategy.php | 5 ++- src/Strategy/ExplodeStrategy.php | 7 ++-- src/Strategy/HydratorStrategy.php | 7 ++-- test/Strategy/CollectionStrategyTest.php | 9 +++--- test/Strategy/HydratorStrategyTest.php | 9 +++--- 10 files changed, 26 insertions(+), 74 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b2bdeeec..02c1b4d7 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -229,22 +229,10 @@ - - - - - - - - - - - - @@ -263,21 +251,11 @@ - - - - - - - - - - @@ -291,26 +269,14 @@ - - - - - - - - - objectClassName]]> - - - @@ -320,9 +286,6 @@ - - - diff --git a/rector.php b/rector.php index 7c6e8c90..911b3e30 100644 --- a/rector.php +++ b/rector.php @@ -13,9 +13,6 @@ ]) ->withPreparedSets( typeDeclarations: true, - ) - ->withSkip([ - \Rector\Php80\Rector\Ternary\GetDebugTypeRector::class, - ]); + ); return $config; \ No newline at end of file diff --git a/src/HydratorPluginManager.php b/src/HydratorPluginManager.php index 273aff68..dbaaed50 100644 --- a/src/HydratorPluginManager.php +++ b/src/HydratorPluginManager.php @@ -11,8 +11,7 @@ use Psr\Container\ContainerInterface; use function array_replace_recursive; -use function gettype; -use function is_object; +use function get_debug_type; use function sprintf; /** @@ -102,7 +101,7 @@ public function validate(mixed $instance): void throw new InvalidServiceException(sprintf( 'Plugin of type %s is invalid; must implement %s', - is_object($instance) ? $instance::class : gettype($instance), + get_debug_type($instance), HydratorInterface::class )); } diff --git a/src/Strategy/BooleanStrategy.php b/src/Strategy/BooleanStrategy.php index 30cdcfa5..c748a476 100644 --- a/src/Strategy/BooleanStrategy.php +++ b/src/Strategy/BooleanStrategy.php @@ -6,10 +6,9 @@ use Laminas\Hydrator\Exception\InvalidArgumentException; -use function gettype; +use function get_debug_type; use function is_bool; use function is_int; -use function is_object; use function is_string; use function sprintf; @@ -34,7 +33,7 @@ public function extract(mixed $value, ?object $object = null): int|string if (! is_bool($value)) { throw new InvalidArgumentException(sprintf( 'Unable to extract. Expected bool. %s was given.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } @@ -57,7 +56,7 @@ public function hydrate(mixed $value, ?array $data = null): bool if (! is_string($value) && ! is_int($value)) { throw new InvalidArgumentException(sprintf( 'Unable to hydrate. Expected bool, string or int. %s was given.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } diff --git a/src/Strategy/CollectionStrategy.php b/src/Strategy/CollectionStrategy.php index 7d506a63..14fa08b0 100644 --- a/src/Strategy/CollectionStrategy.php +++ b/src/Strategy/CollectionStrategy.php @@ -10,9 +10,8 @@ use function array_map; use function class_exists; -use function gettype; +use function get_debug_type; use function is_array; -use function is_object; use function sprintf; final class CollectionStrategy implements StrategyInterface @@ -45,7 +44,7 @@ public function extract(mixed $value, ?object $object = null): array if (! is_array($value)) { throw new Exception\InvalidArgumentException(sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } @@ -54,7 +53,7 @@ public function extract(mixed $value, ?object $object = null): array throw new Exception\InvalidArgumentException(sprintf( 'Value needs to be an instance of "%s", got "%s" instead.', $this->objectClassName, - is_object($object) ? $object::class : gettype($object) + get_debug_type($object) )); } @@ -74,7 +73,7 @@ public function hydrate($value, ?array $data = null): array if (! is_array($value)) { throw new Exception\InvalidArgumentException(sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } diff --git a/src/Strategy/DateTimeFormatterStrategy.php b/src/Strategy/DateTimeFormatterStrategy.php index ff9857b0..7918462d 100644 --- a/src/Strategy/DateTimeFormatterStrategy.php +++ b/src/Strategy/DateTimeFormatterStrategy.php @@ -8,8 +8,7 @@ use DateTimeInterface; use DateTimeZone; -use function gettype; -use function is_object; +use function get_debug_type; use function is_string; use function preg_replace; use function sprintf; @@ -90,7 +89,7 @@ public function hydrate(mixed $value, ?array $data = null): mixed if (! is_string($value)) { throw new Exception\InvalidArgumentException(sprintf( 'Unable to hydrate. Expected null, string, or DateTimeInterface; %s was given.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } diff --git a/src/Strategy/ExplodeStrategy.php b/src/Strategy/ExplodeStrategy.php index 33df8e13..1cea5ccf 100644 --- a/src/Strategy/ExplodeStrategy.php +++ b/src/Strategy/ExplodeStrategy.php @@ -5,11 +5,10 @@ namespace Laminas\Hydrator\Strategy; use function explode; -use function gettype; +use function get_debug_type; use function implode; use function is_array; use function is_numeric; -use function is_object; use function is_string; use function sprintf; @@ -60,7 +59,7 @@ public function hydrate(mixed $value, ?array $data = null): mixed throw new Exception\InvalidArgumentException(sprintf( '%s expects argument 1 to be string, %s provided instead', __METHOD__, - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } @@ -86,7 +85,7 @@ public function extract(mixed $value, ?object $object = null): ?string throw new Exception\InvalidArgumentException(sprintf( '%s expects argument 1 to be array, %s provided instead', __METHOD__, - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); } diff --git a/src/Strategy/HydratorStrategy.php b/src/Strategy/HydratorStrategy.php index 65035438..b49230f5 100644 --- a/src/Strategy/HydratorStrategy.php +++ b/src/Strategy/HydratorStrategy.php @@ -9,9 +9,8 @@ use ReflectionException; use function class_exists; -use function gettype; +use function get_debug_type; use function is_array; -use function is_object; use function sprintf; final class HydratorStrategy implements StrategyInterface @@ -49,7 +48,7 @@ public function extract($value, ?object $object = null): array sprintf( 'Value needs to be an instance of "%s", got "%s" instead.', $this->objectClassName, - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) ) ); } @@ -77,7 +76,7 @@ public function hydrate(mixed $value, ?array $data = null): object|string|null throw new Exception\InvalidArgumentException( sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) ) ); } diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index 16b6e2c4..9b1e1641 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -21,8 +21,7 @@ use function array_map; use function count; use function fopen; -use function gettype; -use function is_object; +use function get_debug_type; use function mt_getrandmax; use function mt_rand; use function spl_object_hash; @@ -79,7 +78,7 @@ public function testExtractRejectsInvalidValue(mixed $value): void $this->expectException(Exception\InvalidArgumentException::class); $this->expectExceptionMessage(sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); /** @psalm-suppress MixedArgument */ @@ -121,7 +120,7 @@ public function testExtractRejectsInvalidObject(mixed $object): void $this->expectExceptionMessage(sprintf( 'Value needs to be an instance of "%s", got "%s" instead.', TestAsset\User::class, - is_object($object) ? $object::class : gettype($object) + get_debug_type($object) )); $strategy->extract($value); @@ -192,7 +191,7 @@ public function testHydrateRejectsInvalidValue(mixed $value): void $this->expectException(Exception\InvalidArgumentException::class); $this->expectExceptionMessage(sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) )); /** @psalm-suppress MixedArgument */ diff --git a/test/Strategy/HydratorStrategyTest.php b/test/Strategy/HydratorStrategyTest.php index 95890591..e64512a7 100644 --- a/test/Strategy/HydratorStrategyTest.php +++ b/test/Strategy/HydratorStrategyTest.php @@ -20,8 +20,7 @@ use function count; use function fopen; -use function gettype; -use function is_object; +use function get_debug_type; use function mt_getrandmax; use function mt_rand; use function spl_object_hash; @@ -81,7 +80,7 @@ public function testExtractRejectsInvalidValue(mixed $value): void sprintf( 'Value needs to be an instance of "%s", got "%s" instead.', TestAsset\User::class, - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) ) ); @@ -120,7 +119,7 @@ public function testExtractRejectsInvalidObject(mixed $object): void sprintf( 'Value needs to be an instance of "%s", got "%s" instead.', TestAsset\User::class, - is_object($object) ? $object::class : gettype($object) + get_debug_type($object) ) ); @@ -184,7 +183,7 @@ public function testHydrateRejectsInvalidValue(mixed $value): void $this->expectExceptionMessage( sprintf( 'Value needs to be an array, got "%s" instead.', - is_object($value) ? $value::class : gettype($value) + get_debug_type($value) ) ); From 3ae0e71e203c58ca0bd6ac63bbc0f79077dbfb99 Mon Sep 17 00:00:00 2001 From: mmalac Date: Mon, 21 Jul 2025 22:11:48 +0200 Subject: [PATCH 06/16] ci: include rector.php in phpcs checks and streamline Rector configuration Signed-off-by: mmalac --- phpcs.xml | 1 + rector.php | 33 +++++++++++++++------------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 7f391d80..a85c462b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -15,6 +15,7 @@ src test + rector.php diff --git a/rector.php b/rector.php index 911b3e30..14a63755 100644 --- a/rector.php +++ b/rector.php @@ -1,18 +1,15 @@ -withPhpSets(php81: true) - ->withPaths([ - __DIR__ . '/src', - __DIR__ . '/test', - ]) - ->withPreparedSets( - typeDeclarations: true, - ); - -return $config; \ No newline at end of file +withPhpSets(php81: true) + ->withPaths([ + __DIR__ . '/src', + __DIR__ . '/test', + ]) + ->withPreparedSets( + typeDeclarations: true, + ); From 90c905e6c905576a6d09dace90db7db39fd6c7f2 Mon Sep 17 00:00:00 2001 From: mmalac Date: Mon, 21 Jul 2025 22:20:29 +0200 Subject: [PATCH 07/16] refactor: update type hints for filters, remove redundant psalm baseline entries Signed-off-by: mmalac --- psalm-baseline.xml | 10 ---------- src/AbstractHydrator.php | 13 +++++++++---- src/ClassMethodsHydrator.php | 7 +++++-- src/Filter/FilterEnabledInterface.php | 8 ++++++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 02c1b4d7..3e3ece73 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -422,11 +422,6 @@ field3]]> - - - - - @@ -459,11 +454,6 @@ - - - - - diff --git a/src/AbstractHydrator.php b/src/AbstractHydrator.php index 5d4c7fc0..b0b2b714 100644 --- a/src/AbstractHydrator.php +++ b/src/AbstractHydrator.php @@ -4,6 +4,8 @@ namespace Laminas\Hydrator; +use Laminas\Hydrator\Filter\FilterInterface; + use function sprintf; abstract class AbstractHydrator implements @@ -166,7 +168,7 @@ public function hydrateName(string $name, ?array $data = null): string /** * Get the filter instance */ - public function getFilter(): Filter\FilterInterface + public function getFilter(): FilterInterface { return $this->getCompositeFilter(); } @@ -188,10 +190,13 @@ public function getFilter(): Filter\FilterInterface * * * @param string $name Index in the composite - * @param callable|Filter\FilterInterface $filter + * @param callable(mixed): mixed|FilterInterface $filter */ - public function addFilter(string $name, mixed $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void - { + public function addFilter( + string $name, + callable|FilterInterface $filter, + int $condition = Filter\FilterComposite::CONDITION_OR + ): void { $this->getCompositeFilter()->addFilter($name, $filter, $condition); } diff --git a/src/ClassMethodsHydrator.php b/src/ClassMethodsHydrator.php index 0933ffb4..d9aee4e4 100644 --- a/src/ClassMethodsHydrator.php +++ b/src/ClassMethodsHydrator.php @@ -248,8 +248,11 @@ public function hydrate(array $data, object $object): object /** * {@inheritDoc} */ - public function addFilter(string $name, mixed $filter, int $condition = Filter\FilterComposite::CONDITION_OR): void - { + public function addFilter( + string $name, + callable|FilterInterface $filter, + int $condition = Filter\FilterComposite::CONDITION_OR + ): void { $this->resetCaches(); parent::addFilter($name, $filter, $condition); } diff --git a/src/Filter/FilterEnabledInterface.php b/src/Filter/FilterEnabledInterface.php index 16902d89..a2455038 100644 --- a/src/Filter/FilterEnabledInterface.php +++ b/src/Filter/FilterEnabledInterface.php @@ -25,9 +25,13 @@ interface FilterEnabledInterface extends FilterProviderInterface * * * @param string $name Index in the composite - * @param callable|FilterInterface $filter + * @param callable(mixed): mixed|FilterInterface $filter */ - public function addFilter(string $name, mixed $filter, int $condition = FilterComposite::CONDITION_OR): void; + public function addFilter( + string $name, + callable|FilterInterface $filter, + int $condition = FilterComposite::CONDITION_OR, + ): void; /** * Check whether a specific filter exists at key $name or not From 8c11f23a106087641b1c26fceda654a611492c66 Mon Sep 17 00:00:00 2001 From: mmalac Date: Mon, 21 Jul 2025 23:01:38 +0200 Subject: [PATCH 08/16] refactor: enforce strict type hints Signed-off-by: mmalac --- psalm-baseline.xml | 8 --- src/AbstractHydrator.php | 19 +++---- src/Aggregate/AggregateHydrator.php | 6 ++- src/Aggregate/ExtractEvent.php | 2 +- src/ClassMethodsHydrator.php | 8 +-- src/HydratorAwareTrait.php | 4 +- src/Iterator/HydratingIteratorIterator.php | 3 +- .../StringSupportTrait.php | 6 +-- src/StandaloneHydratorPluginManager.php | 4 +- test/ArraySerializableHydratorTest.php | 3 +- test/ClassMethodsHydratorTest.php | 3 +- test/DelegatingHydratorTest.php | 11 ++-- test/Filter/OptionalParametersFilterTest.php | 3 +- test/HydratorObjectPropertyTest.php | 10 ++-- test/HydratorTest.php | 18 +++---- test/ReflectionHydratorTest.php | 3 +- test/TestAsset/AggregateObject.php | 21 ++------ test/TestAsset/ArrayObjectObjectVars.php | 16 ------ test/TestAsset/ArraySerializable.php | 3 +- .../ArraySerializableNoGetArrayCopy.php | 3 +- test/TestAsset/ClassMethodsCamelCase.php | 54 +++++++------------ .../ClassMethodsCamelCaseMissing.php | 15 ++---- .../ClassMethodsMagicMethodSetter.php | 13 ++--- .../ClassMethodsOptionalParameters.php | 15 ++---- .../TestAsset/ClassMethodsProtectedSetter.php | 15 ++---- test/TestAsset/ClassMethodsTitleCase.php | 36 ++++++------- test/TestAsset/ClassMethodsUnderscore.php | 54 +++++++------------ .../HydratorClosureStrategyEntity.php | 6 +-- test/TestAsset/HydratorStrategyEntityA.php | 9 ++-- test/TestAsset/HydratorStrategyEntityB.php | 18 ++----- test/TestAsset/ObjectProperty.php | 12 ++--- test/TestAsset/Reflection.php | 6 +-- test/TestAsset/SimpleEntity.php | 15 ++---- 33 files changed, 134 insertions(+), 288 deletions(-) delete mode 100644 test/TestAsset/ArrayObjectObjectVars.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3e3ece73..c0e17a31 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,13 +1,5 @@ - - - eventManager]]> - - - - - diff --git a/src/AbstractHydrator.php b/src/AbstractHydrator.php index b0b2b714..214cbb16 100644 --- a/src/AbstractHydrator.php +++ b/src/AbstractHydrator.php @@ -19,21 +19,17 @@ abstract class AbstractHydrator implements * * @var Strategy\StrategyInterface[] */ - protected $strategies = []; + protected array $strategies = []; /** * An instance of NamingStrategy\NamingStrategyInterface - * - * @var null|NamingStrategy\NamingStrategyInterface */ - protected $namingStrategy; + protected ?NamingStrategy\NamingStrategyInterface $namingStrategy = null; /** * Composite to filter the methods, that need to be hydrated - * - * @var null|Filter\FilterComposite */ - protected $filterComposite; + protected ?Filter\FilterComposite $filterComposite = null; /** * Gets the strategy with the given name. @@ -114,9 +110,8 @@ public function removeStrategy(string $name): void * @param string $name The name of the strategy to use. * @param mixed $value The value that should be converted. * @param null|object $object The object is optionally provided as context. - * @return mixed */ - public function extractValue(string $name, mixed $value, ?object $object = null) + public function extractValue(string $name, mixed $value, ?object $object = null): mixed { return $this->hasStrategy($name) ? $this->getStrategy($name)->extract($value, $object) @@ -129,9 +124,8 @@ public function extractValue(string $name, mixed $value, ?object $object = null) * @param string $name The name of the strategy to use. * @param mixed $value The value that should be converted. * @param null|array $data The whole data is optionally provided as context. - * @return mixed */ - public function hydrateValue(string $name, mixed $value, ?array $data = null) + public function hydrateValue(string $name, mixed $value, ?array $data = null): mixed { return $this->hasStrategy($name) ? $this->getStrategy($name)->hydrate($value, $data) @@ -143,9 +137,8 @@ public function hydrateValue(string $name, mixed $value, ?array $data = null) * * @param string $name The name to convert. * @param null|object $object The object is optionally provided as context. - * @return string */ - public function extractName(string $name, ?object $object = null) + public function extractName(string $name, ?object $object = null): string { return $this->hasNamingStrategy() ? $this->getNamingStrategy()->extract($name, $object) diff --git a/src/Aggregate/AggregateHydrator.php b/src/Aggregate/AggregateHydrator.php index e10ecbe4..6f1cbc20 100644 --- a/src/Aggregate/AggregateHydrator.php +++ b/src/Aggregate/AggregateHydrator.php @@ -9,6 +9,8 @@ use Laminas\EventManager\EventManagerInterface; use Laminas\Hydrator\HydratorInterface; +use function assert; + /** * Aggregate hydrator that composes multiple hydrators via events */ @@ -16,8 +18,7 @@ final class AggregateHydrator implements HydratorInterface, EventManagerAwareInt { public const DEFAULT_PRIORITY = 1; - /** @var EventManagerInterface */ - protected $eventManager; + protected ?EventManagerInterface $eventManager = null; /** * Attaches the provided hydrator to the list of hydrators to be used while hydrating/extracting data @@ -64,6 +65,7 @@ public function getEventManager(): EventManagerInterface { if (null === $this->eventManager) { $this->setEventManager(new EventManager()); + assert($this->eventManager instanceof EventManagerInterface); } return $this->eventManager; diff --git a/src/Aggregate/ExtractEvent.php b/src/Aggregate/ExtractEvent.php index a789d22e..600e97c8 100644 --- a/src/Aggregate/ExtractEvent.php +++ b/src/Aggregate/ExtractEvent.php @@ -25,7 +25,7 @@ final class ExtractEvent extends Event protected $name = self::EVENT_EXTRACT; /** @var mixed[] Data being extracted from the $extractionObject */ - protected $extractedData = []; + protected array $extractedData = []; /** @psalm-param TTarget $target */ public function __construct(object $target, protected object $extractionObject) diff --git a/src/ClassMethodsHydrator.php b/src/ClassMethodsHydrator.php index d9aee4e4..c6e3d05c 100644 --- a/src/ClassMethodsHydrator.php +++ b/src/ClassMethodsHydrator.php @@ -25,18 +25,14 @@ class ClassMethodsHydrator extends AbstractHydrator implements HydratorOptionsIn { /** * Flag defining whether array keys are underscore-separated (true) or camel case (false) - * - * @var bool */ - protected $underscoreSeparatedKeys = true; + protected bool $underscoreSeparatedKeys = true; /** * Flag defining whether to check the setter method with method_exists to prevent the * hydrator from calling __call during hydration - * - * @var bool */ - protected $methodExistsCheck = false; + protected bool $methodExistsCheck = false; /** * Holds the names of the methods used for hydration, indexed by class::property name, diff --git a/src/HydratorAwareTrait.php b/src/HydratorAwareTrait.php index aed2fbb3..be526df3 100644 --- a/src/HydratorAwareTrait.php +++ b/src/HydratorAwareTrait.php @@ -8,10 +8,8 @@ trait HydratorAwareTrait { /** * Hydrator instance - * - * @var null|HydratorInterface */ - protected $hydrator; + protected ?HydratorInterface $hydrator = null; /** * Set hydrator diff --git a/src/Iterator/HydratingIteratorIterator.php b/src/Iterator/HydratingIteratorIterator.php index 9c531cff..edf255e7 100644 --- a/src/Iterator/HydratingIteratorIterator.php +++ b/src/Iterator/HydratingIteratorIterator.php @@ -24,8 +24,7 @@ */ class HydratingIteratorIterator extends IteratorIterator implements HydratingIteratorInterface { - /** @var HydratorInterface */ - protected $hydrator; + protected HydratorInterface $hydrator; /** @var TPrototype */ protected $prototype; diff --git a/src/NamingStrategy/UnderscoreNamingStrategy/StringSupportTrait.php b/src/NamingStrategy/UnderscoreNamingStrategy/StringSupportTrait.php index 067e4ed4..b607edbc 100644 --- a/src/NamingStrategy/UnderscoreNamingStrategy/StringSupportTrait.php +++ b/src/NamingStrategy/UnderscoreNamingStrategy/StringSupportTrait.php @@ -13,11 +13,9 @@ */ trait StringSupportTrait { - /** @var bool */ - private $pcreUnicodeSupport; + private ?bool $pcreUnicodeSupport = null; - /** @var bool */ - private $mbStringSupport; + private ?bool $mbStringSupport = null; private function hasPcreUnicodeSupport(): bool { diff --git a/src/StandaloneHydratorPluginManager.php b/src/StandaloneHydratorPluginManager.php index 9035c201..5a62a36b 100644 --- a/src/StandaloneHydratorPluginManager.php +++ b/src/StandaloneHydratorPluginManager.php @@ -37,7 +37,7 @@ final class StandaloneHydratorPluginManager implements HydratorPluginManagerInte * did not have the Hydrator suffix, it also maps the class name without * the suffix. * - * @var array + * @var array */ private array $aliases = [ 'arrayserializable' => ArraySerializableHydrator::class, @@ -83,7 +83,7 @@ public function get($id) /** * {@inheritDoc} */ - public function has($id) + public function has($id): bool { return null !== $this->resolveName($id); } diff --git a/test/ArraySerializableHydratorTest.php b/test/ArraySerializableHydratorTest.php index 10e1cfe3..f6cdeb25 100644 --- a/test/ArraySerializableHydratorTest.php +++ b/test/ArraySerializableHydratorTest.php @@ -21,8 +21,7 @@ class ArraySerializableHydratorTest extends TestCase { use HydratorTestTrait; - /** @var ArraySerializableHydrator */ - protected $hydrator; + protected ArraySerializableHydrator $hydrator; /** * {@inheritDoc} diff --git a/test/ClassMethodsHydratorTest.php b/test/ClassMethodsHydratorTest.php index 15e2c157..c4b48b9a 100644 --- a/test/ClassMethodsHydratorTest.php +++ b/test/ClassMethodsHydratorTest.php @@ -19,8 +19,7 @@ class ClassMethodsHydratorTest extends TestCase { use HydratorTestTrait; - /** @var ClassMethodsHydrator */ - protected $hydrator; + protected ClassMethodsHydrator $hydrator; /** * {@inheritDoc} diff --git a/test/DelegatingHydratorTest.php b/test/DelegatingHydratorTest.php index a673fae2..945966cc 100644 --- a/test/DelegatingHydratorTest.php +++ b/test/DelegatingHydratorTest.php @@ -15,17 +15,12 @@ #[CoversClass(DelegatingHydrator::class)] class DelegatingHydratorTest extends TestCase { - /** @var DelegatingHydrator */ - protected $hydrator; + protected DelegatingHydrator $hydrator; - /** - * @var ContainerInterface|MockObject - * @psalm-var ContainerInterface&MockObject - */ + /** @psalm-var ContainerInterface&MockObject */ protected MockObject $hydrators; - /** @var ArrayObject */ - protected $object; + protected ArrayObject $object; /** * {@inheritDoc} diff --git a/test/Filter/OptionalParametersFilterTest.php b/test/Filter/OptionalParametersFilterTest.php index e6ba7270..ba2fa8d4 100644 --- a/test/Filter/OptionalParametersFilterTest.php +++ b/test/Filter/OptionalParametersFilterTest.php @@ -13,8 +13,7 @@ #[CoversClass(OptionalParametersFilter::class)] class OptionalParametersFilterTest extends TestCase { - /** @var OptionalParametersFilter */ - protected $filter; + protected OptionalParametersFilter $filter; /** * {@inheritDoc} diff --git a/test/HydratorObjectPropertyTest.php b/test/HydratorObjectPropertyTest.php index f7a343ea..93392172 100644 --- a/test/HydratorObjectPropertyTest.php +++ b/test/HydratorObjectPropertyTest.php @@ -21,20 +21,18 @@ protected function setUp(): void public function testMultipleInvocationsWithDifferentFiltersFindsAllProperties(): void { $instance = new class { - /** @var int */ - public $id; + public int $id; /** @var int[] */ - public $array; + public array $array; /** @var object{id:int} */ - public $object; + public object $object; public function __construct() { $this->id = 4; $this->array = [4, 3, 5, 6]; $this->object = new class { - /** @var int */ - public $id = 4; + public int $id = 4; }; } }; diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 409254da..71c9838f 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -33,23 +33,17 @@ class HydratorTest extends TestCase { - /** @var ClassMethodsCamelCase */ - protected $classMethodsCamelCase; + protected ClassMethodsCamelCase $classMethodsCamelCase; - /** @var ClassMethodsTitleCase */ - protected $classMethodsTitleCase; + protected ClassMethodsTitleCase $classMethodsTitleCase; - /** @var ClassMethodsCamelCaseMissing */ - protected $classMethodsCamelCaseMissing; + protected ClassMethodsCamelCaseMissing $classMethodsCamelCaseMissing; - /** @var ClassMethodsUnderscore */ - protected $classMethodsUnderscore; + protected ClassMethodsUnderscore $classMethodsUnderscore; - /** @var ClassMethodsInvalidParameter */ - protected $classMethodsInvalidParameter; + protected ClassMethodsInvalidParameter $classMethodsInvalidParameter; - /** @var ReflectionAsset */ - protected $reflection; + protected ReflectionAsset $reflection; protected function setUp(): void { diff --git a/test/ReflectionHydratorTest.php b/test/ReflectionHydratorTest.php index 4b2fff8f..80120571 100644 --- a/test/ReflectionHydratorTest.php +++ b/test/ReflectionHydratorTest.php @@ -16,8 +16,7 @@ class ReflectionHydratorTest extends TestCase { use HydratorTestTrait; - /** @var ReflectionHydrator */ - protected $hydrator; + protected ReflectionHydrator $hydrator; /** * {@inheritDoc} diff --git a/test/TestAsset/AggregateObject.php b/test/TestAsset/AggregateObject.php index e211e1da..92036b18 100644 --- a/test/TestAsset/AggregateObject.php +++ b/test/TestAsset/AggregateObject.php @@ -10,32 +10,21 @@ */ class AggregateObject { - /** @var array */ - public $arrayData = ['president' => 'Zaphod']; + public array $arrayData = ['president' => 'Zaphod']; - /** @var string */ - public $maintainer = 'Marvin'; + public string $maintainer = 'Marvin'; - /** - * @return string - */ - public function getMaintainer() + public function getMaintainer(): string { return $this->maintainer; } - /** - * @param string $maintainer - */ - public function setMaintainer($maintainer): void + public function setMaintainer(string $maintainer): void { $this->maintainer = $maintainer; } - /** - * @return array - */ - public function getArrayCopy() + public function getArrayCopy(): array { return $this->arrayData; } diff --git a/test/TestAsset/ArrayObjectObjectVars.php b/test/TestAsset/ArrayObjectObjectVars.php deleted file mode 100644 index 3967295f..00000000 --- a/test/TestAsset/ArrayObjectObjectVars.php +++ /dev/null @@ -1,16 +0,0 @@ -fooBar; } - /** @param string $value */ - public function setFooBar($value): self + public function setFooBar(string $value): self { $this->fooBar = $value; return $this; } - /** @return string */ - public function getFooBarBaz() + public function getFooBarBaz(): string { return $this->fooBarBaz; } - /** @param string $value */ - public function setFooBarBaz($value): self + public function setFooBarBaz(string $value): self { $this->fooBarBaz = $value; return $this; } - /** @return bool */ - public function getIsFoo() + public function getIsFoo(): bool { return $this->isFoo; } - /** @param bool $value */ - public function setIsFoo($value): self + public function setIsFoo(bool $value): self { $this->isFoo = $value; return $this; } - /** @return bool */ - public function isBar() + public function isBar(): bool { return $this->isBar; } - /** @param bool $value */ - public function setIsBar($value): self + public function setIsBar(bool $value): self { $this->isBar = $value; return $this; } - /** @return bool */ - public function getHasFoo() + public function getHasFoo(): bool { return $this->hasFoo; } - /** @param bool $value */ - public function setHasFoo($value): self + public function setHasFoo(bool $value): self { $this->hasFoo = $value; return $this; } - /** @return bool */ - public function hasBar() + public function hasBar(): bool { return $this->hasBar; } - /** @param bool $value */ - public function setHasBar($value): self + public function setHasBar(bool $value): self { $this->hasBar = $value; return $this; diff --git a/test/TestAsset/ClassMethodsCamelCaseMissing.php b/test/TestAsset/ClassMethodsCamelCaseMissing.php index d90a14cb..825aae04 100644 --- a/test/TestAsset/ClassMethodsCamelCaseMissing.php +++ b/test/TestAsset/ClassMethodsCamelCaseMissing.php @@ -6,27 +6,22 @@ class ClassMethodsCamelCaseMissing { - /** @var string */ - protected $fooBar = '1'; + protected string $fooBar = '1'; - /** @var string */ - protected $fooBarBaz = '2'; + protected string $fooBarBaz = '2'; - /** @return string */ - public function getFooBar() + public function getFooBar(): string { return $this->fooBar; } - /** @param string $value */ - public function setFooBar($value): self + public function setFooBar(string $value): self { $this->fooBar = $value; return $this; } - /** @return string */ - public function getFooBarBaz() + public function getFooBarBaz(): string { return $this->fooBarBaz; } diff --git a/test/TestAsset/ClassMethodsMagicMethodSetter.php b/test/TestAsset/ClassMethodsMagicMethodSetter.php index b46194c8..4f3478d7 100644 --- a/test/TestAsset/ClassMethodsMagicMethodSetter.php +++ b/test/TestAsset/ClassMethodsMagicMethodSetter.php @@ -10,22 +10,17 @@ class ClassMethodsMagicMethodSetter { - /** @var mixed */ - protected $foo; + protected mixed $foo; - /** - * @param string $method - */ - public function __call($method, array $args) + public function __call(string $method, array $args) { if (strlen($method) > 3 && strtolower(substr($method, 3)) === 'foo') { $this->foo = $args[0]; } } - /** @return mixed */ - public function getFoo() + public function getFoo(): mixed { - return $this->foo; + return $this->foo ?? null; } } diff --git a/test/TestAsset/ClassMethodsOptionalParameters.php b/test/TestAsset/ClassMethodsOptionalParameters.php index 46327924..79e63bc7 100644 --- a/test/TestAsset/ClassMethodsOptionalParameters.php +++ b/test/TestAsset/ClassMethodsOptionalParameters.php @@ -9,23 +9,14 @@ */ class ClassMethodsOptionalParameters { - /** @var string */ - public $foo = 'bar'; + public string $foo = 'bar'; - /** - * @param mixed $optional - * @return string - */ - public function getFoo($optional = null) + public function getFoo(mixed $optional = null): string { return $this->foo; } - /** - * @param string $foo - * @param mixed $optional - */ - public function setFoo($foo, $optional = null): void + public function setFoo(string $foo, mixed $optional = null): void { $this->foo = $foo; } diff --git a/test/TestAsset/ClassMethodsProtectedSetter.php b/test/TestAsset/ClassMethodsProtectedSetter.php index 1ea99893..0ca58d36 100644 --- a/test/TestAsset/ClassMethodsProtectedSetter.php +++ b/test/TestAsset/ClassMethodsProtectedSetter.php @@ -6,26 +6,21 @@ class ClassMethodsProtectedSetter { - /** @var mixed */ - protected $foo; + protected mixed $foo; - /** @var mixed */ - protected $bar; + protected mixed $bar; - /** @param mixed $foo */ - protected function setFoo($foo): void + protected function setFoo(mixed $foo): void { $this->foo = $foo; } - /** @param mixed $bar */ - public function setBar($bar): void + public function setBar(mixed $bar): void { $this->bar = $bar; } - /** @return mixed */ - public function getBar() + public function getBar(): mixed { return $this->bar; } diff --git a/test/TestAsset/ClassMethodsTitleCase.php b/test/TestAsset/ClassMethodsTitleCase.php index b4a77bb8..765fdea2 100644 --- a/test/TestAsset/ClassMethodsTitleCase.php +++ b/test/TestAsset/ClassMethodsTitleCase.php @@ -6,79 +6,79 @@ class ClassMethodsTitleCase { - protected $FooBar = '1'; + protected string $FooBar = '1'; - protected $FooBarBaz = '2'; + protected string $FooBarBaz = '2'; - protected $IsFoo = true; + protected bool $IsFoo = true; - protected $IsBar = true; + protected bool $IsBar = true; - protected $HasFoo = true; + protected bool $HasFoo = true; - protected $HasBar = true; + protected bool $HasBar = true; - public function getFooBar() + public function getFooBar(): string { return $this->FooBar; } - public function setFooBar($value): self + public function setFooBar(string $value): self { $this->FooBar = $value; return $this; } - public function getFooBarBaz() + public function getFooBarBaz(): string { return $this->FooBarBaz; } - public function setFooBarBaz($value): self + public function setFooBarBaz(string $value): self { $this->FooBarBaz = $value; return $this; } - public function getIsFoo() + public function getIsFoo(): bool { return $this->IsFoo; } - public function setIsFoo($IsFoo): self + public function setIsFoo(bool $IsFoo): self { $this->IsFoo = $IsFoo; return $this; } - public function getIsBar() + public function getIsBar(): bool { return $this->IsBar; } - public function setIsBar($IsBar): self + public function setIsBar(bool $IsBar): self { $this->IsBar = $IsBar; return $this; } - public function getHasFoo() + public function getHasFoo(): bool { return $this->HasFoo; } - public function getHasBar() + public function getHasBar(): bool { return $this->HasBar; } - public function setHasFoo($HasFoo): self + public function setHasFoo(bool $HasFoo): self { $this->HasFoo = $HasFoo; return $this; } - public function setHasBar($HasBar): void + public function setHasBar(bool $HasBar): void { $this->HasBar = $HasBar; } diff --git a/test/TestAsset/ClassMethodsUnderscore.php b/test/TestAsset/ClassMethodsUnderscore.php index 476720d2..57ce8219 100644 --- a/test/TestAsset/ClassMethodsUnderscore.php +++ b/test/TestAsset/ClassMethodsUnderscore.php @@ -7,97 +7,79 @@ class ClassMethodsUnderscore { - /** @var string */ - protected $foo_bar = '1'; + protected string $foo_bar = '1'; - /** @var string */ - protected $foo_bar_baz = '2'; + protected string $foo_bar_baz = '2'; - /** @var bool */ - protected $is_foo = true; + protected bool $is_foo = true; - /** @var bool */ - protected $is_bar = true; + protected bool $is_bar = true; - /** @var bool */ - protected $has_foo = true; + protected bool $has_foo = true; - /** @var bool */ - protected $has_bar = true; + protected bool $has_bar = true; - /** @return string */ - public function getFooBar() + public function getFooBar(): string { return $this->foo_bar; } - /** @param string $value */ - public function setFooBar($value): self + public function setFooBar(string $value): self { $this->foo_bar = $value; return $this; } - /** @return string */ - public function getFooBarBaz() + public function getFooBarBaz(): string { return $this->foo_bar_baz; } - /** @param string $value */ - public function setFooBarBaz($value): self + public function setFooBarBaz(string $value): self { $this->foo_bar_baz = $value; return $this; } - /** @return bool */ - public function getIsFoo() + public function getIsFoo(): bool { return $this->is_foo; } - /** @param bool $value */ - public function setIsFoo($value): self + public function setIsFoo(bool $value): self { $this->is_foo = $value; return $this; } - /** @return bool */ - public function isBar() + public function isBar(): bool { return $this->is_bar; } - /** @param bool $value */ - public function setIsBar($value): self + public function setIsBar(bool $value): self { $this->is_bar = $value; return $this; } - /** @return bool */ - public function getHasFoo() + public function getHasFoo(): bool { return $this->has_foo; } - /** @param bool $value */ - public function setHasFoo($value): self + public function setHasFoo(bool $value): self { $this->has_foo = $value; return $this; } - /** @return bool */ - public function hasBar() + public function hasBar(): bool { return $this->has_bar; } - /** @param bool $value */ - public function setHasBar($value): self + public function setHasBar(bool $value): self { $this->has_bar = $value; return $this; diff --git a/test/TestAsset/HydratorClosureStrategyEntity.php b/test/TestAsset/HydratorClosureStrategyEntity.php index 2ca2c206..be52115d 100644 --- a/test/TestAsset/HydratorClosureStrategyEntity.php +++ b/test/TestAsset/HydratorClosureStrategyEntity.php @@ -9,11 +9,7 @@ #[AllowDynamicProperties] class HydratorClosureStrategyEntity { - /** - * @param mixed $field1 - * @param mixed $field2 - */ - public function __construct(public $field1 = null, public $field2 = null) + public function __construct(public mixed $field1 = null, public mixed $field2 = null) { } } diff --git a/test/TestAsset/HydratorStrategyEntityA.php b/test/TestAsset/HydratorStrategyEntityA.php index a3d00f52..2e31fd12 100644 --- a/test/TestAsset/HydratorStrategyEntityA.php +++ b/test/TestAsset/HydratorStrategyEntityA.php @@ -9,7 +9,7 @@ class HydratorStrategyEntityA { /** @var HydratorStrategyEntityB[] */ - public $entities; // public to make testing easier! + public array $entities; // public to make testing easier! public function __construct() { @@ -24,7 +24,7 @@ public function addEntity(HydratorStrategyEntityB $entity): void /** * @return HydratorStrategyEntityB[] */ - public function getEntities() + public function getEntities(): array { return $this->entities; } @@ -32,7 +32,7 @@ public function getEntities() /** * @param HydratorStrategyEntityB[] $entities */ - public function setEntities($entities): void + public function setEntities(array $entities): void { $this->entities = $entities; } @@ -50,10 +50,9 @@ public function getArrayCopy(): array /** * Add the populate method so we can test the ArraySerializable hydrator: * - * @param array $data * @psalm-param array $data */ - public function populate($data): void + public function populate(array $data): void { foreach ($data as $name => $value) { $this->$name = $value; diff --git a/test/TestAsset/HydratorStrategyEntityB.php b/test/TestAsset/HydratorStrategyEntityB.php index ea9a9ad4..343c0c55 100644 --- a/test/TestAsset/HydratorStrategyEntityB.php +++ b/test/TestAsset/HydratorStrategyEntityB.php @@ -6,35 +6,27 @@ class HydratorStrategyEntityB { - /** - * @param mixed $field1 - * @param mixed $field2 - */ - public function __construct(private $field1, private $field2) + public function __construct(private mixed $field1, private mixed $field2) { } - /** @return mixed */ - public function getField1() + public function getField1(): mixed { return $this->field1; } - /** @return mixed */ - public function getField2() + public function getField2(): mixed { return $this->field2; } - /** @param mixed $value */ - public function setField1($value): self + public function setField1(mixed $value): self { $this->field1 = $value; return $this; } - /** @param mixed $value */ - public function setField2($value): self + public function setField2(mixed $value): self { $this->field2 = $value; return $this; diff --git a/test/TestAsset/ObjectProperty.php b/test/TestAsset/ObjectProperty.php index 08c6fe71..93f5658c 100644 --- a/test/TestAsset/ObjectProperty.php +++ b/test/TestAsset/ObjectProperty.php @@ -9,17 +9,13 @@ #[AllowDynamicProperties] class ObjectProperty { - /** @var string */ - public $foo; + public string $foo; - /** @var string */ - public $bar; + public string $bar; - /** @var string */ - public $blubb; + public string $blubb; - /** @var string */ - public $quo; + public string $quo; protected string $quin; diff --git a/test/TestAsset/Reflection.php b/test/TestAsset/Reflection.php index df22b80c..2d30f145 100644 --- a/test/TestAsset/Reflection.php +++ b/test/TestAsset/Reflection.php @@ -6,11 +6,9 @@ class Reflection { - /** @var string */ - public $foo = '1'; + public string $foo = '1'; - /** @var string */ - protected $fooBar = '2'; + protected string $fooBar = '2'; private string $fooBarBaz = '3'; diff --git a/test/TestAsset/SimpleEntity.php b/test/TestAsset/SimpleEntity.php index 09283c99..32692c6d 100644 --- a/test/TestAsset/SimpleEntity.php +++ b/test/TestAsset/SimpleEntity.php @@ -8,23 +8,16 @@ class SimpleEntity { - /** @var mixed */ - public $value; + public mixed $value; - /** - * @param mixed $value - */ - public function setValue($value): void + public function setValue(mixed $value): void { $this->value = $value; } - /** - * @return mixed - */ - public function getValue() + public function getValue(): mixed { - return $this->value; + return $this->value ?? null; } /** From 8eaee048841e6cf78b5a7bfa9f5f18b9cb107c61 Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 21:18:22 +0200 Subject: [PATCH 09/16] refactor: enforce strict type hints Signed-off-by: mmalac --- psalm-baseline.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c0e17a31..a33792e5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -151,18 +151,6 @@ - - - mbStringSupport === null]]> - pcreUnicodeSupport === null]]> - - - - - - - - From cfc73ff8121e0806e1484f210b94d310e0e079fb Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 21:33:57 +0200 Subject: [PATCH 10/16] refactor: improve type annotations Signed-off-by: mmalac --- psalm-baseline.xml | 5 --- src/Strategy/CollectionStrategy.php | 19 ++++++----- test/Strategy/CollectionStrategyTest.php | 41 ------------------------ 3 files changed, 11 insertions(+), 54 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index a33792e5..3dd03445 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -221,16 +221,12 @@ - - objectClassName]]> - - @@ -545,7 +541,6 @@ - diff --git a/src/Strategy/CollectionStrategy.php b/src/Strategy/CollectionStrategy.php index 14fa08b0..81c442ab 100644 --- a/src/Strategy/CollectionStrategy.php +++ b/src/Strategy/CollectionStrategy.php @@ -14,28 +14,31 @@ use function is_array; use function sprintf; +/** + * @template T of object + */ final class CollectionStrategy implements StrategyInterface { - private readonly string $objectClassName; - /** + * @param class-string $objectClassName * @throws Exception\InvalidArgumentException */ - public function __construct(private readonly HydratorInterface $objectHydrator, string $objectClassName) - { - if (! class_exists($objectClassName)) { + public function __construct( + private readonly HydratorInterface $objectHydrator, + private readonly string $objectClassName + ) { + if (! class_exists($this->objectClassName)) { throw new Exception\InvalidArgumentException(sprintf( 'Object class name needs to be the name of an existing class, got "%s" instead.', - $objectClassName + $this->objectClassName )); } - $this->objectClassName = $objectClassName; } /** * Converts the given value so that it can be extracted by the hydrator. * - * @param mixed[] $value The original value. + * @param object[] $value The original value. * @throws Exception\InvalidArgumentException * @return mixed Returns the value that should be extracted. */ diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index 9b1e1641..f51dcd9f 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -106,47 +106,6 @@ public static function providerInvalidValueForExtraction(): Generator } } - #[DataProvider('providerInvalidObjectForExtraction')] - public function testExtractRejectsInvalidObject(mixed $object): void - { - $value = [$object]; - - $strategy = new CollectionStrategy( - $this->createHydratorMock(), - TestAsset\User::class - ); - - $this->expectException(Exception\InvalidArgumentException::class); - $this->expectExceptionMessage(sprintf( - 'Value needs to be an instance of "%s", got "%s" instead.', - TestAsset\User::class, - get_debug_type($object) - )); - - $strategy->extract($value); - } - - /** - * @return Generator> - */ - public static function providerInvalidObjectForExtraction(): Generator - { - $values = [ - 'boolean-false' => false, - 'boolean-true' => true, - 'float' => mt_rand() / mt_getrandmax(), - 'integer' => mt_rand(), - 'null' => null, - 'object-but-not-instance-of-object-class' => new stdClass(), - 'resource' => fopen(__FILE__, 'r'), - 'string-non-existent-class' => 'FooBarBaz9000', - ]; - - foreach ($values as $key => $value) { - yield $key => [$value]; - } - } - public function testExtractUsesHydratorToExtractValues(): void { $value = [ From bbd7d1f9a3440e5c47cadea427611268d69d0b00 Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 21:42:02 +0200 Subject: [PATCH 11/16] refactor: update type annotations to improve strict typing consistency Signed-off-by: mmalac --- test/DelegatingHydratorTest.php | 3 +-- test/HydratorPluginManagerFactoryTest.php | 4 ++-- test/StandaloneHydratorPluginManagerFactoryTest.php | 3 +-- test/Strategy/CollectionStrategyTest.php | 5 +---- test/Strategy/HydratorStrategyTest.php | 5 +---- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/test/DelegatingHydratorTest.php b/test/DelegatingHydratorTest.php index 945966cc..ffe0a41a 100644 --- a/test/DelegatingHydratorTest.php +++ b/test/DelegatingHydratorTest.php @@ -17,8 +17,7 @@ class DelegatingHydratorTest extends TestCase { protected DelegatingHydrator $hydrator; - /** @psalm-var ContainerInterface&MockObject */ - protected MockObject $hydrators; + protected ContainerInterface&MockObject $hydrators; protected ArrayObject $object; diff --git a/test/HydratorPluginManagerFactoryTest.php b/test/HydratorPluginManagerFactoryTest.php index fcfc7f41..104c7a50 100644 --- a/test/HydratorPluginManagerFactoryTest.php +++ b/test/HydratorPluginManagerFactoryTest.php @@ -10,6 +10,7 @@ use Laminas\Hydrator\ReflectionHydrator; use LaminasTest\Hydrator\TestAsset\InMemoryContainer; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; #[CoversClass(HydratorPluginManagerFactory::class)] @@ -32,8 +33,7 @@ public function testConfiguresHydratorServicesWhenFound(): void 'test' => ReflectionHydrator::class, ], 'factories' => [ - /** @psalm-return MockObject&HydratorInterface */ - 'test-too' => static fn(): HydratorInterface => $hydrator, + 'test-too' => static fn(): MockObject&HydratorInterface => $hydrator, ], ], ]; diff --git a/test/StandaloneHydratorPluginManagerFactoryTest.php b/test/StandaloneHydratorPluginManagerFactoryTest.php index 8df31086..64f13ce8 100644 --- a/test/StandaloneHydratorPluginManagerFactoryTest.php +++ b/test/StandaloneHydratorPluginManagerFactoryTest.php @@ -25,8 +25,7 @@ class StandaloneHydratorPluginManagerFactoryTest extends TestCase private const MESSAGE_DEFAULT_SERVICES = 'Missing the service %s'; private StandaloneHydratorPluginManagerFactory $factory; - /** @var ContainerInterface&MockObject */ - private MockObject $container; + private ContainerInterface&MockObject $container; protected function setUp(): void { diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index f51dcd9f..b8536591 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -215,10 +215,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void self::assertEquals($expected, $strategy->hydrate($value)); } - /** - * @return HydratorInterface&MockObject - */ - private function createHydratorMock(): MockObject + private function createHydratorMock(): HydratorInterface&MockObject { return $this->createMock(HydratorInterface::class); } diff --git a/test/Strategy/HydratorStrategyTest.php b/test/Strategy/HydratorStrategyTest.php index e64512a7..da721eb7 100644 --- a/test/Strategy/HydratorStrategyTest.php +++ b/test/Strategy/HydratorStrategyTest.php @@ -264,10 +264,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void self::assertEquals($hydration($value), $strategy->hydrate($value)); } - /** - * @return MockObject&HydratorInterface - */ - private function createHydratorMock(): HydratorInterface + private function createHydratorMock(): MockObject&HydratorInterface { return $this->createMock(HydratorInterface::class); } From 331df2f3b1ee3a48d5994b99ec400fd1d7f3f436 Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 21:48:54 +0200 Subject: [PATCH 12/16] refactor: update filter type annotations Signed-off-by: mmalac --- psalm-baseline.xml | 4 ---- src/AbstractHydrator.php | 2 +- src/Filter/FilterEnabledInterface.php | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3dd03445..28b5688d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -430,10 +430,6 @@ - - - - diff --git a/src/AbstractHydrator.php b/src/AbstractHydrator.php index 214cbb16..77f57bab 100644 --- a/src/AbstractHydrator.php +++ b/src/AbstractHydrator.php @@ -183,7 +183,7 @@ public function getFilter(): FilterInterface * * * @param string $name Index in the composite - * @param callable(mixed): mixed|FilterInterface $filter + * @param (callable(string, ?object):bool)|FilterInterface $filter */ public function addFilter( string $name, diff --git a/src/Filter/FilterEnabledInterface.php b/src/Filter/FilterEnabledInterface.php index a2455038..ada69518 100644 --- a/src/Filter/FilterEnabledInterface.php +++ b/src/Filter/FilterEnabledInterface.php @@ -25,7 +25,7 @@ interface FilterEnabledInterface extends FilterProviderInterface * * * @param string $name Index in the composite - * @param callable(mixed): mixed|FilterInterface $filter + * @param (callable(string, ?object):bool)|FilterInterface $filter */ public function addFilter( string $name, From 307b765d80cf4817e14011471a4402824ba468f0 Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 22:06:21 +0200 Subject: [PATCH 13/16] refactor: enable rector phpunit code quality rule Signed-off-by: mmalac --- rector.php | 1 + .../AggregateHydratorFunctionalTest.php | 45 ++- test/Aggregate/AggregateHydratorTest.php | 10 +- test/ArraySerializableHydratorTest.php | 19 +- test/ClassMethodsHydratorTest.php | 4 +- test/DelegatingHydratorTest.php | 2 +- test/Filter/FilterCompositeTest.php | 83 ++-- test/Filter/MethodMatchFilterTest.php | 21 +- test/Filter/OptionalParametersFilterTest.php | 17 +- test/HydratorAwareTraitTest.php | 8 +- test/HydratorStrategyTest.php | 11 +- test/HydratorTest.php | 359 +++++++++--------- .../CompositeNamingStrategyTest.php | 12 +- .../IdentityNamingStrategyTest.php | 11 +- test/NamingStrategy/MapNamingStrategyTest.php | 22 +- .../CamelCaseToUnderscoreFilterTest.php | 175 +++++---- .../UnderscoreToCamelCaseFilterTest.php | 149 ++++---- .../UnderscoreNamingStrategyTest.php | 6 +- test/StandaloneHydratorPluginManagerTest.php | 12 +- test/Strategy/BackedEnumStrategyTest.php | 16 +- test/Strategy/BooleanStrategyTest.php | 8 +- test/Strategy/CollectionStrategyTest.php | 35 +- .../DateTimeFormatterStrategyTest.php | 81 ++-- ...DateTimeImmutableFormatterStrategyTest.php | 32 +- test/Strategy/ExplodeStrategyTest.php | 45 ++- test/Strategy/HydratorStrategyTest.php | 41 +- test/Strategy/NullableStrategyTest.php | 32 +- test/Strategy/ScalarTypeStrategyTest.php | 14 +- test/Strategy/SerializableStrategyTest.php | 4 +- 29 files changed, 615 insertions(+), 660 deletions(-) diff --git a/rector.php b/rector.php index 14a63755..c97f12b8 100644 --- a/rector.php +++ b/rector.php @@ -12,4 +12,5 @@ ]) ->withPreparedSets( typeDeclarations: true, + phpunitCodeQuality: true, ); diff --git a/test/Aggregate/AggregateHydratorFunctionalTest.php b/test/Aggregate/AggregateHydratorFunctionalTest.php index b71db64b..2a64ca93 100644 --- a/test/Aggregate/AggregateHydratorFunctionalTest.php +++ b/test/Aggregate/AggregateHydratorFunctionalTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator\Aggregate; use ArrayObject; +use Generator; use Laminas\Hydrator\Aggregate\AggregateHydrator; use Laminas\Hydrator\Aggregate\ExtractEvent; use Laminas\Hydrator\Aggregate\HydrateEvent; @@ -37,10 +38,10 @@ public function testEmptyAggregate(): void { $object = new ArrayObject(['zaphod' => 'beeblebrox']); - self::assertSame([], $this->hydrator->extract($object)); - self::assertSame($object, $this->hydrator->hydrate(['arthur' => 'dent'], $object)); + $this->assertSame([], $this->hydrator->extract($object)); + $this->assertSame($object, $this->hydrator->hydrate(['arthur' => 'dent'], $object)); - self::assertSame(['zaphod' => 'beeblebrox'], $object->getArrayCopy()); + $this->assertSame(['zaphod' => 'beeblebrox'], $object->getArrayCopy()); } /** @@ -53,7 +54,7 @@ public function testSingleHydratorExtraction(HydratorInterface $comparisonHydrat $this->hydrator->add($comparisonHydrator); - self::assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object)); + $this->assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object)); } /** @@ -72,10 +73,10 @@ public function testSingleHydratorHydration( $hydratedBlueprint = $comparisonHydrator->hydrate($data, $blueprint); $hydrated = $this->hydrator->hydrate($data, $object); - self::assertEquals($hydratedBlueprint, $hydrated); + $this->assertEquals($hydratedBlueprint, $hydrated); if ($hydratedBlueprint === $blueprint) { - self::assertSame($hydrated, $object); + $this->assertSame($hydrated, $object); } } @@ -91,10 +92,10 @@ public function testExtractWithMultipleHydrators(): void $extracted = $this->hydrator->extract($object); - self::assertArrayHasKey('maintainer', $extracted); - self::assertArrayHasKey('president', $extracted); - self::assertSame('Marvin', $extracted['maintainer']); - self::assertSame('Zaphod', $extracted['president']); + $this->assertArrayHasKey('maintainer', $extracted); + $this->assertArrayHasKey('president', $extracted); + $this->assertSame('Marvin', $extracted['maintainer']); + $this->assertSame('Zaphod', $extracted['president']); } /** @@ -107,16 +108,16 @@ public function testHydrateWithMultipleHydrators(): void $object = new AggregateObject(); - self::assertSame( + $this->assertSame( $object, $this->hydrator->hydrate(['maintainer' => 'Trillian', 'president' => '???'], $object) ); - self::assertArrayHasKey('maintainer', $object->arrayData); - self::assertArrayHasKey('president', $object->arrayData); - self::assertSame('Trillian', $object->arrayData['maintainer']); - self::assertSame('???', $object->arrayData['president']); - self::assertSame('Trillian', $object->maintainer); + $this->assertArrayHasKey('maintainer', $object->arrayData); + $this->assertArrayHasKey('president', $object->arrayData); + $this->assertSame('Trillian', $object->arrayData['maintainer']); + $this->assertSame('???', $object->arrayData['president']); + $this->assertSame('Trillian', $object->maintainer); } /** @@ -134,7 +135,7 @@ public function testStoppedPropagationInExtraction(): void $this->hydrator->add(new ArraySerializableHydrator()); $this->hydrator->getEventManager()->attach(ExtractEvent::EVENT_EXTRACT, $callback, 1000); - self::assertSame(['Ravenous Bugblatter Beast of Traal'], $this->hydrator->extract($object)); + $this->assertSame(['Ravenous Bugblatter Beast of Traal'], $this->hydrator->extract($object)); } /** @@ -153,18 +154,16 @@ public function testStoppedPropagationInHydration(): void $this->hydrator->add(new ArraySerializableHydrator()); $this->hydrator->getEventManager()->attach(HydrateEvent::EVENT_HYDRATE, $callback, 1000); - self::assertSame($swappedObject, $this->hydrator->hydrate(['president' => 'Zaphod'], $object)); + $this->assertSame($swappedObject, $this->hydrator->hydrate(['president' => 'Zaphod'], $object)); } /** * Data provider method * - * @return list + * @return Generator */ - public static function getHydratorSet(): array + public static function getHydratorSet(): Generator { - return [ - [new ArraySerializableHydrator(), new ArrayObject(['zaphod' => 'beeblebrox']), ['arthur' => 'dent']], - ]; + yield [new ArraySerializableHydrator(), new ArrayObject(['zaphod' => 'beeblebrox']), ['arthur' => 'dent']]; } } diff --git a/test/Aggregate/AggregateHydratorTest.php b/test/Aggregate/AggregateHydratorTest.php index 07d39669..d8b52716 100644 --- a/test/Aggregate/AggregateHydratorTest.php +++ b/test/Aggregate/AggregateHydratorTest.php @@ -33,12 +33,12 @@ public function testAdd(): void $attached = $this->createMock(HydratorInterface::class); $this->eventManager - ->expects(self::exactly(2)) + ->expects($this->exactly(2)) ->method('attach') ->with( self::callback(function (mixed $event): bool { - self::assertIsString($event); - self::assertContains($event, [ + $this->assertIsString($event); + $this->assertContains($event, [ HydrateEvent::EVENT_HYDRATE, ExtractEvent::EVENT_EXTRACT, ]); @@ -46,12 +46,12 @@ public function testAdd(): void return true; }), self::callback(function (mixed $listener): bool { - self::assertIsCallable($listener); + $this->assertIsCallable($listener); return true; }), self::callback(function (mixed $priority): bool { - self::assertSame(123, $priority); + $this->assertSame(123, $priority); return true; }), diff --git a/test/ArraySerializableHydratorTest.php b/test/ArraySerializableHydratorTest.php index f6cdeb25..03ed7e1a 100644 --- a/test/ArraySerializableHydratorTest.php +++ b/test/ArraySerializableHydratorTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator; use ArrayObject; +use Generator; use Laminas\Hydrator\ArraySerializableHydrator; use LaminasTest\Hydrator\TestAsset\ArraySerializable as ArraySerializableAsset; use LaminasTest\Hydrator\TestAsset\ArraySerializableNoGetArrayCopy; @@ -124,19 +125,17 @@ public function testWillReplaceArrayIfNoGetArrayCopy(): void } /** - * @return string[][][] - * @psalm-return array + * @return Generator<(int | string), array>> + * @psalm-return Generator */ - public static function arrayDataProvider(): array + public static function arrayDataProvider(): Generator { - // @codingStandardsIgnoreStart - return [ - // [ existing data, submitted data, expected ] - 'empty' => [['what-exists'], [], []], - 'replacement' => [['what-exists'], ['laminas-hydrator', 'laminas-stdlib'], ['laminas-hydrator', 'laminas-stdlib']], - 'partial' => [['what-exists'], ['what-exists', 'laminas-hydrator'], ['what-exists', 'laminas-hydrator']], - ]; // @codingStandardsIgnoreEnd + // @codingStandardsIgnoreStart + // [ existing data, submitted data, expected ] + yield 'empty' => [['what-exists'], [], []]; + yield 'replacement' => [['what-exists'], ['laminas-hydrator', 'laminas-stdlib'], ['laminas-hydrator', 'laminas-stdlib']]; + yield 'partial' => [['what-exists'], ['what-exists', 'laminas-hydrator'], ['what-exists', 'laminas-hydrator']]; } /** diff --git a/test/ClassMethodsHydratorTest.php b/test/ClassMethodsHydratorTest.php index c4b48b9a..44b95d00 100644 --- a/test/ClassMethodsHydratorTest.php +++ b/test/ClassMethodsHydratorTest.php @@ -87,7 +87,7 @@ public function testSetOptionsFromTraversable(): void ]); $this->hydrator->setOptions($options); - $this->assertSame(false, $this->hydrator->getUnderscoreSeparatedKeys()); + $this->assertFalse($this->hydrator->getUnderscoreSeparatedKeys()); } /** @@ -115,7 +115,7 @@ public function testExtractClassWithoutAnyMethod(): void $data = $this->hydrator->extract( new TestAsset\ClassWithoutAnyMethod() ); - self::assertSame([], $data); + $this->assertSame([], $data); } public function testCanExtractFromAnonymousClassMethods(): void diff --git a/test/DelegatingHydratorTest.php b/test/DelegatingHydratorTest.php index ffe0a41a..dcb71b2d 100644 --- a/test/DelegatingHydratorTest.php +++ b/test/DelegatingHydratorTest.php @@ -42,7 +42,7 @@ public function testExtract(): void ->with(ArrayObject::class) ->willReturn($hydrator); - $this->assertEquals(['foo' => 'bar'], $this->hydrator->extract($this->object)); + $this->assertSame(['foo' => 'bar'], $this->hydrator->extract($this->object)); } public function testHydrate(): void diff --git a/test/Filter/FilterCompositeTest.php b/test/Filter/FilterCompositeTest.php index d41dd535..b4acc9e6 100644 --- a/test/Filter/FilterCompositeTest.php +++ b/test/Filter/FilterCompositeTest.php @@ -32,58 +32,53 @@ public function testFilters(array $orFilters, array $andFilters): void } } - /** @return list */ - public static function validFiltersProvider(): array + /** @return Generator */ + public static function validFiltersProvider(): Generator { - return [ + yield [ + ['foo' => new HasFilter()], + ['bar' => new GetFilter()], + ]; + yield [ [ - ['foo' => new HasFilter()], - ['bar' => new GetFilter()], + 'foo1' => new HasFilter(), + 'foo2' => new IsFilter(), ], [ - [ - 'foo1' => new HasFilter(), - 'foo2' => new IsFilter(), - ], - [ - 'bar1' => new GetFilter(), - 'bar2' => new NumberOfParameterFilter(), - ], + 'bar1' => new GetFilter(), + 'bar2' => new NumberOfParameterFilter(), ], ]; } - /** @return list */ - public static function invalidFiltersProvider(): array + /** @return Generator */ + public static function invalidFiltersProvider(): Generator { $callback = static fn(): bool => true; - - return [ - [ - ['foo' => 'bar'], - [], - 'foo', - ], - [ - [], - ['bar' => 'foo'], - 'bar', - ], - [ - ['foo' => ''], - ['bar' => ''], - 'foo', - ], - [ - ['foo' => $callback], - ['bar' => ''], - 'bar', - ], - [ - ['foo' => ''], - ['bar' => $callback], - 'foo', - ], + yield [ + ['foo' => 'bar'], + [], + 'foo', + ]; + yield [ + [], + ['bar' => 'foo'], + 'bar', + ]; + yield [ + ['foo' => ''], + ['bar' => ''], + 'foo', + ]; + yield [ + ['foo' => $callback], + ['bar' => ''], + 'bar', + ]; + yield [ + ['foo' => ''], + ['bar' => $callback], + 'foo', ]; } @@ -103,7 +98,7 @@ public function testConstructWithInvalidFilter(array $orFilters, array $andFilte public function testNoFilters(): void { $filter = new FilterComposite(); - self::assertTrue($filter->filter('any_value')); + $this->assertTrue($filter->filter('any_value')); } /** @@ -214,6 +209,6 @@ public static function providerCompositionFiltering(): Generator public function testCompositionFiltering(array $orFilters, array $andFilters, bool $expected): void { $filter = new FilterComposite($orFilters, $andFilters); - self::assertSame($expected, $filter->filter('any_value')); + $this->assertSame($expected, $filter->filter('any_value')); } } diff --git a/test/Filter/MethodMatchFilterTest.php b/test/Filter/MethodMatchFilterTest.php index eb72e56e..b4f422e0 100644 --- a/test/Filter/MethodMatchFilterTest.php +++ b/test/Filter/MethodMatchFilterTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator\Filter; +use Iterator; use Laminas\Hydrator\Filter\MethodMatchFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -13,26 +14,24 @@ class MethodMatchFilterTest extends TestCase { /** - * @return (bool|string)[][] - * @psalm-return list + * @return Iterator<(int | string), array<(bool | string)>> + * @psalm-return Iterator */ - public static function providerFilter(): array + public static function providerFilter(): Iterator { - return [ - ['foo', true], - ['bar', false], - ['class::foo', true], - ['class::bar', false], - ]; + yield ['foo', true]; + yield ['bar', false]; + yield ['class::foo', true]; + yield ['class::bar', false]; } #[DataProvider('providerFilter')] public function testFilter(string $methodName, bool $expected): void { $testedInstance = new MethodMatchFilter('foo', false); - self::assertEquals($expected, $testedInstance->filter($methodName)); + $this->assertSame($expected, $testedInstance->filter($methodName)); $testedInstance = new MethodMatchFilter('foo', true); - self::assertEquals(! $expected, $testedInstance->filter($methodName)); + $this->assertSame(! $expected, $testedInstance->filter($methodName)); } } diff --git a/test/Filter/OptionalParametersFilterTest.php b/test/Filter/OptionalParametersFilterTest.php index ba2fa8d4..98037a03 100644 --- a/test/Filter/OptionalParametersFilterTest.php +++ b/test/Filter/OptionalParametersFilterTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator\Filter; use InvalidArgumentException; +use Iterator; use Laminas\Hydrator\Filter\OptionalParametersFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -53,20 +54,18 @@ public function testTriggersExceptionOnUnknownMethod(): void /** * Provides a list of methods to be checked against the filter * - * @psalm-return array */ - public static function methodProvider(): array + public static function methodProvider(): Iterator { - return [ - [self::class . '::methodWithoutParameters', true], - [self::class . '::methodWithSingleMandatoryParameter', false], - [self::class . '::methodWithSingleOptionalParameter', true], - [self::class . '::methodWithMultipleMandatoryParameters', false], - [self::class . '::methodWithMultipleOptionalParameters', true], - ]; + yield [self::class . '::methodWithoutParameters', true]; + yield [self::class . '::methodWithSingleMandatoryParameter', false]; + yield [self::class . '::methodWithSingleOptionalParameter', true]; + yield [self::class . '::methodWithMultipleMandatoryParameters', false]; + yield [self::class . '::methodWithMultipleOptionalParameters', true]; } /** diff --git a/test/HydratorAwareTraitTest.php b/test/HydratorAwareTraitTest.php index dba7b488..394c17e4 100644 --- a/test/HydratorAwareTraitTest.php +++ b/test/HydratorAwareTraitTest.php @@ -13,18 +13,18 @@ class HydratorAwareTraitTest extends TestCase public function testSetHydrator(): void { $object = new HydratorAwareTraitImplementor(); - self::assertNull($object->getHydrator()); + $this->assertNotInstanceOf(HydratorInterface::class, $object->getHydrator()); $hydrator = $this->createMock(HydratorInterface::class); $object->setHydrator($hydrator); - self::assertSame($hydrator, $object->getHydrator()); + $this->assertSame($hydrator, $object->getHydrator()); } public function testGetHydrator(): void { $object = new HydratorAwareTraitImplementor(); - self::assertNull($object->getHydrator()); + $this->assertNotInstanceOf(HydratorInterface::class, $object->getHydrator()); $hydrator = $this->createMock(HydratorInterface::class); $object->setHydrator($hydrator); - self::assertSame($hydrator, $object->getHydrator()); + $this->assertSame($hydrator, $object->getHydrator()); } } diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php index 9ea68ffa..14dd2a6e 100644 --- a/test/HydratorStrategyTest.php +++ b/test/HydratorStrategyTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator; +use Iterator; use Laminas\Hydrator\ClassMethodsHydrator; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\Strategy\StrategyInterface; @@ -125,17 +126,15 @@ public function testWhenUsingUnderscoreSeparatedKeysHydratorStrategyIsAlwaysCons } /** - * @psalm-return array */ - public static function underscoreHandlingDataProvider(): array + public static function underscoreHandlingDataProvider(): Iterator { - return [ - [true, 'foo_bar'], - [false, 'fooBar'], - ]; + yield [true, 'foo_bar']; + yield [false, 'fooBar']; } public function testContextAwarenessExtract(): void diff --git a/test/HydratorTest.php b/test/HydratorTest.php index 71c9838f..fb45ccbb 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator; +use Iterator; use Laminas\Hydrator\AbstractHydrator; use Laminas\Hydrator\ArraySerializableHydrator; use Laminas\Hydrator\ClassMethodsHydrator; @@ -58,61 +59,61 @@ protected function setUp(): void public function testInitiateValues(): void { - self::assertSame('1', $this->classMethodsCamelCase->getFooBar()); - self::assertSame('2', $this->classMethodsCamelCase->getFooBarBaz()); - self::assertSame(true, $this->classMethodsCamelCase->getIsFoo()); - self::assertSame(true, $this->classMethodsCamelCase->isBar()); - self::assertSame(true, $this->classMethodsCamelCase->getHasFoo()); - self::assertSame(true, $this->classMethodsCamelCase->hasBar()); - - self::assertSame('1', $this->classMethodsTitleCase->getFooBar()); - self::assertSame('2', $this->classMethodsTitleCase->getFooBarBaz()); - self::assertSame(true, $this->classMethodsTitleCase->getIsFoo()); - self::assertSame(true, $this->classMethodsTitleCase->getIsBar()); - self::assertSame(true, $this->classMethodsTitleCase->getHasFoo()); - self::assertSame(true, $this->classMethodsTitleCase->getHasBar()); - - self::assertSame('1', $this->classMethodsUnderscore->getFooBar()); - self::assertSame('2', $this->classMethodsUnderscore->getFooBarBaz()); - self::assertSame(true, $this->classMethodsUnderscore->getIsFoo()); - self::assertSame(true, $this->classMethodsUnderscore->isBar()); - self::assertSame(true, $this->classMethodsUnderscore->getHasFoo()); - self::assertSame(true, $this->classMethodsUnderscore->hasBar()); + $this->assertSame('1', $this->classMethodsCamelCase->getFooBar()); + $this->assertSame('2', $this->classMethodsCamelCase->getFooBarBaz()); + $this->assertTrue($this->classMethodsCamelCase->getIsFoo()); + $this->assertTrue($this->classMethodsCamelCase->isBar()); + $this->assertTrue($this->classMethodsCamelCase->getHasFoo()); + $this->assertTrue($this->classMethodsCamelCase->hasBar()); + + $this->assertSame('1', $this->classMethodsTitleCase->getFooBar()); + $this->assertSame('2', $this->classMethodsTitleCase->getFooBarBaz()); + $this->assertTrue($this->classMethodsTitleCase->getIsFoo()); + $this->assertTrue($this->classMethodsTitleCase->getIsBar()); + $this->assertTrue($this->classMethodsTitleCase->getHasFoo()); + $this->assertTrue($this->classMethodsTitleCase->getHasBar()); + + $this->assertSame('1', $this->classMethodsUnderscore->getFooBar()); + $this->assertSame('2', $this->classMethodsUnderscore->getFooBarBaz()); + $this->assertTrue($this->classMethodsUnderscore->getIsFoo()); + $this->assertTrue($this->classMethodsUnderscore->isBar()); + $this->assertTrue($this->classMethodsUnderscore->getHasFoo()); + $this->assertTrue($this->classMethodsUnderscore->hasBar()); } public function testHydratorReflection(): void { $hydrator = new ReflectionHydrator(); $datas = $hydrator->extract($this->reflection); - self::assertArrayHaskey('foo', $datas); - self::assertSame('1', $datas['foo']); - self::assertArrayHaskey('fooBar', $datas); - self::assertSame('2', $datas['fooBar']); - self::assertArrayHaskey('fooBarBaz', $datas); - self::assertSame('3', $datas['fooBarBaz']); + $this->assertArrayHaskey('foo', $datas); + $this->assertSame('1', $datas['foo']); + $this->assertArrayHaskey('fooBar', $datas); + $this->assertSame('2', $datas['fooBar']); + $this->assertArrayHaskey('fooBarBaz', $datas); + $this->assertSame('3', $datas['fooBarBaz']); $test = $hydrator->hydrate(['foo' => 'foo', 'fooBar' => 'bar', 'fooBarBaz' => 'baz'], $this->reflection); - self::assertSame('foo', $test->foo); - self::assertSame('bar', $test->getFooBar()); - self::assertSame('baz', $test->getFooBarBaz()); + $this->assertSame('foo', $test->foo); + $this->assertSame('bar', $test->getFooBar()); + $this->assertSame('baz', $test->getFooBarBaz()); } public function testHydratorClassMethodsCamelCase(): void { $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCase); - self::assertArrayHaskey('fooBar', $datas); - self::assertSame('1', $datas['fooBar']); - self::assertArrayHaskey('fooBarBaz', $datas); - self::assertArrayNotHasKey('foo_bar', $datas); - self::assertArrayHaskey('isFoo', $datas); - self::assertSame(true, $datas['isFoo']); - self::assertArrayHaskey('isBar', $datas); - self::assertSame(true, $datas['isBar']); - self::assertArrayHaskey('hasFoo', $datas); - self::assertSame(true, $datas['hasFoo']); - self::assertArrayHaskey('hasBar', $datas); - self::assertSame(true, $datas['hasBar']); + $this->assertArrayHaskey('fooBar', $datas); + $this->assertSame('1', $datas['fooBar']); + $this->assertArrayHaskey('fooBarBaz', $datas); + $this->assertArrayNotHasKey('foo_bar', $datas); + $this->assertArrayHaskey('isFoo', $datas); + $this->assertTrue($datas['isFoo']); + $this->assertArrayHaskey('isBar', $datas); + $this->assertTrue($datas['isBar']); + $this->assertArrayHaskey('hasFoo', $datas); + $this->assertTrue($datas['hasFoo']); + $this->assertArrayHaskey('hasBar', $datas); + $this->assertTrue($datas['hasBar']); $test = $hydrator->hydrate( [ 'fooBar' => 'foo', @@ -124,31 +125,31 @@ public function testHydratorClassMethodsCamelCase(): void ], $this->classMethodsCamelCase ); - self::assertSame($this->classMethodsCamelCase, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('bar', $test->getFooBarBaz()); - self::assertSame(false, $test->getIsFoo()); - self::assertSame(false, $test->isBar()); - self::assertSame(false, $test->getHasFoo()); - self::assertSame(false, $test->hasBar()); + $this->assertSame($this->classMethodsCamelCase, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('bar', $test->getFooBarBaz()); + $this->assertFalse($test->getIsFoo()); + $this->assertFalse($test->isBar()); + $this->assertFalse($test->getHasFoo()); + $this->assertFalse($test->hasBar()); } public function testHydratorClassMethodsTitleCase(): void { $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsTitleCase); - self::assertArrayHaskey('FooBar', $datas); - self::assertSame('1', $datas['FooBar']); - self::assertArrayHaskey('FooBarBaz', $datas); - self::assertArrayNotHasKey('foo_bar', $datas); - self::assertArrayHaskey('IsFoo', $datas); - self::assertSame(true, $datas['IsFoo']); - self::assertArrayHaskey('IsBar', $datas); - self::assertSame(true, $datas['IsBar']); - self::assertArrayHaskey('HasFoo', $datas); - self::assertSame(true, $datas['HasFoo']); - self::assertArrayHaskey('HasBar', $datas); - self::assertSame(true, $datas['HasBar']); + $this->assertArrayHaskey('FooBar', $datas); + $this->assertSame('1', $datas['FooBar']); + $this->assertArrayHaskey('FooBarBaz', $datas); + $this->assertArrayNotHasKey('foo_bar', $datas); + $this->assertArrayHaskey('IsFoo', $datas); + $this->assertTrue($datas['IsFoo']); + $this->assertArrayHaskey('IsBar', $datas); + $this->assertTrue($datas['IsBar']); + $this->assertArrayHaskey('HasFoo', $datas); + $this->assertTrue($datas['HasFoo']); + $this->assertArrayHaskey('HasBar', $datas); + $this->assertTrue($datas['HasBar']); $test = $hydrator->hydrate( [ 'FooBar' => 'foo', @@ -160,35 +161,35 @@ public function testHydratorClassMethodsTitleCase(): void ], $this->classMethodsTitleCase ); - self::assertSame($this->classMethodsTitleCase, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('bar', $test->getFooBarBaz()); - self::assertSame(false, $test->getIsFoo()); - self::assertSame(false, $test->getIsBar()); - self::assertSame(false, $test->getHasFoo()); - self::assertSame(false, $test->getHasBar()); + $this->assertSame($this->classMethodsTitleCase, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('bar', $test->getFooBarBaz()); + $this->assertFalse($test->getIsFoo()); + $this->assertFalse($test->getIsBar()); + $this->assertFalse($test->getHasFoo()); + $this->assertFalse($test->getHasBar()); } public function testHydratorClassMethodsUnderscore(): void { $hydrator = new ClassMethodsHydrator(true); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertArrayHaskey('foo_bar', $datas); - self::assertSame('1', $datas['foo_bar']); - self::assertArrayHaskey('foo_bar_baz', $datas); - self::assertArrayNotHasKey('fooBar', $datas); - self::assertArrayHaskey('is_foo', $datas); - self::assertArrayNotHasKey('isFoo', $datas); - self::assertSame(true, $datas['is_foo']); - self::assertArrayHaskey('is_bar', $datas); - self::assertArrayNotHasKey('isBar', $datas); - self::assertSame(true, $datas['is_bar']); - self::assertArrayHaskey('has_foo', $datas); - self::assertArrayNotHasKey('hasFoo', $datas); - self::assertSame(true, $datas['has_foo']); - self::assertArrayHaskey('has_bar', $datas); - self::assertArrayNotHasKey('hasBar', $datas); - self::assertSame(true, $datas['has_bar']); + $this->assertArrayHaskey('foo_bar', $datas); + $this->assertSame('1', $datas['foo_bar']); + $this->assertArrayHaskey('foo_bar_baz', $datas); + $this->assertArrayNotHasKey('fooBar', $datas); + $this->assertArrayHaskey('is_foo', $datas); + $this->assertArrayNotHasKey('isFoo', $datas); + $this->assertTrue($datas['is_foo']); + $this->assertArrayHaskey('is_bar', $datas); + $this->assertArrayNotHasKey('isBar', $datas); + $this->assertTrue($datas['is_bar']); + $this->assertArrayHaskey('has_foo', $datas); + $this->assertArrayNotHasKey('hasFoo', $datas); + $this->assertTrue($datas['has_foo']); + $this->assertArrayHaskey('has_bar', $datas); + $this->assertArrayNotHasKey('hasBar', $datas); + $this->assertTrue($datas['has_bar']); $test = $hydrator->hydrate( [ 'foo_bar' => 'foo', @@ -200,13 +201,13 @@ public function testHydratorClassMethodsUnderscore(): void ], $this->classMethodsUnderscore ); - self::assertSame($this->classMethodsUnderscore, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('bar', $test->getFooBarBaz()); - self::assertSame(false, $test->getIsFoo()); - self::assertSame(false, $test->isBar()); - self::assertSame(false, $test->getHasFoo()); - self::assertSame(false, $test->hasBar()); + $this->assertSame($this->classMethodsUnderscore, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('bar', $test->getFooBarBaz()); + $this->assertFalse($test->getIsFoo()); + $this->assertFalse($test->isBar()); + $this->assertFalse($test->getHasFoo()); + $this->assertFalse($test->hasBar()); } public function testHydratorClassMethodsUnderscoreWithUnderscoreUpperCasedHydrateDataKeys(): void @@ -224,23 +225,23 @@ public function testHydratorClassMethodsUnderscoreWithUnderscoreUpperCasedHydrat ], $this->classMethodsUnderscore ); - self::assertSame($this->classMethodsUnderscore, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('bar', $test->getFooBarBaz()); - self::assertSame(false, $test->getIsFoo()); - self::assertSame(false, $test->isBar()); - self::assertSame(false, $test->getHasFoo()); - self::assertSame(false, $test->hasBar()); + $this->assertSame($this->classMethodsUnderscore, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('bar', $test->getFooBarBaz()); + $this->assertFalse($test->getIsFoo()); + $this->assertFalse($test->isBar()); + $this->assertFalse($test->getHasFoo()); + $this->assertFalse($test->hasBar()); } public function testHydratorClassMethodsOptions(): void { $hydrator = new ClassMethodsHydrator(); - self::assertTrue($hydrator->getUnderscoreSeparatedKeys()); + $this->assertTrue($hydrator->getUnderscoreSeparatedKeys()); $hydrator->setOptions(['underscoreSeparatedKeys' => false]); - self::assertFalse($hydrator->getUnderscoreSeparatedKeys()); + $this->assertFalse($hydrator->getUnderscoreSeparatedKeys()); $hydrator->setUnderscoreSeparatedKeys(true); - self::assertTrue($hydrator->getUnderscoreSeparatedKeys()); + $this->assertTrue($hydrator->getUnderscoreSeparatedKeys()); } public function testHydratorClassMethodsIgnoresInvalidValues(): void @@ -252,21 +253,21 @@ public function testHydratorClassMethodsIgnoresInvalidValues(): void 'invalid' => 'value', ]; $test = $hydrator->hydrate($data, $this->classMethodsUnderscore); - self::assertSame($this->classMethodsUnderscore, $test); + $this->assertSame($this->classMethodsUnderscore, $test); } public function testHydratorClassMethodsDefaultBehaviorIsConvertUnderscoreToCamelCase(): void { $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertArrayHaskey('foo_bar', $datas); - self::assertSame('1', $datas['foo_bar']); - self::assertArrayHaskey('foo_bar_baz', $datas); - self::assertArrayNotHaskey('fooBar', $datas); + $this->assertArrayHaskey('foo_bar', $datas); + $this->assertSame('1', $datas['foo_bar']); + $this->assertArrayHaskey('foo_bar_baz', $datas); + $this->assertArrayNotHaskey('fooBar', $datas); $test = $hydrator->hydrate(['foo_bar' => 'foo', 'foo_bar_baz' => 'bar'], $this->classMethodsUnderscore); - self::assertSame($this->classMethodsUnderscore, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('bar', $test->getFooBarBaz()); + $this->assertSame($this->classMethodsUnderscore, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('bar', $test->getFooBarBaz()); } public function testRetrieveWildStrategyAndOther(): void @@ -275,9 +276,9 @@ public function testRetrieveWildStrategyAndOther(): void $hydrator->addStrategy('default', new DefaultStrategy()); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $default = $hydrator->getStrategy('default'); - self::assertInstanceOf(DefaultStrategy::class, $default); + $this->assertInstanceOf(DefaultStrategy::class, $default); $serializable = $hydrator->getStrategy('*'); - self::assertInstanceOf(SerializableStrategy::class, $serializable); + $this->assertInstanceOf(SerializableStrategy::class, $serializable); } public function testUseWildStrategyByDefault(): void @@ -285,25 +286,25 @@ public function testUseWildStrategyByDefault(): void $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertSame('1', $datas['foo_bar']); + $this->assertSame('1', $datas['foo_bar']); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertSame('s:1:"1";', $datas['foo_bar']); + $this->assertSame('s:1:"1";', $datas['foo_bar']); } public function testUseWildStrategyAndOther(): void { $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertSame('1', $datas['foo_bar']); + $this->assertSame('1', $datas['foo_bar']); $hydrator->addStrategy('foo_bar', new DefaultStrategy()); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $datas = $hydrator->extract($this->classMethodsUnderscore); - self::assertSame('1', $datas['foo_bar']); - self::assertSame('s:1:"2";', $datas['foo_bar_baz']); + $this->assertSame('1', $datas['foo_bar']); + $this->assertSame('s:1:"2";', $datas['foo_bar_baz']); } public function testHydratorClassMethodsCamelCaseWithSetterMissing(): void @@ -311,14 +312,14 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCaseMissing); - self::assertArrayHaskey('fooBar', $datas); - self::assertSame('1', $datas['fooBar']); - self::assertArrayHaskey('fooBarBaz', $datas); - self::assertArrayNotHaskey('foo_bar', $datas); + $this->assertArrayHaskey('fooBar', $datas); + $this->assertSame('1', $datas['fooBar']); + $this->assertArrayHaskey('fooBarBaz', $datas); + $this->assertArrayNotHaskey('foo_bar', $datas); $test = $hydrator->hydrate(['fooBar' => 'foo', 'fooBarBaz' => 1], $this->classMethodsCamelCaseMissing); - self::assertSame($this->classMethodsCamelCaseMissing, $test); - self::assertSame('foo', $test->getFooBar()); - self::assertSame('2', $test->getFooBarBaz()); + $this->assertSame($this->classMethodsCamelCaseMissing, $test); + $this->assertSame('foo', $test->getFooBar()); + $this->assertSame('2', $test->getFooBarBaz()); } public function testHydratorClassMethodsManipulateFilter(): void @@ -326,23 +327,23 @@ public function testHydratorClassMethodsManipulateFilter(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCase); - self::assertArrayHaskey('fooBar', $datas); - self::assertSame('1', $datas['fooBar']); - self::assertArrayHaskey('fooBarBaz', $datas); - self::assertArrayNotHasKey('foo_bar', $datas); - self::assertArrayHaskey('isFoo', $datas); - self::assertSame(true, $datas['isFoo']); - self::assertArrayHaskey('isBar', $datas); - self::assertSame(true, $datas['isBar']); - self::assertArrayHaskey('hasFoo', $datas); - self::assertSame(true, $datas['hasFoo']); - self::assertArrayHaskey('hasBar', $datas); - self::assertSame(true, $datas['hasBar']); + $this->assertArrayHaskey('fooBar', $datas); + $this->assertSame('1', $datas['fooBar']); + $this->assertArrayHaskey('fooBarBaz', $datas); + $this->assertArrayNotHasKey('foo_bar', $datas); + $this->assertArrayHaskey('isFoo', $datas); + $this->assertTrue($datas['isFoo']); + $this->assertArrayHaskey('isBar', $datas); + $this->assertTrue($datas['isBar']); + $this->assertArrayHaskey('hasFoo', $datas); + $this->assertTrue($datas['hasFoo']); + $this->assertArrayHaskey('hasBar', $datas); + $this->assertTrue($datas['hasBar']); $hydrator->removeFilter('has'); $datas = $hydrator->extract($this->classMethodsCamelCase); - self::assertArrayHaskey('hasFoo', $datas); //method is getHasFoo - self::assertArrayNotHaskey('hasBar', $datas); //method is hasBar + $this->assertArrayHaskey('hasFoo', $datas); //method is getHasFoo + $this->assertArrayNotHaskey('hasBar', $datas); //method is hasBar } public function testHydratorClassMethodsWithCustomFilter(): void @@ -364,7 +365,7 @@ static function ($property): bool { ); $datas = $hydrator->extract($this->classMethodsCamelCase); - self::assertArrayNotHaskey('hasFoo', $datas); + $this->assertArrayNotHaskey('hasFoo', $datas); } #[DataProvider('filterProvider')] @@ -372,15 +373,12 @@ public function testArraySerializableFilter( AbstractHydrator $hydrator, object $serializable ): void { - self::assertSame( - [ - 'foo' => 'bar', - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], - $hydrator->extract($serializable) - ); + $this->assertSame([ + 'foo' => 'bar', + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], $hydrator->extract($serializable)); $hydrator->addFilter('foo', static function ($property): bool { if ($property === 'foo') { @@ -389,14 +387,11 @@ public function testArraySerializableFilter( return true; }); - self::assertSame( - [ - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], - $hydrator->extract($serializable) - ); + $this->assertSame([ + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], $hydrator->extract($serializable)); $hydrator->addFilter('len', static function ($property): bool { if (strlen($property) !== 3) { @@ -405,38 +400,30 @@ public function testArraySerializableFilter( return true; }, FilterComposite::CONDITION_AND); - self::assertSame( - [ - 'bar' => 'foo', - 'quo' => 'blubb', - ], - $hydrator->extract($serializable) - ); + $this->assertSame([ + 'bar' => 'foo', + 'quo' => 'blubb', + ], $hydrator->extract($serializable)); $hydrator->removeFilter('len'); $hydrator->removeFilter('foo'); - self::assertSame( - [ - 'foo' => 'bar', - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], - $hydrator->extract($serializable) - ); + $this->assertSame([ + 'foo' => 'bar', + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], $hydrator->extract($serializable)); } /** - * @psalm-return list + * @psalm-return Iterator */ - public static function filterProvider(): array + public static function filterProvider(): Iterator { - return [ - [new ObjectPropertyHydrator(), new ObjectPropertyAsset()], - [new ArraySerializableHydrator(), new ArraySerializableAsset()], - [new ReflectionHydrator(), new ReflectionFilter()], - ]; + yield [new ObjectPropertyHydrator(), new ObjectPropertyAsset()]; + yield [new ArraySerializableHydrator(), new ArraySerializableAsset()]; + yield [new ReflectionHydrator(), new ReflectionFilter()]; } public function testHydratorClassMethodsWithInvalidNumberOfParameters(): void @@ -444,9 +431,9 @@ public function testHydratorClassMethodsWithInvalidNumberOfParameters(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsInvalidParameter); - self::assertTrue($datas['hasBar']); - self::assertSame('Bar', $datas['foo']); - self::assertFalse($datas['isBla']); + $this->assertTrue($datas['hasBar']); + $this->assertSame('Bar', $datas['foo']); + $this->assertFalse($datas['isBla']); } public function testObjectBasedFilters(): void @@ -454,9 +441,9 @@ public function testObjectBasedFilters(): void $hydrator = new ClassMethodsHydrator(false); $foo = new ClassMethodsFilterProviderInterface(); $data = $hydrator->extract($foo); - self::assertArrayNotHasKey('filter', $data); - self::assertSame('bar', $data['foo']); - self::assertSame('foo', $data['bar']); + $this->assertArrayNotHasKey('filter', $data); + $this->assertSame('bar', $data['foo']); + $this->assertSame('foo', $data['bar']); } public function testHydratorClassMethodsWithProtectedSetter(): void @@ -466,7 +453,7 @@ public function testHydratorClassMethodsWithProtectedSetter(): void $hydrator->hydrate(['foo' => 'bar', 'bar' => 'BAR'], $object); $data = $hydrator->extract($object); - self::assertSame('BAR', $data['bar']); + $this->assertSame('BAR', $data['bar']); } public function testHydratorClassMethodsWithMagicMethodSetter(): void @@ -476,7 +463,7 @@ public function testHydratorClassMethodsWithMagicMethodSetter(): void $hydrator->hydrate(['foo' => 'bar'], $object); $data = $hydrator->extract($object); - self::assertSame('bar', $data['foo']); + $this->assertSame('bar', $data['foo']); } public function testHydratorClassMethodsWithMagicMethodSetterAndMethodExistsCheck(): void @@ -486,6 +473,6 @@ public function testHydratorClassMethodsWithMagicMethodSetterAndMethodExistsChec $hydrator->hydrate(['foo' => 'bar'], $object); $data = $hydrator->extract($object); - self::assertNull($data['foo']); + $this->assertNull($data['foo']); } } diff --git a/test/NamingStrategy/CompositeNamingStrategyTest.php b/test/NamingStrategy/CompositeNamingStrategyTest.php index 0533a2ac..b2d9eaa1 100644 --- a/test/NamingStrategy/CompositeNamingStrategyTest.php +++ b/test/NamingStrategy/CompositeNamingStrategyTest.php @@ -19,8 +19,8 @@ public function testGetSameNameWhenNoNamingStrategyExistsForTheName(): void 'foo' => $this->createMock(NamingStrategyInterface::class), ]); - $this->assertEquals('bar', $compositeNamingStrategy->hydrate('bar')); - $this->assertEquals('bar', $compositeNamingStrategy->extract('bar')); + $this->assertSame('bar', $compositeNamingStrategy->hydrate('bar')); + $this->assertSame('bar', $compositeNamingStrategy->extract('bar')); } public function testUseDefaultNamingStrategy(): void @@ -40,8 +40,8 @@ public function testUseDefaultNamingStrategy(): void ['bar' => $this->createMock(NamingStrategyInterface::class)], $defaultNamingStrategy ); - $this->assertEquals('Foo', $compositeNamingStrategy->hydrate('foo')); - $this->assertEquals('foo', $compositeNamingStrategy->extract('Foo')); + $this->assertSame('Foo', $compositeNamingStrategy->hydrate('foo')); + $this->assertSame('foo', $compositeNamingStrategy->extract('Foo')); } public function testHydrate(): void @@ -52,7 +52,7 @@ public function testHydrate(): void ->with('foo') ->willReturn('FOO'); $compositeNamingStrategy = new CompositeNamingStrategy(['foo' => $fooNamingStrategy]); - $this->assertEquals('FOO', $compositeNamingStrategy->hydrate('foo')); + $this->assertSame('FOO', $compositeNamingStrategy->hydrate('foo')); } public function testExtract(): void @@ -63,6 +63,6 @@ public function testExtract(): void ->with('FOO') ->willReturn('foo'); $compositeNamingStrategy = new CompositeNamingStrategy(['FOO' => $fooNamingStrategy]); - $this->assertEquals('foo', $compositeNamingStrategy->extract('FOO')); + $this->assertSame('foo', $compositeNamingStrategy->extract('FOO')); } } diff --git a/test/NamingStrategy/IdentityNamingStrategyTest.php b/test/NamingStrategy/IdentityNamingStrategyTest.php index 72ad5bc3..98252f1b 100644 --- a/test/NamingStrategy/IdentityNamingStrategyTest.php +++ b/test/NamingStrategy/IdentityNamingStrategyTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator\NamingStrategy; +use Iterator; use Laminas\Hydrator\NamingStrategy\IdentityNamingStrategy; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -31,13 +32,11 @@ public function testExtract(string $name): void /** * Data provider * - * @return string[][] + * @return Iterator<(int | string), array> */ - public static function getTestedNames(): array + public static function getTestedNames(): Iterator { - return [ - 'foo' => ['foo'], - 'bar' => ['bar'], - ]; + yield 'foo' => ['foo']; + yield 'bar' => ['bar']; } } diff --git a/test/NamingStrategy/MapNamingStrategyTest.php b/test/NamingStrategy/MapNamingStrategyTest.php index 8f5b9a76..f1ec9ff5 100644 --- a/test/NamingStrategy/MapNamingStrategyTest.php +++ b/test/NamingStrategy/MapNamingStrategyTest.php @@ -26,14 +26,14 @@ public static function invalidMapValues(): Generator yield 'object' => [(object) ['foo' => 'bar']]; } - /** @psalm-return Generator}> */ + /** @psalm-return Generator */ public static function invalidKeyArrays(): Generator { yield 'int' => [ - 'invalidKeyArray' => [1 => 'foo'], + [1 => 'foo'], ]; yield 'emtpy-string' => [ - 'invalidKeyArray' => ['' => 'foo'], + ['' => 'foo'], ]; } @@ -84,43 +84,43 @@ public function testHydrationMapConstructorRaisesExceptionWhenFlippingExtraction public function testExtractReturnsVerbatimWhenEmptyExtractionMapProvided(): void { $strategy = MapNamingStrategy::createFromExtractionMap([]); - self::assertEquals('some_stuff', $strategy->extract('some_stuff')); + $this->assertSame('some_stuff', $strategy->extract('some_stuff')); } public function testHydrateReturnsVerbatimWhenEmptyHydrationMapProvided(): void { $strategy = MapNamingStrategy::createFromHydrationMap([]); - self::assertEquals('some_stuff', $strategy->hydrate('some_stuff')); + $this->assertSame('some_stuff', $strategy->hydrate('some_stuff')); } public function testExtractUsesProvidedExtractionMap(): void { $strategy = MapNamingStrategy::createFromExtractionMap(['stuff3' => 'stuff4']); - self::assertEquals('stuff4', $strategy->extract('stuff3')); + $this->assertSame('stuff4', $strategy->extract('stuff3')); } public function testExtractUsesFlippedHydrationMapWhenOnlyHydrationMapProvided(): void { $strategy = MapNamingStrategy::createFromHydrationMap(['stuff3' => 'stuff4']); - self::assertEquals('stuff3', $strategy->extract('stuff4')); + $this->assertSame('stuff3', $strategy->extract('stuff4')); } public function testHydrateUsesProvidedHydrationMap(): void { $strategy = MapNamingStrategy::createFromHydrationMap(['stuff3' => 'stuff4']); - self::assertEquals('stuff4', $strategy->hydrate('stuff3')); + $this->assertSame('stuff4', $strategy->hydrate('stuff3')); } public function testHydrateUsesFlippedExtractionMapOnlyExtractionMapProvided(): void { $strategy = MapNamingStrategy::createFromExtractionMap(['foo' => 'bar']); - self::assertEquals('foo', $strategy->hydrate('bar')); + $this->assertSame('foo', $strategy->hydrate('bar')); } public function testHydrateAndExtractUseAsymmetricMapProvided(): void { $strategy = MapNamingStrategy::createFromAsymmetricMap(['foo' => 'bar'], ['bat' => 'baz']); - self::assertEquals('bar', $strategy->extract('foo')); - self::assertEquals('baz', $strategy->hydrate('bat')); + $this->assertSame('bar', $strategy->extract('foo')); + $this->assertSame('baz', $strategy->hydrate('bat')); } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php index d77e1d90..176ae958 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator\NamingStrategy\UnderscoreNamingStrategy; +use Iterator; use Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy\CamelCaseToUnderscoreFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -26,8 +27,8 @@ public function testFilterUnderscoresNonUnicodeStrings(string $string, string $e $filtered = $filter->filter($string); - $this->assertNotEquals($string, $filtered); - $this->assertEquals($expected, $filtered); + $this->assertNotSame($string, $filtered); + $this->assertSame($expected, $filtered); } #[DataProvider('unicodeProvider')] @@ -41,8 +42,8 @@ public function testFilterUnderscoresUnicodeStrings(string $string, string $expe $filtered = $filter->filter($string); - $this->assertNotEquals($string, $filtered); - $this->assertEquals($expected, $filtered); + $this->assertNotSame($string, $filtered); + $this->assertSame($expected, $filtered); } #[DataProvider('unicodeProviderWithoutMbStrings')] @@ -56,109 +57,103 @@ public function testFilterUnderscoresUnicodeStringsWithoutMbStrings(string $stri $filtered = $filter->filter($string); - $this->assertNotEquals($string, $filtered); - $this->assertEquals($expected, $filtered); + $this->assertNotSame($string, $filtered); + $this->assertSame($expected, $filtered); } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function nonUnicodeProvider(): array + public static function nonUnicodeProvider(): Iterator { - return [ - 'upcased first letter' => [ - 'Camel', - 'camel', - ], - 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ], - 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ], - 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ], - 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ], - 'lowercased alphanumeric' => [ - 'bfd7b82e9cfceaa82704d1c1Foo', - 'bfd7b82e9cfceaa82704d1c1_foo', - ], + yield 'upcased first letter' => [ + 'Camel', + 'camel', + ]; + yield 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ]; + yield 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ]; + yield 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ]; + yield 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ]; + yield 'lowercased alphanumeric' => [ + 'bfd7b82e9cfceaa82704d1c1Foo', + 'bfd7b82e9cfceaa82704d1c1_foo', ]; } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function unicodeProvider(): array + public static function unicodeProvider(): Iterator { - return [ - 'upcased first letter' => [ - 'Camel', - 'camel', - ], - 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ], - 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ], - 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ], - 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ], - 'unicode' => [ - 'testŠuma', - 'test_šuma', - ], + yield 'upcased first letter' => [ + 'Camel', + 'camel', + ]; + yield 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ]; + yield 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ]; + yield 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ]; + yield 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ]; + yield 'unicode' => [ + 'testŠuma', + 'test_šuma', ]; } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function unicodeProviderWithoutMbStrings(): array + public static function unicodeProviderWithoutMbStrings(): Iterator { - return [ - 'upcased first letter' => [ - 'Camel', - 'camel', - ], - 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ], - 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ], - 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ], - 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ], - 'unicode uppercase character' => [ - 'testŠuma', - 'test_Šuma', - ], + yield 'upcased first letter' => [ + 'Camel', + 'camel', + ]; + yield 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ]; + yield 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ]; + yield 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ]; + yield 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ]; + yield 'unicode uppercase character' => [ + 'testŠuma', + 'test_Šuma', ]; } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php index dbc5f1f1..af686e63 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator\NamingStrategy\UnderscoreNamingStrategy; +use Iterator; use Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy\UnderscoreToCamelCaseFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -26,33 +27,31 @@ public function testFilterCamelCasesNonUnicodeStrings(string $string, string $ex $filtered = $filter->filter($string); - $this->assertNotEquals($string, $filtered); - $this->assertEquals($expected, $filtered); + $this->assertNotSame($string, $filtered); + $this->assertSame($expected, $filtered); } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function nonUnicodeProvider(): array + public static function nonUnicodeProvider(): Iterator { - return [ - 'one word' => [ - 'Studly', - 'studly', - ], - 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ], - 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ], - 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', - ], + yield 'one word' => [ + 'Studly', + 'studly', + ]; + yield 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ]; + yield 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ]; + yield 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', ]; } @@ -66,41 +65,39 @@ public function testFilterCamelCasesUnicodeStrings(string $string, string $expec $filter = new UnderscoreToCamelCaseFilter(); $filtered = $filter->filter($string); - $this->assertNotEquals($string, $filtered); - $this->assertEquals($expected, $filtered); + $this->assertNotSame($string, $filtered); + $this->assertSame($expected, $filtered); } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function unicodeProvider(): array + public static function unicodeProvider(): Iterator { - return [ - 'uppercase first letter' => [ - 'Camel', - 'camel', - ], - 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ], - 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ], - 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', - ], - 'unicode character' => [ - 'test_Šuma', - 'testŠuma', - ], - 'unicode character [Laminas-10517]' => [ - 'test_šuma', - 'testŠuma', - ], + yield 'uppercase first letter' => [ + 'Camel', + 'camel', + ]; + yield 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ]; + yield 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ]; + yield 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', + ]; + yield 'unicode character' => [ + 'test_Šuma', + 'testŠuma', + ]; + yield 'unicode character [Laminas-10517]' => [ + 'test_šuma', + 'testŠuma', ]; } @@ -116,36 +113,34 @@ public function testFilterCamelCasesUnicodeStringsWithoutMbStrings( $property->setValue($filter, false); $filtered = $filter->filter($string); - $this->assertEquals($expected, $filtered); + $this->assertSame($expected, $filtered); } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function unicodeWithoutMbStringsProvider(): array + public static function unicodeWithoutMbStringsProvider(): Iterator { - return [ - 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ], - 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ], - 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', - ], - 'uppercase unicode character' => [ - 'test_Šuma', - 'testŠuma', - ], - 'lowercase unicode character' => [ - 'test_šuma', - 'test_šuma', - ], + yield 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ]; + yield 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ]; + yield 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', + ]; + yield 'uppercase unicode character' => [ + 'test_Šuma', + 'testŠuma', + ]; + yield 'lowercase unicode character' => [ + 'test_šuma', + 'test_šuma', ]; } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategyTest.php b/test/NamingStrategy/UnderscoreNamingStrategyTest.php index a698bd3c..5bf0144c 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategyTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategyTest.php @@ -15,13 +15,13 @@ class UnderscoreNamingStrategyTest extends TestCase public function testNameHydratesToCamelCase(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertEquals('fooBarBaz', $strategy->hydrate('foo_bar_baz')); + $this->assertSame('fooBarBaz', $strategy->hydrate('foo_bar_baz')); } public function testNameExtractsToUnderscore(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertEquals('foo_bar_baz', $strategy->extract('fooBarBaz')); + $this->assertSame('foo_bar_baz', $strategy->extract('fooBarBaz')); } #[Group('6422')] @@ -30,6 +30,6 @@ public function testNameHydratesToStudlyCaps(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertEquals('fooBarBaz', $strategy->hydrate('Foo_Bar_Baz')); + $this->assertSame('fooBarBaz', $strategy->hydrate('Foo_Bar_Baz')); } } diff --git a/test/StandaloneHydratorPluginManagerTest.php b/test/StandaloneHydratorPluginManagerTest.php index 8c822a50..29c43e0e 100644 --- a/test/StandaloneHydratorPluginManagerTest.php +++ b/test/StandaloneHydratorPluginManagerTest.php @@ -52,14 +52,14 @@ public function testInstantiationInitializesFactoriesForHydratorsWithoutConstruc { $factories = $this->reflectProperty($this->manager, 'factories'); - self::assertArrayHasKey($class, $factories); - self::assertInstanceOf(Closure::class, $factories[$class]); + $this->assertArrayHasKey($class, $factories); + $this->assertInstanceOf(Closure::class, $factories[$class]); } public function testDelegatingHydratorFactoryIsInitialized(): void { $factories = $this->reflectProperty($this->manager, 'factories'); - self::assertInstanceOf( + $this->assertInstanceOf( Hydrator\DelegatingHydratorFactory::class, $factories[Hydrator\DelegatingHydrator::class] ); @@ -67,7 +67,7 @@ public function testDelegatingHydratorFactoryIsInitialized(): void public function testHasReturnsFalseForUnknownNames(): void { - self::assertFalse($this->manager->has('unknown-service-name')); + $this->assertFalse($this->manager->has('unknown-service-name')); } /** @return Generator */ @@ -89,7 +89,7 @@ public static function knownServices(): Generator #[DataProvider('knownServices')] public function testHasReturnsTrueForKnownServices(string $service): void { - self::assertTrue($this->manager->has($service)); + $this->assertTrue($this->manager->has($service)); } public function testGetRaisesExceptionForUnknownService(): void @@ -106,6 +106,6 @@ public function testGetRaisesExceptionForUnknownService(): void public function testGetReturnsExpectedTypesForKnownServices(string $service, string $expectedType): void { $instance = $this->manager->get($service); - self::assertInstanceOf($expectedType, $instance); + $this->assertInstanceOf($expectedType, $instance); } } diff --git a/test/Strategy/BackedEnumStrategyTest.php b/test/Strategy/BackedEnumStrategyTest.php index 726df809..423f1084 100644 --- a/test/Strategy/BackedEnumStrategyTest.php +++ b/test/Strategy/BackedEnumStrategyTest.php @@ -17,7 +17,7 @@ class BackedEnumStrategyTest extends TestCase public function testExtractInvalidValueThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $strategy->extract(TestUnitEnum::One); } @@ -25,7 +25,7 @@ public function testExtractExtractsValue(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->extract(TestBackedEnum::One); - self::assertSame('one', $actual); + $this->assertSame('one', $actual); } public function testHydrateEnumReturnsEnum(): void @@ -33,22 +33,22 @@ public function testHydrateEnumReturnsEnum(): void $expected = TestBackedEnum::Two; $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->hydrate($expected, null); - self::assertSame(TestBackedEnum::Two, $actual); + $this->assertSame(TestBackedEnum::Two, $actual); } public function testHydrateNonScalarThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - self::expectException(InvalidArgumentException::class); - self::expectExceptionMessage("Value must be string or int; array provided"); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Value must be string or int; array provided"); $strategy->hydrate([], null); } public function testHydrateNonCaseThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - self::expectException(InvalidArgumentException::class); - self::expectExceptionMessage("Value 'three' is not a valid scalar value for " . TestBackedEnum::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Value 'three' is not a valid scalar value for " . TestBackedEnum::class); $strategy->hydrate('three', null); } @@ -56,6 +56,6 @@ public function testHydrateValueReturnsEnum(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->hydrate('two', null); - self::assertSame(TestBackedEnum::Two, $actual); + $this->assertSame(TestBackedEnum::Two, $actual); } } diff --git a/test/Strategy/BooleanStrategyTest.php b/test/Strategy/BooleanStrategyTest.php index bc314826..b4fe60ec 100644 --- a/test/Strategy/BooleanStrategyTest.php +++ b/test/Strategy/BooleanStrategyTest.php @@ -26,16 +26,16 @@ public function testConstructorWithValidString(): void public function testExtractString(): void { $hydrator = new BooleanStrategy('true', 'false'); - $this->assertEquals('true', $hydrator->extract(true)); - $this->assertEquals('false', $hydrator->extract(false)); + $this->assertSame('true', $hydrator->extract(true)); + $this->assertSame('false', $hydrator->extract(false)); } public function testExtractInteger(): void { $hydrator = new BooleanStrategy(1, 0); - $this->assertEquals(1, $hydrator->extract(true)); - $this->assertEquals(0, $hydrator->extract(false)); + $this->assertSame(1, $hydrator->extract(true)); + $this->assertSame(0, $hydrator->extract(false)); } public function testExtractThrowsExceptionOnUnknownValue(): void diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index b8536591..2eb78fb7 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator\Strategy; use Generator; +use Iterator; use Laminas\Hydrator\Exception; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\ReflectionHydrator; @@ -49,22 +50,20 @@ public function testConstructorRejectsInvalidObjectClassName( ); } - /** @return array, 2: string}> */ - public static function providerInvalidObjectClassName(): array + /** @return Iterator, string}> */ + public static function providerInvalidObjectClassName(): Iterator { - // @codingStandardsIgnoreStart - return [ - 'array' => [[], TypeError::class, 'type string'], - 'boolean-false' => [false, TypeError::class, 'type string'], - 'boolean-true' => [true, TypeError::class, 'type string'], - 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string'], - 'integer' => [mt_rand(), TypeError::class, 'type string'], - 'null' => [null, TypeError::class, 'type string'], - 'object' => [new stdClass(), TypeError::class, 'type string'], - 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string'], - 'string-non-existent-class' => ['FooBarBaz9000', Exception\InvalidArgumentException::class, 'class name needs to be the name of an existing class'], - ]; // @codingStandardsIgnoreEnd + // @codingStandardsIgnoreStart + yield 'array' => [[], TypeError::class, 'type string']; + yield 'boolean-false' => [false, TypeError::class, 'type string']; + yield 'boolean-true' => [true, TypeError::class, 'type string']; + yield 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string']; + yield 'integer' => [mt_rand(), TypeError::class, 'type string']; + yield 'null' => [null, TypeError::class, 'type string']; + yield 'object' => [new stdClass(), TypeError::class, 'type string']; + yield 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string']; + yield 'string-non-existent-class' => ['FooBarBaz9000', Exception\InvalidArgumentException::class, 'class name needs to be the name of an existing class']; } #[DataProvider('providerInvalidValueForExtraction')] @@ -125,7 +124,7 @@ public function testExtractUsesHydratorToExtractValues(): void $hydrator = $this->createHydratorMock(); $hydrator - ->expects(self::exactly(count($value))) + ->expects($this->exactly(count($value))) ->method('extract') ->willReturnCallback($extraction); @@ -136,7 +135,7 @@ public function testExtractUsesHydratorToExtractValues(): void $expected = array_map($extraction, $value); - self::assertSame($expected, $strategy->extract($value)); + $this->assertSame($expected, $strategy->extract($value)); } #[DataProvider('providerInvalidValueForHydration')] @@ -201,7 +200,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $hydrator = $this->createHydratorMock(); $hydrator - ->expects(self::exactly(count($value))) + ->expects($this->exactly(count($value))) ->method('hydrate') ->willReturnCallback($hydration); @@ -212,7 +211,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $expected = array_map($hydration, $value); - self::assertEquals($expected, $strategy->hydrate($value)); + $this->assertEquals($expected, $strategy->hydrate($value)); } private function createHydratorMock(): HydratorInterface&MockObject diff --git a/test/Strategy/DateTimeFormatterStrategyTest.php b/test/Strategy/DateTimeFormatterStrategyTest.php index bf2df6cb..0497415b 100644 --- a/test/Strategy/DateTimeFormatterStrategyTest.php +++ b/test/Strategy/DateTimeFormatterStrategyTest.php @@ -8,6 +8,7 @@ use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; +use Iterator; use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; @@ -21,25 +22,25 @@ class DateTimeFormatterStrategyTest extends TestCase public function testHydrate(): void { $strategy = new DateTimeFormatterStrategy('Y-m-d'); - self::assertEquals('2014-04-26', $strategy->hydrate('2014-04-26')->format('Y-m-d')); + $this->assertEquals('2014-04-26', $strategy->hydrate('2014-04-26')->format('Y-m-d')); $strategy = new DateTimeFormatterStrategy('Y-m-d', new DateTimeZone('Asia/Kathmandu')); $date = $strategy->hydrate('2014-04-26'); - self::assertEquals('Asia/Kathmandu', $date->getTimezone()->getName()); + $this->assertEquals('Asia/Kathmandu', $date->getTimezone()->getName()); } public function testExtract(): void { $strategy = new DateTimeFormatterStrategy('d/m/Y'); - self::assertEquals('26/04/2014', $strategy->extract(new DateTime('2014-04-26'))); + $this->assertEquals('26/04/2014', $strategy->extract(new DateTime('2014-04-26'))); } public function testGetNullWithInvalidDateOnHydration(): void { $strategy = new DateTimeFormatterStrategy('Y-m-d'); - self::assertEquals(null, $strategy->hydrate(null)); - self::assertEquals(null, $strategy->hydrate('')); + $this->assertEquals(null, $strategy->hydrate(null)); + $this->assertEquals(null, $strategy->hydrate('')); } public function testCanExtractIfNotDateTime(): void @@ -47,13 +48,13 @@ public function testCanExtractIfNotDateTime(): void $strategy = new DateTimeFormatterStrategy(); $date = $strategy->extract(new stdClass()); - self::assertInstanceOf(stdClass::class, $date); + $this->assertInstanceOf(stdClass::class, $date); } public function testCanHydrateWithInvalidDateTime(): void { $strategy = new DateTimeFormatterStrategy('d/m/Y'); - self::assertSame('foo bar baz', $strategy->hydrate('foo bar baz')); + $this->assertSame('foo bar baz', $strategy->hydrate('foo bar baz')); } public function testCanExtractAnyDateTimeInterface(): void @@ -64,7 +65,7 @@ public function testCanExtractAnyDateTimeInterface(): void $format = 'Y-m-d'; $dateMock - ->expects(self::once()) + ->expects($this->once()) ->method('format') ->with($format); @@ -73,7 +74,7 @@ public function testCanExtractAnyDateTimeInterface(): void ->getMock(); $dateImmutableMock - ->expects(self::once()) + ->expects($this->once()) ->method('format') ->with($format); @@ -89,8 +90,8 @@ public function testAcceptsCreateFromFormatSpecialCharacters(string $format, str $strategy = new DateTimeFormatterStrategy($format); $hydrated = $strategy->hydrate($expectedValue); - self::assertInstanceOf(DateTime::class, $hydrated); - self::assertEquals($expectedValue, $hydrated->format('Y-m-d')); + $this->assertInstanceOf(DateTime::class, $hydrated); + $this->assertSame($expectedValue, $hydrated->format('Y-m-d')); } #[DataProvider('formatsWithSpecialCharactersProvider')] @@ -100,7 +101,7 @@ public function testCanExtractWithCreateFromFormatSpecialCharacters(string $form $strategy = new DateTimeFormatterStrategy($format); $extracted = $strategy->extract($date); - self::assertEquals($expectedValue, $extracted); + $this->assertEquals($expectedValue, $extracted); } public function testCanExtractWithCreateFromFormatEscapedSpecialCharacters(): void @@ -108,20 +109,18 @@ public function testCanExtractWithCreateFromFormatEscapedSpecialCharacters(): vo $date = DateTime::createFromFormat('Y-m-d', '2018-02-05'); $strategy = new DateTimeFormatterStrategy('Y-m-d\\+'); $extracted = $strategy->extract($date); - self::assertEquals('2018-02-05+', $extracted); + $this->assertEquals('2018-02-05+', $extracted); } /** - * @return string[][] - * @psalm-return array + * @return Iterator<(int | string), array> + * @psalm-return Iterator */ - public static function formatsWithSpecialCharactersProvider(): array + public static function formatsWithSpecialCharactersProvider(): Iterator { - return [ - '!-prepended' => ['!Y-m-d', '2018-02-05'], - '|-appended' => ['Y-m-d|', '2018-02-05'], - '+-appended' => ['Y-m-d+', '2018-02-05'], - ]; + yield '!-prepended' => ['!Y-m-d', '2018-02-05']; + yield '|-appended' => ['Y-m-d|', '2018-02-05']; + yield '+-appended' => ['Y-m-d+', '2018-02-05']; } public function testCanHydrateWithDateTimeFallback(): void @@ -129,27 +128,25 @@ public function testCanHydrateWithDateTimeFallback(): void $strategy = new DateTimeFormatterStrategy('Y-m-d', null, true); $date = $strategy->hydrate('2018-09-06T12:10:30'); - self::assertInstanceOf(DateTimeInterface::class, $date); - self::assertSame('2018-09-06', $date->format('Y-m-d')); + $this->assertInstanceOf(DateTimeInterface::class, $date); + $this->assertSame('2018-09-06', $date->format('Y-m-d')); $strategy = new DateTimeFormatterStrategy('Y-m-d', new DateTimeZone('Europe/Prague'), true); $date = $strategy->hydrate('2018-09-06T12:10:30'); - self::assertInstanceOf(DateTimeInterface::class, $date); - self::assertSame('Europe/Prague', $date->getTimezone()->getName()); + $this->assertInstanceOf(DateTimeInterface::class, $date); + $this->assertSame('Europe/Prague', $date->getTimezone()->getName()); } - /** @return array> */ - public static function invalidValuesForHydration(): array + /** @return Iterator> */ + public static function invalidValuesForHydration(): Iterator { - return [ - 'zero' => [0], - 'int' => [1], - 'zero-float' => [0.0], - 'float' => [1.1], - 'array' => [['2018-11-20']], - 'object' => [(object) ['date' => '2018-11-20']], - ]; + yield 'zero' => [0]; + yield 'int' => [1]; + yield 'zero-float' => [0.0]; + yield 'float' => [1.1]; + yield 'array' => [['2018-11-20']]; + yield 'object' => [(object) ['date' => '2018-11-20']]; } #[DataProvider('invalidValuesForHydration')] @@ -162,14 +159,12 @@ public function testHydrateRaisesExceptionIfValueIsInvalid(mixed $value): void $strategy->hydrate($value); } - /** @return array> */ - public static function validUnHydratableValues(): array + /** @return Iterator> */ + public static function validUnHydratableValues(): Iterator { - return [ - 'empty string' => [''], - 'null' => [null], - 'date-time' => [new DateTimeImmutable('now')], - ]; + yield 'empty string' => ['']; + yield 'null' => [null]; + yield 'date-time' => [new DateTimeImmutable('now')]; } #[DataProvider('validUnHydratableValues')] @@ -177,6 +172,6 @@ public function testReturnsValueVerbatimUnderSpecificConditions(mixed $value): v { $strategy = new DateTimeFormatterStrategy('Y-m-d'); $hydrated = $strategy->hydrate($value); - self::assertSame($value, $hydrated); + $this->assertSame($value, $hydrated); } } diff --git a/test/Strategy/DateTimeImmutableFormatterStrategyTest.php b/test/Strategy/DateTimeImmutableFormatterStrategyTest.php index 6a31b386..66027ea0 100644 --- a/test/Strategy/DateTimeImmutableFormatterStrategyTest.php +++ b/test/Strategy/DateTimeImmutableFormatterStrategyTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator\Strategy; use DateTimeImmutable; +use Iterator; use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy; use PHPUnit\Framework\Attributes\CoversClass; @@ -25,47 +26,36 @@ protected function setUp(): void public function testExtraction(): void { - self::assertEquals( - '2020-05-25', - $this->strategy->extract(new DateTimeImmutable('2020-05-25')) - ); + $this->assertEquals('2020-05-25', $this->strategy->extract(new DateTimeImmutable('2020-05-25'))); } public function testHydrationWithDateTimeImmutableObjectShouldReturnSame(): void { $dateTime = new DateTimeImmutable('2020-05-25'); - self::assertEquals($dateTime, $this->strategy->hydrate($dateTime)); + $this->assertEquals($dateTime, $this->strategy->hydrate($dateTime)); } public function testHydrationShouldReturnImmutableDateTimeObject(): void { - self::assertInstanceOf( - DateTimeImmutable::class, - $this->strategy->hydrate('2020-05-25') - ); + $this->assertInstanceOf(DateTimeImmutable::class, $this->strategy->hydrate('2020-05-25')); } public function testHydrationShouldReturnDateTimeObjectWithSameValue(): void { - self::assertSame( - '2020-05-25', - $this->strategy->hydrate('2020-05-25')->format('Y-m-d') - ); + $this->assertSame('2020-05-25', $this->strategy->hydrate('2020-05-25')->format('Y-m-d')); } #[DataProvider('dataProviderForInvalidDateValues')] public function testHydrationShouldReturnInvalidDateValuesAsIs(string|null $value): void { - self::assertSame($value, $this->strategy->hydrate($value)); + $this->assertSame($value, $this->strategy->hydrate($value)); } - /** @return array */ - public static function dataProviderForInvalidDateValues(): array + /** @return Iterator */ + public static function dataProviderForInvalidDateValues(): Iterator { - return [ - 'null' => [null], - 'empty-string' => [''], - 'foo' => ['foo'], - ]; + yield 'null' => [null]; + yield 'empty-string' => ['']; + yield 'foo' => ['foo']; } } diff --git a/test/Strategy/ExplodeStrategyTest.php b/test/Strategy/ExplodeStrategyTest.php index c0e3fd5a..4563c066 100644 --- a/test/Strategy/ExplodeStrategyTest.php +++ b/test/Strategy/ExplodeStrategyTest.php @@ -4,6 +4,7 @@ namespace LaminasTest\Hydrator\Strategy; +use Iterator; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; use Laminas\Hydrator\Strategy\ExplodeStrategy; use PHPUnit\Framework\Attributes\CoversClass; @@ -26,9 +27,9 @@ public function testExtract(mixed $expected, string $delimiter, array $extractVa $strategy = new ExplodeStrategy($delimiter); if (is_numeric($expected)) { - self::assertEquals($expected, $strategy->extract($extractValue)); + $this->assertEquals($expected, $strategy->extract($extractValue)); } else { - self::assertSame($expected, $strategy->extract($extractValue)); + $this->assertSame($expected, $strategy->extract($extractValue)); } } @@ -46,7 +47,7 @@ public function testGetEmptyArrayWhenHydratingNullValue(): void { $strategy = new ExplodeStrategy(); - self::assertSame([], $strategy->hydrate(null)); + $this->assertSame([], $strategy->hydrate(null)); } public function testGetExceptionWithEmptyDelimiter(): void @@ -60,10 +61,10 @@ public function testGetExceptionWithEmptyDelimiter(): void public function testHydrateWithExplodeLimit(): void { $strategy = new ExplodeStrategy('-', 2); - self::assertSame(['foo', 'bar-baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); + $this->assertSame(['foo', 'bar-baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); $strategy = new ExplodeStrategy('-', 3); - self::assertSame(['foo', 'bar', 'baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); + $this->assertSame(['foo', 'bar', 'baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); } public function testHydrateWithInvalidScalarType(): void @@ -114,29 +115,27 @@ public function testHydration(mixed $value, string $delimiter, array $expected): { $strategy = new ExplodeStrategy($delimiter); - self::assertSame($expected, $strategy->hydrate($value)); + $this->assertSame($expected, $strategy->hydrate($value)); } /** - * @return array}> + * @return Iterator}> */ - public static function getValidHydratedValues(): array + public static function getValidHydratedValues(): Iterator { - // @codingStandardsIgnoreStart - return [ - 'null-comma' => [null, ',', []], - 'empty-comma' => ['', ',', ['']], - 'string without delimiter-comma' => ['foo', ',', ['foo']], - 'string with delimiter-comma' => ['foo,bar', ',', ['foo', 'bar']], - 'string with delimiter-period' => ['foo.bar', '.', ['foo', 'bar']], - 'string with mismatched delimiter-comma' => ['foo.bar', ',', ['foo.bar']], - 'integer-comma' => [123, ',', ['123']], - 'integer-numeric delimiter' => [123, '2', ['1', '3']], - 'integer with mismatched delimiter-comma' => [123.456, ',', ['123.456']], - 'float-period' => [123.456, '.', ['123', '456']], - 'string containing null-comma' => ['foo,bar,dev,null', ',', ['foo', 'bar', 'dev', 'null']], - 'string containing null-semicolon' => ['foo;bar;dev;null', ';', ['foo', 'bar', 'dev', 'null']], - ]; // @codingStandardsIgnoreEnd + // @codingStandardsIgnoreStart + yield 'null-comma' => [null, ',', []]; + yield 'empty-comma' => ['', ',', ['']]; + yield 'string without delimiter-comma' => ['foo', ',', ['foo']]; + yield 'string with delimiter-comma' => ['foo,bar', ',', ['foo', 'bar']]; + yield 'string with delimiter-period' => ['foo.bar', '.', ['foo', 'bar']]; + yield 'string with mismatched delimiter-comma' => ['foo.bar', ',', ['foo.bar']]; + yield 'integer-comma' => [123, ',', ['123']]; + yield 'integer-numeric delimiter' => [123, '2', ['1', '3']]; + yield 'integer with mismatched delimiter-comma' => [123.456, ',', ['123.456']]; + yield 'float-period' => [123.456, '.', ['123', '456']]; + yield 'string containing null-comma' => ['foo,bar,dev,null', ',', ['foo', 'bar', 'dev', 'null']]; + yield 'string containing null-semicolon' => ['foo;bar;dev;null', ';', ['foo', 'bar', 'dev', 'null']]; } } diff --git a/test/Strategy/HydratorStrategyTest.php b/test/Strategy/HydratorStrategyTest.php index da721eb7..7ff4ed17 100644 --- a/test/Strategy/HydratorStrategyTest.php +++ b/test/Strategy/HydratorStrategyTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\Hydrator\Strategy; use Generator; +use Iterator; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\ReflectionHydrator; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; @@ -47,23 +48,21 @@ public function testConstructorRejectsInvalidObjectClassName( ); } - /** @return non-empty-array, string}> */ - public static function providerInvalidObjectClassName(): array + /** @return Iterator, string}> */ + public static function providerInvalidObjectClassName(): Iterator { - return [ - 'array' => [[], TypeError::class, 'type string'], - 'boolean-false' => [false, TypeError::class, 'type string'], - 'boolean-true' => [true, TypeError::class, 'type string'], - 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string'], - 'integer' => [mt_rand(), TypeError::class, 'type string'], - 'null' => [null, TypeError::class, 'type string'], - 'object' => [new stdClass(), TypeError::class, 'type string'], - 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string'], - 'string-non-existent-class' => [ - 'FooBarBaz9000', - InvalidArgumentException::class, - 'class name needs to be the name of an existing class', - ], + yield 'array' => [[], TypeError::class, 'type string']; + yield 'boolean-false' => [false, TypeError::class, 'type string']; + yield 'boolean-true' => [true, TypeError::class, 'type string']; + yield 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string']; + yield 'integer' => [mt_rand(), TypeError::class, 'type string']; + yield 'null' => [null, TypeError::class, 'type string']; + yield 'object' => [new stdClass(), TypeError::class, 'type string']; + yield 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string']; + yield 'string-non-existent-class' => [ + 'FooBarBaz9000', + InvalidArgumentException::class, + 'class name needs to be the name of an existing class', ]; } @@ -159,7 +158,7 @@ public function testExtractUsesHydratorToExtractValues(): void $hydrator = $this->createHydratorMock(); - $hydrator->expects(self::once()) + $hydrator->expects($this->once()) ->method('extract') ->willReturnCallback($extraction); @@ -168,7 +167,7 @@ public function testExtractUsesHydratorToExtractValues(): void TestAsset\User::class ); - self::assertSame($extraction($value), $strategy->extract($value)); + $this->assertSame($extraction($value), $strategy->extract($value)); } #[DataProvider('providerInvalidValueForHydration')] @@ -216,7 +215,7 @@ public function testHydrateShouldReturnEmptyOrSameObjects(mixed $value): void TestAsset\User::class ); - self::assertSame($value, $strategy->hydrate($value)); + $this->assertSame($value, $strategy->hydrate($value)); } /** @return Generator */ @@ -252,7 +251,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $hydrator = $this->createHydratorMock(); - $hydrator->expects(self::exactly(count($value))) + $hydrator->expects($this->exactly(count($value))) ->method('hydrate') ->willReturnCallback($hydration); @@ -261,7 +260,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void TestAsset\User::class ); - self::assertEquals($hydration($value), $strategy->hydrate($value)); + $this->assertEquals($hydration($value), $strategy->hydrate($value)); } private function createHydratorMock(): MockObject&HydratorInterface diff --git a/test/Strategy/NullableStrategyTest.php b/test/Strategy/NullableStrategyTest.php index 608632a1..c18851d9 100644 --- a/test/Strategy/NullableStrategyTest.php +++ b/test/Strategy/NullableStrategyTest.php @@ -15,89 +15,89 @@ class NullableStrategyTest extends TestCase public function testExtractNonNullAndNonEmptyValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::once()) + $strategy->expects($this->once()) ->method('extract') ->with('original value') ->willReturn('extracted value'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertEquals('extracted value', $nullableStrategy->extract('original value')); + $this->assertEquals('extracted value', $nullableStrategy->extract('original value')); } public function testExtractNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::never()) + $strategy->expects($this->never()) ->method('extract'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertNull($nullableStrategy->extract(null)); + $this->assertNull($nullableStrategy->extract(null)); } public function testExtractEmptyValueAsNull(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::never()) + $strategy->expects($this->never()) ->method('extract'); $nullableStrategy = new NullableStrategy($strategy, true); - self::assertNull($nullableStrategy->extract('')); + $this->assertNull($nullableStrategy->extract('')); } public function testExtractEmptyValueByHydrator(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::once()) + $strategy->expects($this->once()) ->method('extract') ->with('') ->willReturn('extracted empty value'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertEquals('extracted empty value', $nullableStrategy->extract('')); + $this->assertEquals('extracted empty value', $nullableStrategy->extract('')); } public function testHydrateNonNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::once()) + $strategy->expects($this->once()) ->method('hydrate') ->with('original value') ->willReturn('hydrated value'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertEquals('hydrated value', $nullableStrategy->hydrate('original value')); + $this->assertEquals('hydrated value', $nullableStrategy->hydrate('original value')); } public function testHydrateNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::never()) + $strategy->expects($this->never()) ->method('hydrate'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertNull($nullableStrategy->hydrate(null)); + $this->assertNull($nullableStrategy->hydrate(null)); } public function testHydrateEmptyValueAsNull(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::never()) + $strategy->expects($this->never()) ->method('hydrate'); $nullableStrategy = new NullableStrategy($strategy, true); - self::assertNull($nullableStrategy->hydrate('')); + $this->assertNull($nullableStrategy->hydrate('')); } public function testHydrateEmptyValueByHydrator(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects(self::once()) + $strategy->expects($this->once()) ->method('hydrate') ->with('') ->willReturn('hydrated empty value'); $nullableStrategy = new NullableStrategy($strategy, false); - self::assertEquals('hydrated empty value', $nullableStrategy->hydrate('')); + $this->assertEquals('hydrated empty value', $nullableStrategy->hydrate('')); } } diff --git a/test/Strategy/ScalarTypeStrategyTest.php b/test/Strategy/ScalarTypeStrategyTest.php index c33a10a9..ebf4c88b 100644 --- a/test/Strategy/ScalarTypeStrategyTest.php +++ b/test/Strategy/ScalarTypeStrategyTest.php @@ -8,6 +8,8 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +use const PHP_FLOAT_EPSILON; + #[CoversClass(ScalarTypeStrategy::class)] class ScalarTypeStrategyTest extends TestCase { @@ -15,7 +17,11 @@ public function testHydrate(): void { $this->assertSame(123, ScalarTypeStrategy::createToInt()->hydrate('123', null)); $this->assertNull(ScalarTypeStrategy::createToInt()->hydrate(null, null)); - $this->assertSame(123.99, ScalarTypeStrategy::createToFloat()->hydrate('123.99', null)); + $this->assertEqualsWithDelta( + 123.99, + ScalarTypeStrategy::createToFloat()->hydrate('123.99', null), + PHP_FLOAT_EPSILON + ); $this->assertTrue(ScalarTypeStrategy::createToBoolean()->hydrate(1, null)); $this->assertFalse(ScalarTypeStrategy::createToBoolean()->hydrate(0, null)); @@ -31,9 +37,9 @@ public function __toString(): string public function testExtract(): void { $this->assertSame(123, ScalarTypeStrategy::createToInt()->extract(123)); - $this->assertSame(123.99, ScalarTypeStrategy::createToFloat()->extract(123.99)); + $this->assertEqualsWithDelta(123.99, ScalarTypeStrategy::createToFloat()->extract(123.99), PHP_FLOAT_EPSILON); $this->assertSame('foo', ScalarTypeStrategy::createToString()->extract('foo')); - $this->assertSame(true, ScalarTypeStrategy::createToBoolean()->extract(true)); - $this->assertSame(false, ScalarTypeStrategy::createToBoolean()->extract(false)); + $this->assertTrue(ScalarTypeStrategy::createToBoolean()->extract(true)); + $this->assertFalse(ScalarTypeStrategy::createToBoolean()->extract(false)); } } diff --git a/test/Strategy/SerializableStrategyTest.php b/test/Strategy/SerializableStrategyTest.php index 9a69c40a..8970b497 100644 --- a/test/Strategy/SerializableStrategyTest.php +++ b/test/Strategy/SerializableStrategyTest.php @@ -17,7 +17,7 @@ public function testCanSerialize(): void $serializer = new PhpSerialize(); $serializerStrategy = new SerializableStrategy($serializer); $serialized = $serializerStrategy->extract('foo'); - $this->assertEquals($serialized, 's:3:"foo";'); + $this->assertSame('s:3:"foo";', $serialized); } public function testCanUnserialize(): void @@ -25,6 +25,6 @@ public function testCanUnserialize(): void $serializer = new PhpSerialize(); $serializerStrategy = new SerializableStrategy($serializer); $serialized = $serializerStrategy->hydrate('s:3:"foo";'); - $this->assertEquals($serialized, 'foo'); + $this->assertEquals('foo', $serialized); } } From d3def694ea886d266512c47e490327ab78bbbd11 Mon Sep 17 00:00:00 2001 From: mmalac Date: Tue, 22 Jul 2025 22:10:15 +0200 Subject: [PATCH 14/16] refactor: enable rector privatization rule Signed-off-by: mmalac --- rector.php | 1 + src/Aggregate/AggregateHydrator.php | 2 +- src/Aggregate/ExtractEvent.php | 4 ++-- src/Aggregate/HydrateEvent.php | 2 +- src/Strategy/ClosureStrategy.php | 4 ++-- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rector.php b/rector.php index c97f12b8..06497604 100644 --- a/rector.php +++ b/rector.php @@ -12,5 +12,6 @@ ]) ->withPreparedSets( typeDeclarations: true, + privatization: true, phpunitCodeQuality: true, ); diff --git a/src/Aggregate/AggregateHydrator.php b/src/Aggregate/AggregateHydrator.php index 6f1cbc20..fd315151 100644 --- a/src/Aggregate/AggregateHydrator.php +++ b/src/Aggregate/AggregateHydrator.php @@ -18,7 +18,7 @@ final class AggregateHydrator implements HydratorInterface, EventManagerAwareInt { public const DEFAULT_PRIORITY = 1; - protected ?EventManagerInterface $eventManager = null; + private ?EventManagerInterface $eventManager = null; /** * Attaches the provided hydrator to the list of hydrators to be used while hydrating/extracting data diff --git a/src/Aggregate/ExtractEvent.php b/src/Aggregate/ExtractEvent.php index 600e97c8..05e64040 100644 --- a/src/Aggregate/ExtractEvent.php +++ b/src/Aggregate/ExtractEvent.php @@ -25,10 +25,10 @@ final class ExtractEvent extends Event protected $name = self::EVENT_EXTRACT; /** @var mixed[] Data being extracted from the $extractionObject */ - protected array $extractedData = []; + private array $extractedData = []; /** @psalm-param TTarget $target */ - public function __construct(object $target, protected object $extractionObject) + public function __construct(object $target, private object $extractionObject) { parent::__construct(self::EVENT_EXTRACT, $target, []); } diff --git a/src/Aggregate/HydrateEvent.php b/src/Aggregate/HydrateEvent.php index ba9074ac..75516b2e 100644 --- a/src/Aggregate/HydrateEvent.php +++ b/src/Aggregate/HydrateEvent.php @@ -28,7 +28,7 @@ final class HydrateEvent extends Event * @psalm-param TTarget $target * @psalm-param T $hydratedObject */ - public function __construct(object $target, protected object $hydratedObject, protected array $hydrationData) + public function __construct(object $target, private object $hydratedObject, private array $hydrationData) { parent::__construct(self::EVENT_HYDRATE, $target, []); } diff --git a/src/Strategy/ClosureStrategy.php b/src/Strategy/ClosureStrategy.php index 5354c1d2..5860426a 100644 --- a/src/Strategy/ClosureStrategy.php +++ b/src/Strategy/ClosureStrategy.php @@ -17,7 +17,7 @@ final class ClosureStrategy implements StrategyInterface * * @var null|callable */ - protected $extractFunc; + private $extractFunc; /** * Function, used in hydrate method, default: @@ -30,7 +30,7 @@ final class ClosureStrategy implements StrategyInterface * * @var null|callable */ - protected $hydrateFunc; + private $hydrateFunc; /** * You can describe how your values will extract and hydrate, like this: From 4af6e518804dc9a77afcf2aa1c5e3bbb096f9d3f Mon Sep 17 00:00:00 2001 From: mmalac Date: Wed, 23 Jul 2025 21:25:11 +0200 Subject: [PATCH 15/16] Revert "refactor: enable rector phpunit code quality rule" This reverts commit 307b765d80cf4817e14011471a4402824ba468f0. # Conflicts: # rector.php Signed-off-by: mmalac --- psalm-baseline.xml | 3 - rector.php | 1 - .../AggregateHydratorFunctionalTest.php | 45 +-- test/Aggregate/AggregateHydratorTest.php | 10 +- test/ArraySerializableHydratorTest.php | 19 +- test/ClassMethodsHydratorTest.php | 4 +- test/DelegatingHydratorTest.php | 2 +- test/Filter/FilterCompositeTest.php | 83 ++-- test/Filter/MethodMatchFilterTest.php | 21 +- test/Filter/OptionalParametersFilterTest.php | 17 +- test/HydratorAwareTraitTest.php | 8 +- test/HydratorStrategyTest.php | 11 +- test/HydratorTest.php | 359 +++++++++--------- .../CompositeNamingStrategyTest.php | 12 +- .../IdentityNamingStrategyTest.php | 11 +- test/NamingStrategy/MapNamingStrategyTest.php | 22 +- .../CamelCaseToUnderscoreFilterTest.php | 175 ++++----- .../UnderscoreToCamelCaseFilterTest.php | 149 ++++---- .../UnderscoreNamingStrategyTest.php | 6 +- test/StandaloneHydratorPluginManagerTest.php | 12 +- test/Strategy/BackedEnumStrategyTest.php | 16 +- test/Strategy/BooleanStrategyTest.php | 8 +- test/Strategy/CollectionStrategyTest.php | 35 +- .../DateTimeFormatterStrategyTest.php | 81 ++-- ...DateTimeImmutableFormatterStrategyTest.php | 32 +- test/Strategy/ExplodeStrategyTest.php | 45 +-- test/Strategy/HydratorStrategyTest.php | 43 ++- test/Strategy/NullableStrategyTest.php | 32 +- test/Strategy/ScalarTypeStrategyTest.php | 14 +- test/Strategy/SerializableStrategyTest.php | 4 +- 30 files changed, 661 insertions(+), 619 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 28b5688d..bc647ed2 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -577,9 +577,6 @@ - - - diff --git a/rector.php b/rector.php index 06497604..4856bfd3 100644 --- a/rector.php +++ b/rector.php @@ -13,5 +13,4 @@ ->withPreparedSets( typeDeclarations: true, privatization: true, - phpunitCodeQuality: true, ); diff --git a/test/Aggregate/AggregateHydratorFunctionalTest.php b/test/Aggregate/AggregateHydratorFunctionalTest.php index 2a64ca93..b71db64b 100644 --- a/test/Aggregate/AggregateHydratorFunctionalTest.php +++ b/test/Aggregate/AggregateHydratorFunctionalTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator\Aggregate; use ArrayObject; -use Generator; use Laminas\Hydrator\Aggregate\AggregateHydrator; use Laminas\Hydrator\Aggregate\ExtractEvent; use Laminas\Hydrator\Aggregate\HydrateEvent; @@ -38,10 +37,10 @@ public function testEmptyAggregate(): void { $object = new ArrayObject(['zaphod' => 'beeblebrox']); - $this->assertSame([], $this->hydrator->extract($object)); - $this->assertSame($object, $this->hydrator->hydrate(['arthur' => 'dent'], $object)); + self::assertSame([], $this->hydrator->extract($object)); + self::assertSame($object, $this->hydrator->hydrate(['arthur' => 'dent'], $object)); - $this->assertSame(['zaphod' => 'beeblebrox'], $object->getArrayCopy()); + self::assertSame(['zaphod' => 'beeblebrox'], $object->getArrayCopy()); } /** @@ -54,7 +53,7 @@ public function testSingleHydratorExtraction(HydratorInterface $comparisonHydrat $this->hydrator->add($comparisonHydrator); - $this->assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object)); + self::assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object)); } /** @@ -73,10 +72,10 @@ public function testSingleHydratorHydration( $hydratedBlueprint = $comparisonHydrator->hydrate($data, $blueprint); $hydrated = $this->hydrator->hydrate($data, $object); - $this->assertEquals($hydratedBlueprint, $hydrated); + self::assertEquals($hydratedBlueprint, $hydrated); if ($hydratedBlueprint === $blueprint) { - $this->assertSame($hydrated, $object); + self::assertSame($hydrated, $object); } } @@ -92,10 +91,10 @@ public function testExtractWithMultipleHydrators(): void $extracted = $this->hydrator->extract($object); - $this->assertArrayHasKey('maintainer', $extracted); - $this->assertArrayHasKey('president', $extracted); - $this->assertSame('Marvin', $extracted['maintainer']); - $this->assertSame('Zaphod', $extracted['president']); + self::assertArrayHasKey('maintainer', $extracted); + self::assertArrayHasKey('president', $extracted); + self::assertSame('Marvin', $extracted['maintainer']); + self::assertSame('Zaphod', $extracted['president']); } /** @@ -108,16 +107,16 @@ public function testHydrateWithMultipleHydrators(): void $object = new AggregateObject(); - $this->assertSame( + self::assertSame( $object, $this->hydrator->hydrate(['maintainer' => 'Trillian', 'president' => '???'], $object) ); - $this->assertArrayHasKey('maintainer', $object->arrayData); - $this->assertArrayHasKey('president', $object->arrayData); - $this->assertSame('Trillian', $object->arrayData['maintainer']); - $this->assertSame('???', $object->arrayData['president']); - $this->assertSame('Trillian', $object->maintainer); + self::assertArrayHasKey('maintainer', $object->arrayData); + self::assertArrayHasKey('president', $object->arrayData); + self::assertSame('Trillian', $object->arrayData['maintainer']); + self::assertSame('???', $object->arrayData['president']); + self::assertSame('Trillian', $object->maintainer); } /** @@ -135,7 +134,7 @@ public function testStoppedPropagationInExtraction(): void $this->hydrator->add(new ArraySerializableHydrator()); $this->hydrator->getEventManager()->attach(ExtractEvent::EVENT_EXTRACT, $callback, 1000); - $this->assertSame(['Ravenous Bugblatter Beast of Traal'], $this->hydrator->extract($object)); + self::assertSame(['Ravenous Bugblatter Beast of Traal'], $this->hydrator->extract($object)); } /** @@ -154,16 +153,18 @@ public function testStoppedPropagationInHydration(): void $this->hydrator->add(new ArraySerializableHydrator()); $this->hydrator->getEventManager()->attach(HydrateEvent::EVENT_HYDRATE, $callback, 1000); - $this->assertSame($swappedObject, $this->hydrator->hydrate(['president' => 'Zaphod'], $object)); + self::assertSame($swappedObject, $this->hydrator->hydrate(['president' => 'Zaphod'], $object)); } /** * Data provider method * - * @return Generator + * @return list */ - public static function getHydratorSet(): Generator + public static function getHydratorSet(): array { - yield [new ArraySerializableHydrator(), new ArrayObject(['zaphod' => 'beeblebrox']), ['arthur' => 'dent']]; + return [ + [new ArraySerializableHydrator(), new ArrayObject(['zaphod' => 'beeblebrox']), ['arthur' => 'dent']], + ]; } } diff --git a/test/Aggregate/AggregateHydratorTest.php b/test/Aggregate/AggregateHydratorTest.php index d8b52716..07d39669 100644 --- a/test/Aggregate/AggregateHydratorTest.php +++ b/test/Aggregate/AggregateHydratorTest.php @@ -33,12 +33,12 @@ public function testAdd(): void $attached = $this->createMock(HydratorInterface::class); $this->eventManager - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('attach') ->with( self::callback(function (mixed $event): bool { - $this->assertIsString($event); - $this->assertContains($event, [ + self::assertIsString($event); + self::assertContains($event, [ HydrateEvent::EVENT_HYDRATE, ExtractEvent::EVENT_EXTRACT, ]); @@ -46,12 +46,12 @@ public function testAdd(): void return true; }), self::callback(function (mixed $listener): bool { - $this->assertIsCallable($listener); + self::assertIsCallable($listener); return true; }), self::callback(function (mixed $priority): bool { - $this->assertSame(123, $priority); + self::assertSame(123, $priority); return true; }), diff --git a/test/ArraySerializableHydratorTest.php b/test/ArraySerializableHydratorTest.php index 03ed7e1a..f6cdeb25 100644 --- a/test/ArraySerializableHydratorTest.php +++ b/test/ArraySerializableHydratorTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator; use ArrayObject; -use Generator; use Laminas\Hydrator\ArraySerializableHydrator; use LaminasTest\Hydrator\TestAsset\ArraySerializable as ArraySerializableAsset; use LaminasTest\Hydrator\TestAsset\ArraySerializableNoGetArrayCopy; @@ -125,17 +124,19 @@ public function testWillReplaceArrayIfNoGetArrayCopy(): void } /** - * @return Generator<(int | string), array>> - * @psalm-return Generator + * @return string[][][] + * @psalm-return array */ - public static function arrayDataProvider(): Generator + public static function arrayDataProvider(): array { - // @codingStandardsIgnoreEnd // @codingStandardsIgnoreStart - // [ existing data, submitted data, expected ] - yield 'empty' => [['what-exists'], [], []]; - yield 'replacement' => [['what-exists'], ['laminas-hydrator', 'laminas-stdlib'], ['laminas-hydrator', 'laminas-stdlib']]; - yield 'partial' => [['what-exists'], ['what-exists', 'laminas-hydrator'], ['what-exists', 'laminas-hydrator']]; + return [ + // [ existing data, submitted data, expected ] + 'empty' => [['what-exists'], [], []], + 'replacement' => [['what-exists'], ['laminas-hydrator', 'laminas-stdlib'], ['laminas-hydrator', 'laminas-stdlib']], + 'partial' => [['what-exists'], ['what-exists', 'laminas-hydrator'], ['what-exists', 'laminas-hydrator']], + ]; + // @codingStandardsIgnoreEnd } /** diff --git a/test/ClassMethodsHydratorTest.php b/test/ClassMethodsHydratorTest.php index 44b95d00..c4b48b9a 100644 --- a/test/ClassMethodsHydratorTest.php +++ b/test/ClassMethodsHydratorTest.php @@ -87,7 +87,7 @@ public function testSetOptionsFromTraversable(): void ]); $this->hydrator->setOptions($options); - $this->assertFalse($this->hydrator->getUnderscoreSeparatedKeys()); + $this->assertSame(false, $this->hydrator->getUnderscoreSeparatedKeys()); } /** @@ -115,7 +115,7 @@ public function testExtractClassWithoutAnyMethod(): void $data = $this->hydrator->extract( new TestAsset\ClassWithoutAnyMethod() ); - $this->assertSame([], $data); + self::assertSame([], $data); } public function testCanExtractFromAnonymousClassMethods(): void diff --git a/test/DelegatingHydratorTest.php b/test/DelegatingHydratorTest.php index dcb71b2d..ffe0a41a 100644 --- a/test/DelegatingHydratorTest.php +++ b/test/DelegatingHydratorTest.php @@ -42,7 +42,7 @@ public function testExtract(): void ->with(ArrayObject::class) ->willReturn($hydrator); - $this->assertSame(['foo' => 'bar'], $this->hydrator->extract($this->object)); + $this->assertEquals(['foo' => 'bar'], $this->hydrator->extract($this->object)); } public function testHydrate(): void diff --git a/test/Filter/FilterCompositeTest.php b/test/Filter/FilterCompositeTest.php index b4acc9e6..d41dd535 100644 --- a/test/Filter/FilterCompositeTest.php +++ b/test/Filter/FilterCompositeTest.php @@ -32,53 +32,58 @@ public function testFilters(array $orFilters, array $andFilters): void } } - /** @return Generator */ - public static function validFiltersProvider(): Generator + /** @return list */ + public static function validFiltersProvider(): array { - yield [ - ['foo' => new HasFilter()], - ['bar' => new GetFilter()], - ]; - yield [ + return [ [ - 'foo1' => new HasFilter(), - 'foo2' => new IsFilter(), + ['foo' => new HasFilter()], + ['bar' => new GetFilter()], ], [ - 'bar1' => new GetFilter(), - 'bar2' => new NumberOfParameterFilter(), + [ + 'foo1' => new HasFilter(), + 'foo2' => new IsFilter(), + ], + [ + 'bar1' => new GetFilter(), + 'bar2' => new NumberOfParameterFilter(), + ], ], ]; } - /** @return Generator */ - public static function invalidFiltersProvider(): Generator + /** @return list */ + public static function invalidFiltersProvider(): array { $callback = static fn(): bool => true; - yield [ - ['foo' => 'bar'], - [], - 'foo', - ]; - yield [ - [], - ['bar' => 'foo'], - 'bar', - ]; - yield [ - ['foo' => ''], - ['bar' => ''], - 'foo', - ]; - yield [ - ['foo' => $callback], - ['bar' => ''], - 'bar', - ]; - yield [ - ['foo' => ''], - ['bar' => $callback], - 'foo', + + return [ + [ + ['foo' => 'bar'], + [], + 'foo', + ], + [ + [], + ['bar' => 'foo'], + 'bar', + ], + [ + ['foo' => ''], + ['bar' => ''], + 'foo', + ], + [ + ['foo' => $callback], + ['bar' => ''], + 'bar', + ], + [ + ['foo' => ''], + ['bar' => $callback], + 'foo', + ], ]; } @@ -98,7 +103,7 @@ public function testConstructWithInvalidFilter(array $orFilters, array $andFilte public function testNoFilters(): void { $filter = new FilterComposite(); - $this->assertTrue($filter->filter('any_value')); + self::assertTrue($filter->filter('any_value')); } /** @@ -209,6 +214,6 @@ public static function providerCompositionFiltering(): Generator public function testCompositionFiltering(array $orFilters, array $andFilters, bool $expected): void { $filter = new FilterComposite($orFilters, $andFilters); - $this->assertSame($expected, $filter->filter('any_value')); + self::assertSame($expected, $filter->filter('any_value')); } } diff --git a/test/Filter/MethodMatchFilterTest.php b/test/Filter/MethodMatchFilterTest.php index b4f422e0..eb72e56e 100644 --- a/test/Filter/MethodMatchFilterTest.php +++ b/test/Filter/MethodMatchFilterTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator\Filter; -use Iterator; use Laminas\Hydrator\Filter\MethodMatchFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -14,24 +13,26 @@ class MethodMatchFilterTest extends TestCase { /** - * @return Iterator<(int | string), array<(bool | string)>> - * @psalm-return Iterator + * @return (bool|string)[][] + * @psalm-return list */ - public static function providerFilter(): Iterator + public static function providerFilter(): array { - yield ['foo', true]; - yield ['bar', false]; - yield ['class::foo', true]; - yield ['class::bar', false]; + return [ + ['foo', true], + ['bar', false], + ['class::foo', true], + ['class::bar', false], + ]; } #[DataProvider('providerFilter')] public function testFilter(string $methodName, bool $expected): void { $testedInstance = new MethodMatchFilter('foo', false); - $this->assertSame($expected, $testedInstance->filter($methodName)); + self::assertEquals($expected, $testedInstance->filter($methodName)); $testedInstance = new MethodMatchFilter('foo', true); - $this->assertSame(! $expected, $testedInstance->filter($methodName)); + self::assertEquals(! $expected, $testedInstance->filter($methodName)); } } diff --git a/test/Filter/OptionalParametersFilterTest.php b/test/Filter/OptionalParametersFilterTest.php index 98037a03..ba2fa8d4 100644 --- a/test/Filter/OptionalParametersFilterTest.php +++ b/test/Filter/OptionalParametersFilterTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator\Filter; use InvalidArgumentException; -use Iterator; use Laminas\Hydrator\Filter\OptionalParametersFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -54,18 +53,20 @@ public function testTriggersExceptionOnUnknownMethod(): void /** * Provides a list of methods to be checked against the filter * - * @psalm-return Iterator */ - public static function methodProvider(): Iterator + public static function methodProvider(): array { - yield [self::class . '::methodWithoutParameters', true]; - yield [self::class . '::methodWithSingleMandatoryParameter', false]; - yield [self::class . '::methodWithSingleOptionalParameter', true]; - yield [self::class . '::methodWithMultipleMandatoryParameters', false]; - yield [self::class . '::methodWithMultipleOptionalParameters', true]; + return [ + [self::class . '::methodWithoutParameters', true], + [self::class . '::methodWithSingleMandatoryParameter', false], + [self::class . '::methodWithSingleOptionalParameter', true], + [self::class . '::methodWithMultipleMandatoryParameters', false], + [self::class . '::methodWithMultipleOptionalParameters', true], + ]; } /** diff --git a/test/HydratorAwareTraitTest.php b/test/HydratorAwareTraitTest.php index 394c17e4..dba7b488 100644 --- a/test/HydratorAwareTraitTest.php +++ b/test/HydratorAwareTraitTest.php @@ -13,18 +13,18 @@ class HydratorAwareTraitTest extends TestCase public function testSetHydrator(): void { $object = new HydratorAwareTraitImplementor(); - $this->assertNotInstanceOf(HydratorInterface::class, $object->getHydrator()); + self::assertNull($object->getHydrator()); $hydrator = $this->createMock(HydratorInterface::class); $object->setHydrator($hydrator); - $this->assertSame($hydrator, $object->getHydrator()); + self::assertSame($hydrator, $object->getHydrator()); } public function testGetHydrator(): void { $object = new HydratorAwareTraitImplementor(); - $this->assertNotInstanceOf(HydratorInterface::class, $object->getHydrator()); + self::assertNull($object->getHydrator()); $hydrator = $this->createMock(HydratorInterface::class); $object->setHydrator($hydrator); - $this->assertSame($hydrator, $object->getHydrator()); + self::assertSame($hydrator, $object->getHydrator()); } } diff --git a/test/HydratorStrategyTest.php b/test/HydratorStrategyTest.php index 14dd2a6e..9ea68ffa 100644 --- a/test/HydratorStrategyTest.php +++ b/test/HydratorStrategyTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator; -use Iterator; use Laminas\Hydrator\ClassMethodsHydrator; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\Strategy\StrategyInterface; @@ -126,15 +125,17 @@ public function testWhenUsingUnderscoreSeparatedKeysHydratorStrategyIsAlwaysCons } /** - * @psalm-return Iterator */ - public static function underscoreHandlingDataProvider(): Iterator + public static function underscoreHandlingDataProvider(): array { - yield [true, 'foo_bar']; - yield [false, 'fooBar']; + return [ + [true, 'foo_bar'], + [false, 'fooBar'], + ]; } public function testContextAwarenessExtract(): void diff --git a/test/HydratorTest.php b/test/HydratorTest.php index fb45ccbb..71c9838f 100644 --- a/test/HydratorTest.php +++ b/test/HydratorTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator; -use Iterator; use Laminas\Hydrator\AbstractHydrator; use Laminas\Hydrator\ArraySerializableHydrator; use Laminas\Hydrator\ClassMethodsHydrator; @@ -59,61 +58,61 @@ protected function setUp(): void public function testInitiateValues(): void { - $this->assertSame('1', $this->classMethodsCamelCase->getFooBar()); - $this->assertSame('2', $this->classMethodsCamelCase->getFooBarBaz()); - $this->assertTrue($this->classMethodsCamelCase->getIsFoo()); - $this->assertTrue($this->classMethodsCamelCase->isBar()); - $this->assertTrue($this->classMethodsCamelCase->getHasFoo()); - $this->assertTrue($this->classMethodsCamelCase->hasBar()); - - $this->assertSame('1', $this->classMethodsTitleCase->getFooBar()); - $this->assertSame('2', $this->classMethodsTitleCase->getFooBarBaz()); - $this->assertTrue($this->classMethodsTitleCase->getIsFoo()); - $this->assertTrue($this->classMethodsTitleCase->getIsBar()); - $this->assertTrue($this->classMethodsTitleCase->getHasFoo()); - $this->assertTrue($this->classMethodsTitleCase->getHasBar()); - - $this->assertSame('1', $this->classMethodsUnderscore->getFooBar()); - $this->assertSame('2', $this->classMethodsUnderscore->getFooBarBaz()); - $this->assertTrue($this->classMethodsUnderscore->getIsFoo()); - $this->assertTrue($this->classMethodsUnderscore->isBar()); - $this->assertTrue($this->classMethodsUnderscore->getHasFoo()); - $this->assertTrue($this->classMethodsUnderscore->hasBar()); + self::assertSame('1', $this->classMethodsCamelCase->getFooBar()); + self::assertSame('2', $this->classMethodsCamelCase->getFooBarBaz()); + self::assertSame(true, $this->classMethodsCamelCase->getIsFoo()); + self::assertSame(true, $this->classMethodsCamelCase->isBar()); + self::assertSame(true, $this->classMethodsCamelCase->getHasFoo()); + self::assertSame(true, $this->classMethodsCamelCase->hasBar()); + + self::assertSame('1', $this->classMethodsTitleCase->getFooBar()); + self::assertSame('2', $this->classMethodsTitleCase->getFooBarBaz()); + self::assertSame(true, $this->classMethodsTitleCase->getIsFoo()); + self::assertSame(true, $this->classMethodsTitleCase->getIsBar()); + self::assertSame(true, $this->classMethodsTitleCase->getHasFoo()); + self::assertSame(true, $this->classMethodsTitleCase->getHasBar()); + + self::assertSame('1', $this->classMethodsUnderscore->getFooBar()); + self::assertSame('2', $this->classMethodsUnderscore->getFooBarBaz()); + self::assertSame(true, $this->classMethodsUnderscore->getIsFoo()); + self::assertSame(true, $this->classMethodsUnderscore->isBar()); + self::assertSame(true, $this->classMethodsUnderscore->getHasFoo()); + self::assertSame(true, $this->classMethodsUnderscore->hasBar()); } public function testHydratorReflection(): void { $hydrator = new ReflectionHydrator(); $datas = $hydrator->extract($this->reflection); - $this->assertArrayHaskey('foo', $datas); - $this->assertSame('1', $datas['foo']); - $this->assertArrayHaskey('fooBar', $datas); - $this->assertSame('2', $datas['fooBar']); - $this->assertArrayHaskey('fooBarBaz', $datas); - $this->assertSame('3', $datas['fooBarBaz']); + self::assertArrayHaskey('foo', $datas); + self::assertSame('1', $datas['foo']); + self::assertArrayHaskey('fooBar', $datas); + self::assertSame('2', $datas['fooBar']); + self::assertArrayHaskey('fooBarBaz', $datas); + self::assertSame('3', $datas['fooBarBaz']); $test = $hydrator->hydrate(['foo' => 'foo', 'fooBar' => 'bar', 'fooBarBaz' => 'baz'], $this->reflection); - $this->assertSame('foo', $test->foo); - $this->assertSame('bar', $test->getFooBar()); - $this->assertSame('baz', $test->getFooBarBaz()); + self::assertSame('foo', $test->foo); + self::assertSame('bar', $test->getFooBar()); + self::assertSame('baz', $test->getFooBarBaz()); } public function testHydratorClassMethodsCamelCase(): void { $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCase); - $this->assertArrayHaskey('fooBar', $datas); - $this->assertSame('1', $datas['fooBar']); - $this->assertArrayHaskey('fooBarBaz', $datas); - $this->assertArrayNotHasKey('foo_bar', $datas); - $this->assertArrayHaskey('isFoo', $datas); - $this->assertTrue($datas['isFoo']); - $this->assertArrayHaskey('isBar', $datas); - $this->assertTrue($datas['isBar']); - $this->assertArrayHaskey('hasFoo', $datas); - $this->assertTrue($datas['hasFoo']); - $this->assertArrayHaskey('hasBar', $datas); - $this->assertTrue($datas['hasBar']); + self::assertArrayHaskey('fooBar', $datas); + self::assertSame('1', $datas['fooBar']); + self::assertArrayHaskey('fooBarBaz', $datas); + self::assertArrayNotHasKey('foo_bar', $datas); + self::assertArrayHaskey('isFoo', $datas); + self::assertSame(true, $datas['isFoo']); + self::assertArrayHaskey('isBar', $datas); + self::assertSame(true, $datas['isBar']); + self::assertArrayHaskey('hasFoo', $datas); + self::assertSame(true, $datas['hasFoo']); + self::assertArrayHaskey('hasBar', $datas); + self::assertSame(true, $datas['hasBar']); $test = $hydrator->hydrate( [ 'fooBar' => 'foo', @@ -125,31 +124,31 @@ public function testHydratorClassMethodsCamelCase(): void ], $this->classMethodsCamelCase ); - $this->assertSame($this->classMethodsCamelCase, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('bar', $test->getFooBarBaz()); - $this->assertFalse($test->getIsFoo()); - $this->assertFalse($test->isBar()); - $this->assertFalse($test->getHasFoo()); - $this->assertFalse($test->hasBar()); + self::assertSame($this->classMethodsCamelCase, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('bar', $test->getFooBarBaz()); + self::assertSame(false, $test->getIsFoo()); + self::assertSame(false, $test->isBar()); + self::assertSame(false, $test->getHasFoo()); + self::assertSame(false, $test->hasBar()); } public function testHydratorClassMethodsTitleCase(): void { $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsTitleCase); - $this->assertArrayHaskey('FooBar', $datas); - $this->assertSame('1', $datas['FooBar']); - $this->assertArrayHaskey('FooBarBaz', $datas); - $this->assertArrayNotHasKey('foo_bar', $datas); - $this->assertArrayHaskey('IsFoo', $datas); - $this->assertTrue($datas['IsFoo']); - $this->assertArrayHaskey('IsBar', $datas); - $this->assertTrue($datas['IsBar']); - $this->assertArrayHaskey('HasFoo', $datas); - $this->assertTrue($datas['HasFoo']); - $this->assertArrayHaskey('HasBar', $datas); - $this->assertTrue($datas['HasBar']); + self::assertArrayHaskey('FooBar', $datas); + self::assertSame('1', $datas['FooBar']); + self::assertArrayHaskey('FooBarBaz', $datas); + self::assertArrayNotHasKey('foo_bar', $datas); + self::assertArrayHaskey('IsFoo', $datas); + self::assertSame(true, $datas['IsFoo']); + self::assertArrayHaskey('IsBar', $datas); + self::assertSame(true, $datas['IsBar']); + self::assertArrayHaskey('HasFoo', $datas); + self::assertSame(true, $datas['HasFoo']); + self::assertArrayHaskey('HasBar', $datas); + self::assertSame(true, $datas['HasBar']); $test = $hydrator->hydrate( [ 'FooBar' => 'foo', @@ -161,35 +160,35 @@ public function testHydratorClassMethodsTitleCase(): void ], $this->classMethodsTitleCase ); - $this->assertSame($this->classMethodsTitleCase, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('bar', $test->getFooBarBaz()); - $this->assertFalse($test->getIsFoo()); - $this->assertFalse($test->getIsBar()); - $this->assertFalse($test->getHasFoo()); - $this->assertFalse($test->getHasBar()); + self::assertSame($this->classMethodsTitleCase, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('bar', $test->getFooBarBaz()); + self::assertSame(false, $test->getIsFoo()); + self::assertSame(false, $test->getIsBar()); + self::assertSame(false, $test->getHasFoo()); + self::assertSame(false, $test->getHasBar()); } public function testHydratorClassMethodsUnderscore(): void { $hydrator = new ClassMethodsHydrator(true); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertArrayHaskey('foo_bar', $datas); - $this->assertSame('1', $datas['foo_bar']); - $this->assertArrayHaskey('foo_bar_baz', $datas); - $this->assertArrayNotHasKey('fooBar', $datas); - $this->assertArrayHaskey('is_foo', $datas); - $this->assertArrayNotHasKey('isFoo', $datas); - $this->assertTrue($datas['is_foo']); - $this->assertArrayHaskey('is_bar', $datas); - $this->assertArrayNotHasKey('isBar', $datas); - $this->assertTrue($datas['is_bar']); - $this->assertArrayHaskey('has_foo', $datas); - $this->assertArrayNotHasKey('hasFoo', $datas); - $this->assertTrue($datas['has_foo']); - $this->assertArrayHaskey('has_bar', $datas); - $this->assertArrayNotHasKey('hasBar', $datas); - $this->assertTrue($datas['has_bar']); + self::assertArrayHaskey('foo_bar', $datas); + self::assertSame('1', $datas['foo_bar']); + self::assertArrayHaskey('foo_bar_baz', $datas); + self::assertArrayNotHasKey('fooBar', $datas); + self::assertArrayHaskey('is_foo', $datas); + self::assertArrayNotHasKey('isFoo', $datas); + self::assertSame(true, $datas['is_foo']); + self::assertArrayHaskey('is_bar', $datas); + self::assertArrayNotHasKey('isBar', $datas); + self::assertSame(true, $datas['is_bar']); + self::assertArrayHaskey('has_foo', $datas); + self::assertArrayNotHasKey('hasFoo', $datas); + self::assertSame(true, $datas['has_foo']); + self::assertArrayHaskey('has_bar', $datas); + self::assertArrayNotHasKey('hasBar', $datas); + self::assertSame(true, $datas['has_bar']); $test = $hydrator->hydrate( [ 'foo_bar' => 'foo', @@ -201,13 +200,13 @@ public function testHydratorClassMethodsUnderscore(): void ], $this->classMethodsUnderscore ); - $this->assertSame($this->classMethodsUnderscore, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('bar', $test->getFooBarBaz()); - $this->assertFalse($test->getIsFoo()); - $this->assertFalse($test->isBar()); - $this->assertFalse($test->getHasFoo()); - $this->assertFalse($test->hasBar()); + self::assertSame($this->classMethodsUnderscore, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('bar', $test->getFooBarBaz()); + self::assertSame(false, $test->getIsFoo()); + self::assertSame(false, $test->isBar()); + self::assertSame(false, $test->getHasFoo()); + self::assertSame(false, $test->hasBar()); } public function testHydratorClassMethodsUnderscoreWithUnderscoreUpperCasedHydrateDataKeys(): void @@ -225,23 +224,23 @@ public function testHydratorClassMethodsUnderscoreWithUnderscoreUpperCasedHydrat ], $this->classMethodsUnderscore ); - $this->assertSame($this->classMethodsUnderscore, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('bar', $test->getFooBarBaz()); - $this->assertFalse($test->getIsFoo()); - $this->assertFalse($test->isBar()); - $this->assertFalse($test->getHasFoo()); - $this->assertFalse($test->hasBar()); + self::assertSame($this->classMethodsUnderscore, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('bar', $test->getFooBarBaz()); + self::assertSame(false, $test->getIsFoo()); + self::assertSame(false, $test->isBar()); + self::assertSame(false, $test->getHasFoo()); + self::assertSame(false, $test->hasBar()); } public function testHydratorClassMethodsOptions(): void { $hydrator = new ClassMethodsHydrator(); - $this->assertTrue($hydrator->getUnderscoreSeparatedKeys()); + self::assertTrue($hydrator->getUnderscoreSeparatedKeys()); $hydrator->setOptions(['underscoreSeparatedKeys' => false]); - $this->assertFalse($hydrator->getUnderscoreSeparatedKeys()); + self::assertFalse($hydrator->getUnderscoreSeparatedKeys()); $hydrator->setUnderscoreSeparatedKeys(true); - $this->assertTrue($hydrator->getUnderscoreSeparatedKeys()); + self::assertTrue($hydrator->getUnderscoreSeparatedKeys()); } public function testHydratorClassMethodsIgnoresInvalidValues(): void @@ -253,21 +252,21 @@ public function testHydratorClassMethodsIgnoresInvalidValues(): void 'invalid' => 'value', ]; $test = $hydrator->hydrate($data, $this->classMethodsUnderscore); - $this->assertSame($this->classMethodsUnderscore, $test); + self::assertSame($this->classMethodsUnderscore, $test); } public function testHydratorClassMethodsDefaultBehaviorIsConvertUnderscoreToCamelCase(): void { $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertArrayHaskey('foo_bar', $datas); - $this->assertSame('1', $datas['foo_bar']); - $this->assertArrayHaskey('foo_bar_baz', $datas); - $this->assertArrayNotHaskey('fooBar', $datas); + self::assertArrayHaskey('foo_bar', $datas); + self::assertSame('1', $datas['foo_bar']); + self::assertArrayHaskey('foo_bar_baz', $datas); + self::assertArrayNotHaskey('fooBar', $datas); $test = $hydrator->hydrate(['foo_bar' => 'foo', 'foo_bar_baz' => 'bar'], $this->classMethodsUnderscore); - $this->assertSame($this->classMethodsUnderscore, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('bar', $test->getFooBarBaz()); + self::assertSame($this->classMethodsUnderscore, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('bar', $test->getFooBarBaz()); } public function testRetrieveWildStrategyAndOther(): void @@ -276,9 +275,9 @@ public function testRetrieveWildStrategyAndOther(): void $hydrator->addStrategy('default', new DefaultStrategy()); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $default = $hydrator->getStrategy('default'); - $this->assertInstanceOf(DefaultStrategy::class, $default); + self::assertInstanceOf(DefaultStrategy::class, $default); $serializable = $hydrator->getStrategy('*'); - $this->assertInstanceOf(SerializableStrategy::class, $serializable); + self::assertInstanceOf(SerializableStrategy::class, $serializable); } public function testUseWildStrategyByDefault(): void @@ -286,25 +285,25 @@ public function testUseWildStrategyByDefault(): void $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertSame('1', $datas['foo_bar']); + self::assertSame('1', $datas['foo_bar']); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertSame('s:1:"1";', $datas['foo_bar']); + self::assertSame('s:1:"1";', $datas['foo_bar']); } public function testUseWildStrategyAndOther(): void { $hydrator = new ClassMethodsHydrator(); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertSame('1', $datas['foo_bar']); + self::assertSame('1', $datas['foo_bar']); $hydrator->addStrategy('foo_bar', new DefaultStrategy()); $hydrator->addStrategy('*', new SerializableStrategy(new PhpSerialize())); $datas = $hydrator->extract($this->classMethodsUnderscore); - $this->assertSame('1', $datas['foo_bar']); - $this->assertSame('s:1:"2";', $datas['foo_bar_baz']); + self::assertSame('1', $datas['foo_bar']); + self::assertSame('s:1:"2";', $datas['foo_bar_baz']); } public function testHydratorClassMethodsCamelCaseWithSetterMissing(): void @@ -312,14 +311,14 @@ public function testHydratorClassMethodsCamelCaseWithSetterMissing(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCaseMissing); - $this->assertArrayHaskey('fooBar', $datas); - $this->assertSame('1', $datas['fooBar']); - $this->assertArrayHaskey('fooBarBaz', $datas); - $this->assertArrayNotHaskey('foo_bar', $datas); + self::assertArrayHaskey('fooBar', $datas); + self::assertSame('1', $datas['fooBar']); + self::assertArrayHaskey('fooBarBaz', $datas); + self::assertArrayNotHaskey('foo_bar', $datas); $test = $hydrator->hydrate(['fooBar' => 'foo', 'fooBarBaz' => 1], $this->classMethodsCamelCaseMissing); - $this->assertSame($this->classMethodsCamelCaseMissing, $test); - $this->assertSame('foo', $test->getFooBar()); - $this->assertSame('2', $test->getFooBarBaz()); + self::assertSame($this->classMethodsCamelCaseMissing, $test); + self::assertSame('foo', $test->getFooBar()); + self::assertSame('2', $test->getFooBarBaz()); } public function testHydratorClassMethodsManipulateFilter(): void @@ -327,23 +326,23 @@ public function testHydratorClassMethodsManipulateFilter(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsCamelCase); - $this->assertArrayHaskey('fooBar', $datas); - $this->assertSame('1', $datas['fooBar']); - $this->assertArrayHaskey('fooBarBaz', $datas); - $this->assertArrayNotHasKey('foo_bar', $datas); - $this->assertArrayHaskey('isFoo', $datas); - $this->assertTrue($datas['isFoo']); - $this->assertArrayHaskey('isBar', $datas); - $this->assertTrue($datas['isBar']); - $this->assertArrayHaskey('hasFoo', $datas); - $this->assertTrue($datas['hasFoo']); - $this->assertArrayHaskey('hasBar', $datas); - $this->assertTrue($datas['hasBar']); + self::assertArrayHaskey('fooBar', $datas); + self::assertSame('1', $datas['fooBar']); + self::assertArrayHaskey('fooBarBaz', $datas); + self::assertArrayNotHasKey('foo_bar', $datas); + self::assertArrayHaskey('isFoo', $datas); + self::assertSame(true, $datas['isFoo']); + self::assertArrayHaskey('isBar', $datas); + self::assertSame(true, $datas['isBar']); + self::assertArrayHaskey('hasFoo', $datas); + self::assertSame(true, $datas['hasFoo']); + self::assertArrayHaskey('hasBar', $datas); + self::assertSame(true, $datas['hasBar']); $hydrator->removeFilter('has'); $datas = $hydrator->extract($this->classMethodsCamelCase); - $this->assertArrayHaskey('hasFoo', $datas); //method is getHasFoo - $this->assertArrayNotHaskey('hasBar', $datas); //method is hasBar + self::assertArrayHaskey('hasFoo', $datas); //method is getHasFoo + self::assertArrayNotHaskey('hasBar', $datas); //method is hasBar } public function testHydratorClassMethodsWithCustomFilter(): void @@ -365,7 +364,7 @@ static function ($property): bool { ); $datas = $hydrator->extract($this->classMethodsCamelCase); - $this->assertArrayNotHaskey('hasFoo', $datas); + self::assertArrayNotHaskey('hasFoo', $datas); } #[DataProvider('filterProvider')] @@ -373,12 +372,15 @@ public function testArraySerializableFilter( AbstractHydrator $hydrator, object $serializable ): void { - $this->assertSame([ - 'foo' => 'bar', - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], $hydrator->extract($serializable)); + self::assertSame( + [ + 'foo' => 'bar', + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], + $hydrator->extract($serializable) + ); $hydrator->addFilter('foo', static function ($property): bool { if ($property === 'foo') { @@ -387,11 +389,14 @@ public function testArraySerializableFilter( return true; }); - $this->assertSame([ - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], $hydrator->extract($serializable)); + self::assertSame( + [ + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], + $hydrator->extract($serializable) + ); $hydrator->addFilter('len', static function ($property): bool { if (strlen($property) !== 3) { @@ -400,30 +405,38 @@ public function testArraySerializableFilter( return true; }, FilterComposite::CONDITION_AND); - $this->assertSame([ - 'bar' => 'foo', - 'quo' => 'blubb', - ], $hydrator->extract($serializable)); + self::assertSame( + [ + 'bar' => 'foo', + 'quo' => 'blubb', + ], + $hydrator->extract($serializable) + ); $hydrator->removeFilter('len'); $hydrator->removeFilter('foo'); - $this->assertSame([ - 'foo' => 'bar', - 'bar' => 'foo', - 'blubb' => 'baz', - 'quo' => 'blubb', - ], $hydrator->extract($serializable)); + self::assertSame( + [ + 'foo' => 'bar', + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], + $hydrator->extract($serializable) + ); } /** - * @psalm-return Iterator + * @psalm-return list */ - public static function filterProvider(): Iterator + public static function filterProvider(): array { - yield [new ObjectPropertyHydrator(), new ObjectPropertyAsset()]; - yield [new ArraySerializableHydrator(), new ArraySerializableAsset()]; - yield [new ReflectionHydrator(), new ReflectionFilter()]; + return [ + [new ObjectPropertyHydrator(), new ObjectPropertyAsset()], + [new ArraySerializableHydrator(), new ArraySerializableAsset()], + [new ReflectionHydrator(), new ReflectionFilter()], + ]; } public function testHydratorClassMethodsWithInvalidNumberOfParameters(): void @@ -431,9 +444,9 @@ public function testHydratorClassMethodsWithInvalidNumberOfParameters(): void $hydrator = new ClassMethodsHydrator(false); $datas = $hydrator->extract($this->classMethodsInvalidParameter); - $this->assertTrue($datas['hasBar']); - $this->assertSame('Bar', $datas['foo']); - $this->assertFalse($datas['isBla']); + self::assertTrue($datas['hasBar']); + self::assertSame('Bar', $datas['foo']); + self::assertFalse($datas['isBla']); } public function testObjectBasedFilters(): void @@ -441,9 +454,9 @@ public function testObjectBasedFilters(): void $hydrator = new ClassMethodsHydrator(false); $foo = new ClassMethodsFilterProviderInterface(); $data = $hydrator->extract($foo); - $this->assertArrayNotHasKey('filter', $data); - $this->assertSame('bar', $data['foo']); - $this->assertSame('foo', $data['bar']); + self::assertArrayNotHasKey('filter', $data); + self::assertSame('bar', $data['foo']); + self::assertSame('foo', $data['bar']); } public function testHydratorClassMethodsWithProtectedSetter(): void @@ -453,7 +466,7 @@ public function testHydratorClassMethodsWithProtectedSetter(): void $hydrator->hydrate(['foo' => 'bar', 'bar' => 'BAR'], $object); $data = $hydrator->extract($object); - $this->assertSame('BAR', $data['bar']); + self::assertSame('BAR', $data['bar']); } public function testHydratorClassMethodsWithMagicMethodSetter(): void @@ -463,7 +476,7 @@ public function testHydratorClassMethodsWithMagicMethodSetter(): void $hydrator->hydrate(['foo' => 'bar'], $object); $data = $hydrator->extract($object); - $this->assertSame('bar', $data['foo']); + self::assertSame('bar', $data['foo']); } public function testHydratorClassMethodsWithMagicMethodSetterAndMethodExistsCheck(): void @@ -473,6 +486,6 @@ public function testHydratorClassMethodsWithMagicMethodSetterAndMethodExistsChec $hydrator->hydrate(['foo' => 'bar'], $object); $data = $hydrator->extract($object); - $this->assertNull($data['foo']); + self::assertNull($data['foo']); } } diff --git a/test/NamingStrategy/CompositeNamingStrategyTest.php b/test/NamingStrategy/CompositeNamingStrategyTest.php index b2d9eaa1..0533a2ac 100644 --- a/test/NamingStrategy/CompositeNamingStrategyTest.php +++ b/test/NamingStrategy/CompositeNamingStrategyTest.php @@ -19,8 +19,8 @@ public function testGetSameNameWhenNoNamingStrategyExistsForTheName(): void 'foo' => $this->createMock(NamingStrategyInterface::class), ]); - $this->assertSame('bar', $compositeNamingStrategy->hydrate('bar')); - $this->assertSame('bar', $compositeNamingStrategy->extract('bar')); + $this->assertEquals('bar', $compositeNamingStrategy->hydrate('bar')); + $this->assertEquals('bar', $compositeNamingStrategy->extract('bar')); } public function testUseDefaultNamingStrategy(): void @@ -40,8 +40,8 @@ public function testUseDefaultNamingStrategy(): void ['bar' => $this->createMock(NamingStrategyInterface::class)], $defaultNamingStrategy ); - $this->assertSame('Foo', $compositeNamingStrategy->hydrate('foo')); - $this->assertSame('foo', $compositeNamingStrategy->extract('Foo')); + $this->assertEquals('Foo', $compositeNamingStrategy->hydrate('foo')); + $this->assertEquals('foo', $compositeNamingStrategy->extract('Foo')); } public function testHydrate(): void @@ -52,7 +52,7 @@ public function testHydrate(): void ->with('foo') ->willReturn('FOO'); $compositeNamingStrategy = new CompositeNamingStrategy(['foo' => $fooNamingStrategy]); - $this->assertSame('FOO', $compositeNamingStrategy->hydrate('foo')); + $this->assertEquals('FOO', $compositeNamingStrategy->hydrate('foo')); } public function testExtract(): void @@ -63,6 +63,6 @@ public function testExtract(): void ->with('FOO') ->willReturn('foo'); $compositeNamingStrategy = new CompositeNamingStrategy(['FOO' => $fooNamingStrategy]); - $this->assertSame('foo', $compositeNamingStrategy->extract('FOO')); + $this->assertEquals('foo', $compositeNamingStrategy->extract('FOO')); } } diff --git a/test/NamingStrategy/IdentityNamingStrategyTest.php b/test/NamingStrategy/IdentityNamingStrategyTest.php index 98252f1b..72ad5bc3 100644 --- a/test/NamingStrategy/IdentityNamingStrategyTest.php +++ b/test/NamingStrategy/IdentityNamingStrategyTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator\NamingStrategy; -use Iterator; use Laminas\Hydrator\NamingStrategy\IdentityNamingStrategy; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -32,11 +31,13 @@ public function testExtract(string $name): void /** * Data provider * - * @return Iterator<(int | string), array> + * @return string[][] */ - public static function getTestedNames(): Iterator + public static function getTestedNames(): array { - yield 'foo' => ['foo']; - yield 'bar' => ['bar']; + return [ + 'foo' => ['foo'], + 'bar' => ['bar'], + ]; } } diff --git a/test/NamingStrategy/MapNamingStrategyTest.php b/test/NamingStrategy/MapNamingStrategyTest.php index f1ec9ff5..8f5b9a76 100644 --- a/test/NamingStrategy/MapNamingStrategyTest.php +++ b/test/NamingStrategy/MapNamingStrategyTest.php @@ -26,14 +26,14 @@ public static function invalidMapValues(): Generator yield 'object' => [(object) ['foo' => 'bar']]; } - /** @psalm-return Generator */ + /** @psalm-return Generator}> */ public static function invalidKeyArrays(): Generator { yield 'int' => [ - [1 => 'foo'], + 'invalidKeyArray' => [1 => 'foo'], ]; yield 'emtpy-string' => [ - ['' => 'foo'], + 'invalidKeyArray' => ['' => 'foo'], ]; } @@ -84,43 +84,43 @@ public function testHydrationMapConstructorRaisesExceptionWhenFlippingExtraction public function testExtractReturnsVerbatimWhenEmptyExtractionMapProvided(): void { $strategy = MapNamingStrategy::createFromExtractionMap([]); - $this->assertSame('some_stuff', $strategy->extract('some_stuff')); + self::assertEquals('some_stuff', $strategy->extract('some_stuff')); } public function testHydrateReturnsVerbatimWhenEmptyHydrationMapProvided(): void { $strategy = MapNamingStrategy::createFromHydrationMap([]); - $this->assertSame('some_stuff', $strategy->hydrate('some_stuff')); + self::assertEquals('some_stuff', $strategy->hydrate('some_stuff')); } public function testExtractUsesProvidedExtractionMap(): void { $strategy = MapNamingStrategy::createFromExtractionMap(['stuff3' => 'stuff4']); - $this->assertSame('stuff4', $strategy->extract('stuff3')); + self::assertEquals('stuff4', $strategy->extract('stuff3')); } public function testExtractUsesFlippedHydrationMapWhenOnlyHydrationMapProvided(): void { $strategy = MapNamingStrategy::createFromHydrationMap(['stuff3' => 'stuff4']); - $this->assertSame('stuff3', $strategy->extract('stuff4')); + self::assertEquals('stuff3', $strategy->extract('stuff4')); } public function testHydrateUsesProvidedHydrationMap(): void { $strategy = MapNamingStrategy::createFromHydrationMap(['stuff3' => 'stuff4']); - $this->assertSame('stuff4', $strategy->hydrate('stuff3')); + self::assertEquals('stuff4', $strategy->hydrate('stuff3')); } public function testHydrateUsesFlippedExtractionMapOnlyExtractionMapProvided(): void { $strategy = MapNamingStrategy::createFromExtractionMap(['foo' => 'bar']); - $this->assertSame('foo', $strategy->hydrate('bar')); + self::assertEquals('foo', $strategy->hydrate('bar')); } public function testHydrateAndExtractUseAsymmetricMapProvided(): void { $strategy = MapNamingStrategy::createFromAsymmetricMap(['foo' => 'bar'], ['bat' => 'baz']); - $this->assertSame('bar', $strategy->extract('foo')); - $this->assertSame('baz', $strategy->hydrate('bat')); + self::assertEquals('bar', $strategy->extract('foo')); + self::assertEquals('baz', $strategy->hydrate('bat')); } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php index 176ae958..d77e1d90 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/CamelCaseToUnderscoreFilterTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator\NamingStrategy\UnderscoreNamingStrategy; -use Iterator; use Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy\CamelCaseToUnderscoreFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -27,8 +26,8 @@ public function testFilterUnderscoresNonUnicodeStrings(string $string, string $e $filtered = $filter->filter($string); - $this->assertNotSame($string, $filtered); - $this->assertSame($expected, $filtered); + $this->assertNotEquals($string, $filtered); + $this->assertEquals($expected, $filtered); } #[DataProvider('unicodeProvider')] @@ -42,8 +41,8 @@ public function testFilterUnderscoresUnicodeStrings(string $string, string $expe $filtered = $filter->filter($string); - $this->assertNotSame($string, $filtered); - $this->assertSame($expected, $filtered); + $this->assertNotEquals($string, $filtered); + $this->assertEquals($expected, $filtered); } #[DataProvider('unicodeProviderWithoutMbStrings')] @@ -57,103 +56,109 @@ public function testFilterUnderscoresUnicodeStringsWithoutMbStrings(string $stri $filtered = $filter->filter($string); - $this->assertNotSame($string, $filtered); - $this->assertSame($expected, $filtered); + $this->assertNotEquals($string, $filtered); + $this->assertEquals($expected, $filtered); } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function nonUnicodeProvider(): Iterator + public static function nonUnicodeProvider(): array { - yield 'upcased first letter' => [ - 'Camel', - 'camel', - ]; - yield 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ]; - yield 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ]; - yield 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ]; - yield 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ]; - yield 'lowercased alphanumeric' => [ - 'bfd7b82e9cfceaa82704d1c1Foo', - 'bfd7b82e9cfceaa82704d1c1_foo', + return [ + 'upcased first letter' => [ + 'Camel', + 'camel', + ], + 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ], + 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ], + 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ], + 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ], + 'lowercased alphanumeric' => [ + 'bfd7b82e9cfceaa82704d1c1Foo', + 'bfd7b82e9cfceaa82704d1c1_foo', + ], ]; } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function unicodeProvider(): Iterator + public static function unicodeProvider(): array { - yield 'upcased first letter' => [ - 'Camel', - 'camel', - ]; - yield 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ]; - yield 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ]; - yield 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ]; - yield 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ]; - yield 'unicode' => [ - 'testŠuma', - 'test_šuma', + return [ + 'upcased first letter' => [ + 'Camel', + 'camel', + ], + 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ], + 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ], + 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ], + 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ], + 'unicode' => [ + 'testŠuma', + 'test_šuma', + ], ]; } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function unicodeProviderWithoutMbStrings(): Iterator + public static function unicodeProviderWithoutMbStrings(): array { - yield 'upcased first letter' => [ - 'Camel', - 'camel', - ]; - yield 'multiple words' => [ - 'underscoresMe', - 'underscores_me', - ]; - yield 'alphanumeric' => [ - 'one2Three', - 'one2_three', - ]; - yield 'multiple uppercased letters and underscores' => [ - 'TheseAre_SOME_CamelCASEDWords', - 'these_are_some_camel_cased_words', - ]; - yield 'alphanumeric multiple up cases' => [ - 'one2THR23ree', - 'one2_thr23ree', - ]; - yield 'unicode uppercase character' => [ - 'testŠuma', - 'test_Šuma', + return [ + 'upcased first letter' => [ + 'Camel', + 'camel', + ], + 'multiple words' => [ + 'underscoresMe', + 'underscores_me', + ], + 'alphanumeric' => [ + 'one2Three', + 'one2_three', + ], + 'multiple uppercased letters and underscores' => [ + 'TheseAre_SOME_CamelCASEDWords', + 'these_are_some_camel_cased_words', + ], + 'alphanumeric multiple up cases' => [ + 'one2THR23ree', + 'one2_thr23ree', + ], + 'unicode uppercase character' => [ + 'testŠuma', + 'test_Šuma', + ], ]; } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php index af686e63..dbc5f1f1 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategy/UnderscoreToCamelCaseFilterTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator\NamingStrategy\UnderscoreNamingStrategy; -use Iterator; use Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy\UnderscoreToCamelCaseFilter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -27,31 +26,33 @@ public function testFilterCamelCasesNonUnicodeStrings(string $string, string $ex $filtered = $filter->filter($string); - $this->assertNotSame($string, $filtered); - $this->assertSame($expected, $filtered); + $this->assertNotEquals($string, $filtered); + $this->assertEquals($expected, $filtered); } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function nonUnicodeProvider(): Iterator + public static function nonUnicodeProvider(): array { - yield 'one word' => [ - 'Studly', - 'studly', - ]; - yield 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ]; - yield 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ]; - yield 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', + return [ + 'one word' => [ + 'Studly', + 'studly', + ], + 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ], + 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ], + 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', + ], ]; } @@ -65,39 +66,41 @@ public function testFilterCamelCasesUnicodeStrings(string $string, string $expec $filter = new UnderscoreToCamelCaseFilter(); $filtered = $filter->filter($string); - $this->assertNotSame($string, $filtered); - $this->assertSame($expected, $filtered); + $this->assertNotEquals($string, $filtered); + $this->assertEquals($expected, $filtered); } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function unicodeProvider(): Iterator + public static function unicodeProvider(): array { - yield 'uppercase first letter' => [ - 'Camel', - 'camel', - ]; - yield 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ]; - yield 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ]; - yield 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', - ]; - yield 'unicode character' => [ - 'test_Šuma', - 'testŠuma', - ]; - yield 'unicode character [Laminas-10517]' => [ - 'test_šuma', - 'testŠuma', + return [ + 'uppercase first letter' => [ + 'Camel', + 'camel', + ], + 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ], + 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ], + 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', + ], + 'unicode character' => [ + 'test_Šuma', + 'testŠuma', + ], + 'unicode character [Laminas-10517]' => [ + 'test_šuma', + 'testŠuma', + ], ]; } @@ -113,34 +116,36 @@ public function testFilterCamelCasesUnicodeStringsWithoutMbStrings( $property->setValue($filter, false); $filtered = $filter->filter($string); - $this->assertSame($expected, $filtered); + $this->assertEquals($expected, $filtered); } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function unicodeWithoutMbStringsProvider(): Iterator + public static function unicodeWithoutMbStringsProvider(): array { - yield 'multiple words' => [ - 'studly_cases_me', - 'studlyCasesMe', - ]; - yield 'alphanumeric in single word' => [ - 'one_2_three', - 'one2Three', - ]; - yield 'alphanumeric in separate words' => [ - 'one2_three', - 'one2Three', - ]; - yield 'uppercase unicode character' => [ - 'test_Šuma', - 'testŠuma', - ]; - yield 'lowercase unicode character' => [ - 'test_šuma', - 'test_šuma', + return [ + 'multiple words' => [ + 'studly_cases_me', + 'studlyCasesMe', + ], + 'alphanumeric in single word' => [ + 'one_2_three', + 'one2Three', + ], + 'alphanumeric in separate words' => [ + 'one2_three', + 'one2Three', + ], + 'uppercase unicode character' => [ + 'test_Šuma', + 'testŠuma', + ], + 'lowercase unicode character' => [ + 'test_šuma', + 'test_šuma', + ], ]; } } diff --git a/test/NamingStrategy/UnderscoreNamingStrategyTest.php b/test/NamingStrategy/UnderscoreNamingStrategyTest.php index 5bf0144c..a698bd3c 100644 --- a/test/NamingStrategy/UnderscoreNamingStrategyTest.php +++ b/test/NamingStrategy/UnderscoreNamingStrategyTest.php @@ -15,13 +15,13 @@ class UnderscoreNamingStrategyTest extends TestCase public function testNameHydratesToCamelCase(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertSame('fooBarBaz', $strategy->hydrate('foo_bar_baz')); + $this->assertEquals('fooBarBaz', $strategy->hydrate('foo_bar_baz')); } public function testNameExtractsToUnderscore(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertSame('foo_bar_baz', $strategy->extract('fooBarBaz')); + $this->assertEquals('foo_bar_baz', $strategy->extract('fooBarBaz')); } #[Group('6422')] @@ -30,6 +30,6 @@ public function testNameHydratesToStudlyCaps(): void { $strategy = new UnderscoreNamingStrategy(); - $this->assertSame('fooBarBaz', $strategy->hydrate('Foo_Bar_Baz')); + $this->assertEquals('fooBarBaz', $strategy->hydrate('Foo_Bar_Baz')); } } diff --git a/test/StandaloneHydratorPluginManagerTest.php b/test/StandaloneHydratorPluginManagerTest.php index 29c43e0e..8c822a50 100644 --- a/test/StandaloneHydratorPluginManagerTest.php +++ b/test/StandaloneHydratorPluginManagerTest.php @@ -52,14 +52,14 @@ public function testInstantiationInitializesFactoriesForHydratorsWithoutConstruc { $factories = $this->reflectProperty($this->manager, 'factories'); - $this->assertArrayHasKey($class, $factories); - $this->assertInstanceOf(Closure::class, $factories[$class]); + self::assertArrayHasKey($class, $factories); + self::assertInstanceOf(Closure::class, $factories[$class]); } public function testDelegatingHydratorFactoryIsInitialized(): void { $factories = $this->reflectProperty($this->manager, 'factories'); - $this->assertInstanceOf( + self::assertInstanceOf( Hydrator\DelegatingHydratorFactory::class, $factories[Hydrator\DelegatingHydrator::class] ); @@ -67,7 +67,7 @@ public function testDelegatingHydratorFactoryIsInitialized(): void public function testHasReturnsFalseForUnknownNames(): void { - $this->assertFalse($this->manager->has('unknown-service-name')); + self::assertFalse($this->manager->has('unknown-service-name')); } /** @return Generator */ @@ -89,7 +89,7 @@ public static function knownServices(): Generator #[DataProvider('knownServices')] public function testHasReturnsTrueForKnownServices(string $service): void { - $this->assertTrue($this->manager->has($service)); + self::assertTrue($this->manager->has($service)); } public function testGetRaisesExceptionForUnknownService(): void @@ -106,6 +106,6 @@ public function testGetRaisesExceptionForUnknownService(): void public function testGetReturnsExpectedTypesForKnownServices(string $service, string $expectedType): void { $instance = $this->manager->get($service); - $this->assertInstanceOf($expectedType, $instance); + self::assertInstanceOf($expectedType, $instance); } } diff --git a/test/Strategy/BackedEnumStrategyTest.php b/test/Strategy/BackedEnumStrategyTest.php index 423f1084..726df809 100644 --- a/test/Strategy/BackedEnumStrategyTest.php +++ b/test/Strategy/BackedEnumStrategyTest.php @@ -17,7 +17,7 @@ class BackedEnumStrategyTest extends TestCase public function testExtractInvalidValueThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - $this->expectException(InvalidArgumentException::class); + self::expectException(InvalidArgumentException::class); $strategy->extract(TestUnitEnum::One); } @@ -25,7 +25,7 @@ public function testExtractExtractsValue(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->extract(TestBackedEnum::One); - $this->assertSame('one', $actual); + self::assertSame('one', $actual); } public function testHydrateEnumReturnsEnum(): void @@ -33,22 +33,22 @@ public function testHydrateEnumReturnsEnum(): void $expected = TestBackedEnum::Two; $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->hydrate($expected, null); - $this->assertSame(TestBackedEnum::Two, $actual); + self::assertSame(TestBackedEnum::Two, $actual); } public function testHydrateNonScalarThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Value must be string or int; array provided"); + self::expectException(InvalidArgumentException::class); + self::expectExceptionMessage("Value must be string or int; array provided"); $strategy->hydrate([], null); } public function testHydrateNonCaseThrowsException(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Value 'three' is not a valid scalar value for " . TestBackedEnum::class); + self::expectException(InvalidArgumentException::class); + self::expectExceptionMessage("Value 'three' is not a valid scalar value for " . TestBackedEnum::class); $strategy->hydrate('three', null); } @@ -56,6 +56,6 @@ public function testHydrateValueReturnsEnum(): void { $strategy = new BackedEnumStrategy(TestBackedEnum::class); $actual = $strategy->hydrate('two', null); - $this->assertSame(TestBackedEnum::Two, $actual); + self::assertSame(TestBackedEnum::Two, $actual); } } diff --git a/test/Strategy/BooleanStrategyTest.php b/test/Strategy/BooleanStrategyTest.php index b4fe60ec..bc314826 100644 --- a/test/Strategy/BooleanStrategyTest.php +++ b/test/Strategy/BooleanStrategyTest.php @@ -26,16 +26,16 @@ public function testConstructorWithValidString(): void public function testExtractString(): void { $hydrator = new BooleanStrategy('true', 'false'); - $this->assertSame('true', $hydrator->extract(true)); - $this->assertSame('false', $hydrator->extract(false)); + $this->assertEquals('true', $hydrator->extract(true)); + $this->assertEquals('false', $hydrator->extract(false)); } public function testExtractInteger(): void { $hydrator = new BooleanStrategy(1, 0); - $this->assertSame(1, $hydrator->extract(true)); - $this->assertSame(0, $hydrator->extract(false)); + $this->assertEquals(1, $hydrator->extract(true)); + $this->assertEquals(0, $hydrator->extract(false)); } public function testExtractThrowsExceptionOnUnknownValue(): void diff --git a/test/Strategy/CollectionStrategyTest.php b/test/Strategy/CollectionStrategyTest.php index 2eb78fb7..b8536591 100644 --- a/test/Strategy/CollectionStrategyTest.php +++ b/test/Strategy/CollectionStrategyTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator\Strategy; use Generator; -use Iterator; use Laminas\Hydrator\Exception; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\ReflectionHydrator; @@ -50,20 +49,22 @@ public function testConstructorRejectsInvalidObjectClassName( ); } - /** @return Iterator, string}> */ - public static function providerInvalidObjectClassName(): Iterator + /** @return array, 2: string}> */ + public static function providerInvalidObjectClassName(): array { - // @codingStandardsIgnoreEnd // @codingStandardsIgnoreStart - yield 'array' => [[], TypeError::class, 'type string']; - yield 'boolean-false' => [false, TypeError::class, 'type string']; - yield 'boolean-true' => [true, TypeError::class, 'type string']; - yield 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string']; - yield 'integer' => [mt_rand(), TypeError::class, 'type string']; - yield 'null' => [null, TypeError::class, 'type string']; - yield 'object' => [new stdClass(), TypeError::class, 'type string']; - yield 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string']; - yield 'string-non-existent-class' => ['FooBarBaz9000', Exception\InvalidArgumentException::class, 'class name needs to be the name of an existing class']; + return [ + 'array' => [[], TypeError::class, 'type string'], + 'boolean-false' => [false, TypeError::class, 'type string'], + 'boolean-true' => [true, TypeError::class, 'type string'], + 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string'], + 'integer' => [mt_rand(), TypeError::class, 'type string'], + 'null' => [null, TypeError::class, 'type string'], + 'object' => [new stdClass(), TypeError::class, 'type string'], + 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string'], + 'string-non-existent-class' => ['FooBarBaz9000', Exception\InvalidArgumentException::class, 'class name needs to be the name of an existing class'], + ]; + // @codingStandardsIgnoreEnd } #[DataProvider('providerInvalidValueForExtraction')] @@ -124,7 +125,7 @@ public function testExtractUsesHydratorToExtractValues(): void $hydrator = $this->createHydratorMock(); $hydrator - ->expects($this->exactly(count($value))) + ->expects(self::exactly(count($value))) ->method('extract') ->willReturnCallback($extraction); @@ -135,7 +136,7 @@ public function testExtractUsesHydratorToExtractValues(): void $expected = array_map($extraction, $value); - $this->assertSame($expected, $strategy->extract($value)); + self::assertSame($expected, $strategy->extract($value)); } #[DataProvider('providerInvalidValueForHydration')] @@ -200,7 +201,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $hydrator = $this->createHydratorMock(); $hydrator - ->expects($this->exactly(count($value))) + ->expects(self::exactly(count($value))) ->method('hydrate') ->willReturnCallback($hydration); @@ -211,7 +212,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $expected = array_map($hydration, $value); - $this->assertEquals($expected, $strategy->hydrate($value)); + self::assertEquals($expected, $strategy->hydrate($value)); } private function createHydratorMock(): HydratorInterface&MockObject diff --git a/test/Strategy/DateTimeFormatterStrategyTest.php b/test/Strategy/DateTimeFormatterStrategyTest.php index 0497415b..bf2df6cb 100644 --- a/test/Strategy/DateTimeFormatterStrategyTest.php +++ b/test/Strategy/DateTimeFormatterStrategyTest.php @@ -8,7 +8,6 @@ use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; -use Iterator; use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversClass; @@ -22,25 +21,25 @@ class DateTimeFormatterStrategyTest extends TestCase public function testHydrate(): void { $strategy = new DateTimeFormatterStrategy('Y-m-d'); - $this->assertEquals('2014-04-26', $strategy->hydrate('2014-04-26')->format('Y-m-d')); + self::assertEquals('2014-04-26', $strategy->hydrate('2014-04-26')->format('Y-m-d')); $strategy = new DateTimeFormatterStrategy('Y-m-d', new DateTimeZone('Asia/Kathmandu')); $date = $strategy->hydrate('2014-04-26'); - $this->assertEquals('Asia/Kathmandu', $date->getTimezone()->getName()); + self::assertEquals('Asia/Kathmandu', $date->getTimezone()->getName()); } public function testExtract(): void { $strategy = new DateTimeFormatterStrategy('d/m/Y'); - $this->assertEquals('26/04/2014', $strategy->extract(new DateTime('2014-04-26'))); + self::assertEquals('26/04/2014', $strategy->extract(new DateTime('2014-04-26'))); } public function testGetNullWithInvalidDateOnHydration(): void { $strategy = new DateTimeFormatterStrategy('Y-m-d'); - $this->assertEquals(null, $strategy->hydrate(null)); - $this->assertEquals(null, $strategy->hydrate('')); + self::assertEquals(null, $strategy->hydrate(null)); + self::assertEquals(null, $strategy->hydrate('')); } public function testCanExtractIfNotDateTime(): void @@ -48,13 +47,13 @@ public function testCanExtractIfNotDateTime(): void $strategy = new DateTimeFormatterStrategy(); $date = $strategy->extract(new stdClass()); - $this->assertInstanceOf(stdClass::class, $date); + self::assertInstanceOf(stdClass::class, $date); } public function testCanHydrateWithInvalidDateTime(): void { $strategy = new DateTimeFormatterStrategy('d/m/Y'); - $this->assertSame('foo bar baz', $strategy->hydrate('foo bar baz')); + self::assertSame('foo bar baz', $strategy->hydrate('foo bar baz')); } public function testCanExtractAnyDateTimeInterface(): void @@ -65,7 +64,7 @@ public function testCanExtractAnyDateTimeInterface(): void $format = 'Y-m-d'; $dateMock - ->expects($this->once()) + ->expects(self::once()) ->method('format') ->with($format); @@ -74,7 +73,7 @@ public function testCanExtractAnyDateTimeInterface(): void ->getMock(); $dateImmutableMock - ->expects($this->once()) + ->expects(self::once()) ->method('format') ->with($format); @@ -90,8 +89,8 @@ public function testAcceptsCreateFromFormatSpecialCharacters(string $format, str $strategy = new DateTimeFormatterStrategy($format); $hydrated = $strategy->hydrate($expectedValue); - $this->assertInstanceOf(DateTime::class, $hydrated); - $this->assertSame($expectedValue, $hydrated->format('Y-m-d')); + self::assertInstanceOf(DateTime::class, $hydrated); + self::assertEquals($expectedValue, $hydrated->format('Y-m-d')); } #[DataProvider('formatsWithSpecialCharactersProvider')] @@ -101,7 +100,7 @@ public function testCanExtractWithCreateFromFormatSpecialCharacters(string $form $strategy = new DateTimeFormatterStrategy($format); $extracted = $strategy->extract($date); - $this->assertEquals($expectedValue, $extracted); + self::assertEquals($expectedValue, $extracted); } public function testCanExtractWithCreateFromFormatEscapedSpecialCharacters(): void @@ -109,18 +108,20 @@ public function testCanExtractWithCreateFromFormatEscapedSpecialCharacters(): vo $date = DateTime::createFromFormat('Y-m-d', '2018-02-05'); $strategy = new DateTimeFormatterStrategy('Y-m-d\\+'); $extracted = $strategy->extract($date); - $this->assertEquals('2018-02-05+', $extracted); + self::assertEquals('2018-02-05+', $extracted); } /** - * @return Iterator<(int | string), array> - * @psalm-return Iterator + * @return string[][] + * @psalm-return array */ - public static function formatsWithSpecialCharactersProvider(): Iterator + public static function formatsWithSpecialCharactersProvider(): array { - yield '!-prepended' => ['!Y-m-d', '2018-02-05']; - yield '|-appended' => ['Y-m-d|', '2018-02-05']; - yield '+-appended' => ['Y-m-d+', '2018-02-05']; + return [ + '!-prepended' => ['!Y-m-d', '2018-02-05'], + '|-appended' => ['Y-m-d|', '2018-02-05'], + '+-appended' => ['Y-m-d+', '2018-02-05'], + ]; } public function testCanHydrateWithDateTimeFallback(): void @@ -128,25 +129,27 @@ public function testCanHydrateWithDateTimeFallback(): void $strategy = new DateTimeFormatterStrategy('Y-m-d', null, true); $date = $strategy->hydrate('2018-09-06T12:10:30'); - $this->assertInstanceOf(DateTimeInterface::class, $date); - $this->assertSame('2018-09-06', $date->format('Y-m-d')); + self::assertInstanceOf(DateTimeInterface::class, $date); + self::assertSame('2018-09-06', $date->format('Y-m-d')); $strategy = new DateTimeFormatterStrategy('Y-m-d', new DateTimeZone('Europe/Prague'), true); $date = $strategy->hydrate('2018-09-06T12:10:30'); - $this->assertInstanceOf(DateTimeInterface::class, $date); - $this->assertSame('Europe/Prague', $date->getTimezone()->getName()); + self::assertInstanceOf(DateTimeInterface::class, $date); + self::assertSame('Europe/Prague', $date->getTimezone()->getName()); } - /** @return Iterator> */ - public static function invalidValuesForHydration(): Iterator + /** @return array> */ + public static function invalidValuesForHydration(): array { - yield 'zero' => [0]; - yield 'int' => [1]; - yield 'zero-float' => [0.0]; - yield 'float' => [1.1]; - yield 'array' => [['2018-11-20']]; - yield 'object' => [(object) ['date' => '2018-11-20']]; + return [ + 'zero' => [0], + 'int' => [1], + 'zero-float' => [0.0], + 'float' => [1.1], + 'array' => [['2018-11-20']], + 'object' => [(object) ['date' => '2018-11-20']], + ]; } #[DataProvider('invalidValuesForHydration')] @@ -159,12 +162,14 @@ public function testHydrateRaisesExceptionIfValueIsInvalid(mixed $value): void $strategy->hydrate($value); } - /** @return Iterator> */ - public static function validUnHydratableValues(): Iterator + /** @return array> */ + public static function validUnHydratableValues(): array { - yield 'empty string' => ['']; - yield 'null' => [null]; - yield 'date-time' => [new DateTimeImmutable('now')]; + return [ + 'empty string' => [''], + 'null' => [null], + 'date-time' => [new DateTimeImmutable('now')], + ]; } #[DataProvider('validUnHydratableValues')] @@ -172,6 +177,6 @@ public function testReturnsValueVerbatimUnderSpecificConditions(mixed $value): v { $strategy = new DateTimeFormatterStrategy('Y-m-d'); $hydrated = $strategy->hydrate($value); - $this->assertSame($value, $hydrated); + self::assertSame($value, $hydrated); } } diff --git a/test/Strategy/DateTimeImmutableFormatterStrategyTest.php b/test/Strategy/DateTimeImmutableFormatterStrategyTest.php index 66027ea0..6a31b386 100644 --- a/test/Strategy/DateTimeImmutableFormatterStrategyTest.php +++ b/test/Strategy/DateTimeImmutableFormatterStrategyTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator\Strategy; use DateTimeImmutable; -use Iterator; use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy; use PHPUnit\Framework\Attributes\CoversClass; @@ -26,36 +25,47 @@ protected function setUp(): void public function testExtraction(): void { - $this->assertEquals('2020-05-25', $this->strategy->extract(new DateTimeImmutable('2020-05-25'))); + self::assertEquals( + '2020-05-25', + $this->strategy->extract(new DateTimeImmutable('2020-05-25')) + ); } public function testHydrationWithDateTimeImmutableObjectShouldReturnSame(): void { $dateTime = new DateTimeImmutable('2020-05-25'); - $this->assertEquals($dateTime, $this->strategy->hydrate($dateTime)); + self::assertEquals($dateTime, $this->strategy->hydrate($dateTime)); } public function testHydrationShouldReturnImmutableDateTimeObject(): void { - $this->assertInstanceOf(DateTimeImmutable::class, $this->strategy->hydrate('2020-05-25')); + self::assertInstanceOf( + DateTimeImmutable::class, + $this->strategy->hydrate('2020-05-25') + ); } public function testHydrationShouldReturnDateTimeObjectWithSameValue(): void { - $this->assertSame('2020-05-25', $this->strategy->hydrate('2020-05-25')->format('Y-m-d')); + self::assertSame( + '2020-05-25', + $this->strategy->hydrate('2020-05-25')->format('Y-m-d') + ); } #[DataProvider('dataProviderForInvalidDateValues')] public function testHydrationShouldReturnInvalidDateValuesAsIs(string|null $value): void { - $this->assertSame($value, $this->strategy->hydrate($value)); + self::assertSame($value, $this->strategy->hydrate($value)); } - /** @return Iterator */ - public static function dataProviderForInvalidDateValues(): Iterator + /** @return array */ + public static function dataProviderForInvalidDateValues(): array { - yield 'null' => [null]; - yield 'empty-string' => ['']; - yield 'foo' => ['foo']; + return [ + 'null' => [null], + 'empty-string' => [''], + 'foo' => ['foo'], + ]; } } diff --git a/test/Strategy/ExplodeStrategyTest.php b/test/Strategy/ExplodeStrategyTest.php index 4563c066..c0e3fd5a 100644 --- a/test/Strategy/ExplodeStrategyTest.php +++ b/test/Strategy/ExplodeStrategyTest.php @@ -4,7 +4,6 @@ namespace LaminasTest\Hydrator\Strategy; -use Iterator; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; use Laminas\Hydrator\Strategy\ExplodeStrategy; use PHPUnit\Framework\Attributes\CoversClass; @@ -27,9 +26,9 @@ public function testExtract(mixed $expected, string $delimiter, array $extractVa $strategy = new ExplodeStrategy($delimiter); if (is_numeric($expected)) { - $this->assertEquals($expected, $strategy->extract($extractValue)); + self::assertEquals($expected, $strategy->extract($extractValue)); } else { - $this->assertSame($expected, $strategy->extract($extractValue)); + self::assertSame($expected, $strategy->extract($extractValue)); } } @@ -47,7 +46,7 @@ public function testGetEmptyArrayWhenHydratingNullValue(): void { $strategy = new ExplodeStrategy(); - $this->assertSame([], $strategy->hydrate(null)); + self::assertSame([], $strategy->hydrate(null)); } public function testGetExceptionWithEmptyDelimiter(): void @@ -61,10 +60,10 @@ public function testGetExceptionWithEmptyDelimiter(): void public function testHydrateWithExplodeLimit(): void { $strategy = new ExplodeStrategy('-', 2); - $this->assertSame(['foo', 'bar-baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); + self::assertSame(['foo', 'bar-baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); $strategy = new ExplodeStrategy('-', 3); - $this->assertSame(['foo', 'bar', 'baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); + self::assertSame(['foo', 'bar', 'baz-bat'], $strategy->hydrate('foo-bar-baz-bat')); } public function testHydrateWithInvalidScalarType(): void @@ -115,27 +114,29 @@ public function testHydration(mixed $value, string $delimiter, array $expected): { $strategy = new ExplodeStrategy($delimiter); - $this->assertSame($expected, $strategy->hydrate($value)); + self::assertSame($expected, $strategy->hydrate($value)); } /** - * @return Iterator}> + * @return array}> */ - public static function getValidHydratedValues(): Iterator + public static function getValidHydratedValues(): array { - // @codingStandardsIgnoreEnd // @codingStandardsIgnoreStart - yield 'null-comma' => [null, ',', []]; - yield 'empty-comma' => ['', ',', ['']]; - yield 'string without delimiter-comma' => ['foo', ',', ['foo']]; - yield 'string with delimiter-comma' => ['foo,bar', ',', ['foo', 'bar']]; - yield 'string with delimiter-period' => ['foo.bar', '.', ['foo', 'bar']]; - yield 'string with mismatched delimiter-comma' => ['foo.bar', ',', ['foo.bar']]; - yield 'integer-comma' => [123, ',', ['123']]; - yield 'integer-numeric delimiter' => [123, '2', ['1', '3']]; - yield 'integer with mismatched delimiter-comma' => [123.456, ',', ['123.456']]; - yield 'float-period' => [123.456, '.', ['123', '456']]; - yield 'string containing null-comma' => ['foo,bar,dev,null', ',', ['foo', 'bar', 'dev', 'null']]; - yield 'string containing null-semicolon' => ['foo;bar;dev;null', ';', ['foo', 'bar', 'dev', 'null']]; + return [ + 'null-comma' => [null, ',', []], + 'empty-comma' => ['', ',', ['']], + 'string without delimiter-comma' => ['foo', ',', ['foo']], + 'string with delimiter-comma' => ['foo,bar', ',', ['foo', 'bar']], + 'string with delimiter-period' => ['foo.bar', '.', ['foo', 'bar']], + 'string with mismatched delimiter-comma' => ['foo.bar', ',', ['foo.bar']], + 'integer-comma' => [123, ',', ['123']], + 'integer-numeric delimiter' => [123, '2', ['1', '3']], + 'integer with mismatched delimiter-comma' => [123.456, ',', ['123.456']], + 'float-period' => [123.456, '.', ['123', '456']], + 'string containing null-comma' => ['foo,bar,dev,null', ',', ['foo', 'bar', 'dev', 'null']], + 'string containing null-semicolon' => ['foo;bar;dev;null', ';', ['foo', 'bar', 'dev', 'null']], + ]; + // @codingStandardsIgnoreEnd } } diff --git a/test/Strategy/HydratorStrategyTest.php b/test/Strategy/HydratorStrategyTest.php index 7ff4ed17..ae631252 100644 --- a/test/Strategy/HydratorStrategyTest.php +++ b/test/Strategy/HydratorStrategyTest.php @@ -5,7 +5,6 @@ namespace LaminasTest\Hydrator\Strategy; use Generator; -use Iterator; use Laminas\Hydrator\HydratorInterface; use Laminas\Hydrator\ReflectionHydrator; use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException; @@ -48,21 +47,23 @@ public function testConstructorRejectsInvalidObjectClassName( ); } - /** @return Iterator, string}> */ - public static function providerInvalidObjectClassName(): Iterator + /** @return non-empty-array, string}> */ + public static function providerInvalidObjectClassName(): array { - yield 'array' => [[], TypeError::class, 'type string']; - yield 'boolean-false' => [false, TypeError::class, 'type string']; - yield 'boolean-true' => [true, TypeError::class, 'type string']; - yield 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string']; - yield 'integer' => [mt_rand(), TypeError::class, 'type string']; - yield 'null' => [null, TypeError::class, 'type string']; - yield 'object' => [new stdClass(), TypeError::class, 'type string']; - yield 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string']; - yield 'string-non-existent-class' => [ - 'FooBarBaz9000', - InvalidArgumentException::class, - 'class name needs to be the name of an existing class', + return [ + 'array' => [[], TypeError::class, 'type string'], + 'boolean-false' => [false, TypeError::class, 'type string'], + 'boolean-true' => [true, TypeError::class, 'type string'], + 'float' => [mt_rand() / mt_getrandmax(), TypeError::class, 'type string'], + 'integer' => [mt_rand(), TypeError::class, 'type string'], + 'null' => [null, TypeError::class, 'type string'], + 'object' => [new stdClass(), TypeError::class, 'type string'], + 'resource' => [fopen(__FILE__, 'r'), TypeError::class, 'type string'], + 'string-non-existent-class' => [ + 'FooBarBaz9000', + InvalidArgumentException::class, + 'class name needs to be the name of an existing class', + ], ]; } @@ -158,7 +159,7 @@ public function testExtractUsesHydratorToExtractValues(): void $hydrator = $this->createHydratorMock(); - $hydrator->expects($this->once()) + $hydrator->expects(self::once()) ->method('extract') ->willReturnCallback($extraction); @@ -167,7 +168,7 @@ public function testExtractUsesHydratorToExtractValues(): void TestAsset\User::class ); - $this->assertSame($extraction($value), $strategy->extract($value)); + self::assertSame($extraction($value), $strategy->extract($value)); } #[DataProvider('providerInvalidValueForHydration')] @@ -215,7 +216,7 @@ public function testHydrateShouldReturnEmptyOrSameObjects(mixed $value): void TestAsset\User::class ); - $this->assertSame($value, $strategy->hydrate($value)); + self::assertSame($value, $strategy->hydrate($value)); } /** @return Generator */ @@ -236,7 +237,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void { $value = ['name' => 'John Doe']; - $hydration = static function ($data) { + $hydration = static function ($data): mixed { static $hydrator; if (null === $hydrator) { @@ -251,7 +252,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void $hydrator = $this->createHydratorMock(); - $hydrator->expects($this->exactly(count($value))) + $hydrator->expects(self::exactly(count($value))) ->method('hydrate') ->willReturnCallback($hydration); @@ -260,7 +261,7 @@ public function testHydrateUsesHydratorToHydrateValues(): void TestAsset\User::class ); - $this->assertEquals($hydration($value), $strategy->hydrate($value)); + self::assertEquals($hydration($value), $strategy->hydrate($value)); } private function createHydratorMock(): MockObject&HydratorInterface diff --git a/test/Strategy/NullableStrategyTest.php b/test/Strategy/NullableStrategyTest.php index c18851d9..608632a1 100644 --- a/test/Strategy/NullableStrategyTest.php +++ b/test/Strategy/NullableStrategyTest.php @@ -15,89 +15,89 @@ class NullableStrategyTest extends TestCase public function testExtractNonNullAndNonEmptyValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->once()) + $strategy->expects(self::once()) ->method('extract') ->with('original value') ->willReturn('extracted value'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertEquals('extracted value', $nullableStrategy->extract('original value')); + self::assertEquals('extracted value', $nullableStrategy->extract('original value')); } public function testExtractNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->never()) + $strategy->expects(self::never()) ->method('extract'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertNull($nullableStrategy->extract(null)); + self::assertNull($nullableStrategy->extract(null)); } public function testExtractEmptyValueAsNull(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->never()) + $strategy->expects(self::never()) ->method('extract'); $nullableStrategy = new NullableStrategy($strategy, true); - $this->assertNull($nullableStrategy->extract('')); + self::assertNull($nullableStrategy->extract('')); } public function testExtractEmptyValueByHydrator(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->once()) + $strategy->expects(self::once()) ->method('extract') ->with('') ->willReturn('extracted empty value'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertEquals('extracted empty value', $nullableStrategy->extract('')); + self::assertEquals('extracted empty value', $nullableStrategy->extract('')); } public function testHydrateNonNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->once()) + $strategy->expects(self::once()) ->method('hydrate') ->with('original value') ->willReturn('hydrated value'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertEquals('hydrated value', $nullableStrategy->hydrate('original value')); + self::assertEquals('hydrated value', $nullableStrategy->hydrate('original value')); } public function testHydrateNullValue(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->never()) + $strategy->expects(self::never()) ->method('hydrate'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertNull($nullableStrategy->hydrate(null)); + self::assertNull($nullableStrategy->hydrate(null)); } public function testHydrateEmptyValueAsNull(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->never()) + $strategy->expects(self::never()) ->method('hydrate'); $nullableStrategy = new NullableStrategy($strategy, true); - $this->assertNull($nullableStrategy->hydrate('')); + self::assertNull($nullableStrategy->hydrate('')); } public function testHydrateEmptyValueByHydrator(): void { $strategy = $this->createMock(StrategyInterface::class); - $strategy->expects($this->once()) + $strategy->expects(self::once()) ->method('hydrate') ->with('') ->willReturn('hydrated empty value'); $nullableStrategy = new NullableStrategy($strategy, false); - $this->assertEquals('hydrated empty value', $nullableStrategy->hydrate('')); + self::assertEquals('hydrated empty value', $nullableStrategy->hydrate('')); } } diff --git a/test/Strategy/ScalarTypeStrategyTest.php b/test/Strategy/ScalarTypeStrategyTest.php index ebf4c88b..c33a10a9 100644 --- a/test/Strategy/ScalarTypeStrategyTest.php +++ b/test/Strategy/ScalarTypeStrategyTest.php @@ -8,8 +8,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use const PHP_FLOAT_EPSILON; - #[CoversClass(ScalarTypeStrategy::class)] class ScalarTypeStrategyTest extends TestCase { @@ -17,11 +15,7 @@ public function testHydrate(): void { $this->assertSame(123, ScalarTypeStrategy::createToInt()->hydrate('123', null)); $this->assertNull(ScalarTypeStrategy::createToInt()->hydrate(null, null)); - $this->assertEqualsWithDelta( - 123.99, - ScalarTypeStrategy::createToFloat()->hydrate('123.99', null), - PHP_FLOAT_EPSILON - ); + $this->assertSame(123.99, ScalarTypeStrategy::createToFloat()->hydrate('123.99', null)); $this->assertTrue(ScalarTypeStrategy::createToBoolean()->hydrate(1, null)); $this->assertFalse(ScalarTypeStrategy::createToBoolean()->hydrate(0, null)); @@ -37,9 +31,9 @@ public function __toString(): string public function testExtract(): void { $this->assertSame(123, ScalarTypeStrategy::createToInt()->extract(123)); - $this->assertEqualsWithDelta(123.99, ScalarTypeStrategy::createToFloat()->extract(123.99), PHP_FLOAT_EPSILON); + $this->assertSame(123.99, ScalarTypeStrategy::createToFloat()->extract(123.99)); $this->assertSame('foo', ScalarTypeStrategy::createToString()->extract('foo')); - $this->assertTrue(ScalarTypeStrategy::createToBoolean()->extract(true)); - $this->assertFalse(ScalarTypeStrategy::createToBoolean()->extract(false)); + $this->assertSame(true, ScalarTypeStrategy::createToBoolean()->extract(true)); + $this->assertSame(false, ScalarTypeStrategy::createToBoolean()->extract(false)); } } diff --git a/test/Strategy/SerializableStrategyTest.php b/test/Strategy/SerializableStrategyTest.php index 8970b497..9a69c40a 100644 --- a/test/Strategy/SerializableStrategyTest.php +++ b/test/Strategy/SerializableStrategyTest.php @@ -17,7 +17,7 @@ public function testCanSerialize(): void $serializer = new PhpSerialize(); $serializerStrategy = new SerializableStrategy($serializer); $serialized = $serializerStrategy->extract('foo'); - $this->assertSame('s:3:"foo";', $serialized); + $this->assertEquals($serialized, 's:3:"foo";'); } public function testCanUnserialize(): void @@ -25,6 +25,6 @@ public function testCanUnserialize(): void $serializer = new PhpSerialize(); $serializerStrategy = new SerializableStrategy($serializer); $serialized = $serializerStrategy->hydrate('s:3:"foo";'); - $this->assertEquals('foo', $serialized); + $this->assertEquals($serialized, 'foo'); } } From ddcae44180a213325d2000c5b11c16ac9df3419f Mon Sep 17 00:00:00 2001 From: mmalac Date: Wed, 23 Jul 2025 21:47:30 +0200 Subject: [PATCH 16/16] refactor: update type annotations in CollectionStrategy Signed-off-by: mmalac --- src/Strategy/CollectionStrategy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Strategy/CollectionStrategy.php b/src/Strategy/CollectionStrategy.php index 81c442ab..1e4d8e9f 100644 --- a/src/Strategy/CollectionStrategy.php +++ b/src/Strategy/CollectionStrategy.php @@ -38,7 +38,7 @@ public function __construct( /** * Converts the given value so that it can be extracted by the hydrator. * - * @param object[] $value The original value. + * @param array $value The original value. * @throws Exception\InvalidArgumentException * @return mixed Returns the value that should be extracted. */ @@ -67,9 +67,9 @@ public function extract(mixed $value, ?object $object = null): array /** * Converts the given value so that it can be hydrated by the hydrator. * - * @param mixed[] $value The original value. + * @param mixed[] $value The original value. * @throws Exception\InvalidArgumentException - * @return object[] Returns the value that should be hydrated. + * @return array Returns the value that should be hydrated. */ public function hydrate($value, ?array $data = null): array {