From c2bc215b1b06a3c63a746527483c3fc869da1a53 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Sat, 13 Jan 2024 17:07:43 +0700 Subject: [PATCH] chore: Fix static analysis issues. --- CHANGELOG.md | 2 ++ phpstan.neon.dist | 4 ++++ .../Model/TraversableCountableWrapper.php | 13 +++++++++++-- .../MethodMapper/ObjectWithArrayPropertyDto.php | 4 +--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 123c946f..2cae333f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * refactor: Refactor exception. * feat: Add attribute matching. * refactor: Simplify object caching. +* refactor: Remove `$context` from `MapperInterface` +* chore: Fix static analysis issues. ## 0.5.4 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 54446afe..09d11da2 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,6 +8,9 @@ parameters: checkFunctionNameCase: true checkInternalClassCaseSensitivity: true reportMaybesInPropertyPhpDocTypes: true + treatPhpDocTypesAsCertain: false + ignoreErrors: + - '#with type mixed is not subtype of native type mixed#' includes: - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon @@ -15,3 +18,4 @@ includes: - vendor/bnf/phpstan-psr-container/extension.neon - vendor/ekino/phpstan-banned-code/extension.neon - vendor/dave-liddament/phpstan-php-language-extensions/extension.neon + - phar://phpstan.phar/conf/bleedingEdge.neon diff --git a/src/Transformer/Model/TraversableCountableWrapper.php b/src/Transformer/Model/TraversableCountableWrapper.php index 0f0dd7fa..ff80f6a8 100644 --- a/src/Transformer/Model/TraversableCountableWrapper.php +++ b/src/Transformer/Model/TraversableCountableWrapper.php @@ -34,12 +34,21 @@ public function getIterator(): \Traversable return $this->traversable; } + /** + * @return int<0,max> + */ public function count(): int { if (is_int($this->countable)) { - return $this->countable; + $result = $this->countable; + } else { + $result = $this->countable->count(); + } + + if ($result < 0) { + throw new \LogicException('Countable must return positive integer.'); } - return $this->countable->count(); + return $result; } } diff --git a/tests/Fixtures/MethodMapper/ObjectWithArrayPropertyDto.php b/tests/Fixtures/MethodMapper/ObjectWithArrayPropertyDto.php index f25dd400..73e66f12 100644 --- a/tests/Fixtures/MethodMapper/ObjectWithArrayPropertyDto.php +++ b/tests/Fixtures/MethodMapper/ObjectWithArrayPropertyDto.php @@ -33,6 +33,7 @@ public static function mapFromObject( $result = new self(); + /** @var array|null $property */ $property = $mapper->mapForProperty( $source->property, ObjectWithArrayPropertyDto::class, @@ -40,9 +41,6 @@ public static function mapFromObject( $context ); - assert(is_array($property)); - - /** @psalm-suppress MixedPropertyTypeCoercion */ $result->property = $property; return $result;