From 970d4e2f04ca068a2db36cb6856a95bcaf82f1e4 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Tue, 4 Apr 2023 18:14:57 +0200 Subject: [PATCH] fixes for latest PHPStan --- src/Collection/DbalCollection.php | 2 -- .../Functions/CompareEqualsFunction.php | 3 +-- .../Functions/CompareNotEqualsFunction.php | 3 +-- src/Collection/HasManyCollection.php | 4 ++-- src/Entity/AbstractEntity.php | 4 ++-- src/Entity/Reflection/ModifierParser.php | 5 +---- src/Relationships/HasMany.php | 19 +++++++++-------- src/Relationships/IRelationshipCollection.php | 2 +- src/Relationships/ManyHasMany.php | 21 ++++++++++--------- src/Relationships/OneHasMany.php | 13 +++++------- .../phpstan/AssertTypeSpecifierExtension.php | 7 ++++--- 11 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/Collection/DbalCollection.php b/src/Collection/DbalCollection.php index ddb78e02..ef73c994 100644 --- a/src/Collection/DbalCollection.php +++ b/src/Collection/DbalCollection.php @@ -139,8 +139,6 @@ public function orderBy($expression, string $direction = ICollection::ASC): ICol ]; } } else { - /** @phpstan-var string|list $expression */ - $expression = $expression; // no-op for PHPStan $collection->ordering[] = [ $helper->processPropertyExpr($collection->queryBuilder, $expression), $direction, diff --git a/src/Collection/Functions/CompareEqualsFunction.php b/src/Collection/Functions/CompareEqualsFunction.php index d3e70439..c139da3f 100644 --- a/src/Collection/Functions/CompareEqualsFunction.php +++ b/src/Collection/Functions/CompareEqualsFunction.php @@ -40,9 +40,8 @@ protected function evaluateInDb(DbalExpressionResult $expression, $value, string $columns[] = $column . $modifiers[$i]; } $value = array_map(function ($value) use ($columns): array { - /** @var array|false $combined */ $combined = array_combine($columns, $value); - if ($combined === false) { + if ($combined === false) { // @phpstan-ignore-line $pn = count($columns); $vn = count($value); throw new InvalidArgumentException("Number of values ($vn) does not match number of properties ($pn)."); diff --git a/src/Collection/Functions/CompareNotEqualsFunction.php b/src/Collection/Functions/CompareNotEqualsFunction.php index 415f8e77..e950ffb0 100644 --- a/src/Collection/Functions/CompareNotEqualsFunction.php +++ b/src/Collection/Functions/CompareNotEqualsFunction.php @@ -40,9 +40,8 @@ protected function evaluateInDb(DbalExpressionResult $expression, $value, string $columns[] = $column . $modifiers[$i]; } $value = array_map(function ($value) use ($columns): array { - /** @var array|false $combined */ $combined = array_combine($columns, $value); - if ($combined === false) { + if ($combined === false) { // @phpstan-ignore-line $pn = count($columns); $vn = count($value); throw new InvalidArgumentException("Number of values ($vn) does not match number of properties ($pn)."); diff --git a/src/Collection/HasManyCollection.php b/src/Collection/HasManyCollection.php index a930b09d..aecdcfc3 100644 --- a/src/Collection/HasManyCollection.php +++ b/src/Collection/HasManyCollection.php @@ -47,7 +47,7 @@ class HasManyCollection implements ICollection /** * @var callable A callback returning a list entities to add & remove. - * @phpstan-var callable(): array{array, array} + * @phpstan-var callable(): array{array, array} */ private $diffCallback; @@ -58,7 +58,7 @@ class HasManyCollection implements ICollection /** * @phpstan-param IRepository $repository * @phpstan-param ICollection $innerCollection - * @phpstan-param callable():array{array, array} $diffCallback + * @phpstan-param callable():array{array, array} $diffCallback */ public function __construct( IRepository $repository, diff --git a/src/Entity/AbstractEntity.php b/src/Entity/AbstractEntity.php index 0d17fe0d..65916ee9 100644 --- a/src/Entity/AbstractEntity.php +++ b/src/Entity/AbstractEntity.php @@ -399,7 +399,7 @@ public function onAfterRemove(): void * @param mixed $value * @return mixed */ - private function setterPrimaryProxy($value, PropertyMetadata $metadata) + private function setterPrimaryProxy($value, PropertyMetadata $metadata) // @phpstan-ignore-line { $keys = $this->metadata->getPrimaryKey(); if (!$metadata->isVirtual) { @@ -429,7 +429,7 @@ private function setterPrimaryProxy($value, PropertyMetadata $metadata) * @param mixed $value * @return mixed */ - private function getterPrimaryProxy($value, PropertyMetadata $metadata) + private function getterPrimaryProxy($value, PropertyMetadata $metadata) // @phpstan-ignore-line { if ($this->persistedId !== null) { return $this->persistedId; diff --git a/src/Entity/Reflection/ModifierParser.php b/src/Entity/Reflection/ModifierParser.php index 65e9180a..f22fd941 100644 --- a/src/Entity/Reflection/ModifierParser.php +++ b/src/Entity/Reflection/ModifierParser.php @@ -217,10 +217,7 @@ private function processKeyword(string $value, ReflectionClass $reflectionClass) } elseif (strcasecmp($value, 'null') === 0) { return null; } elseif (is_numeric($value)) { - // hack for phpstan - /** @var int $val */ - $val = $value; - return $val * 1; + return $value * 1; } elseif (preg_match('#^[a-z0-9_\\\\]+::[a-z0-9_]*(\\*)?$#i', $value) === 1) { [$className, $const] = explode('::', $value, 2); if ($className === 'self' || $className === 'static') { diff --git a/src/Relationships/HasMany.php b/src/Relationships/HasMany.php index ea5a4852..c7f38c5e 100644 --- a/src/Relationships/HasMany.php +++ b/src/Relationships/HasMany.php @@ -50,19 +50,19 @@ abstract class HasMany implements IRelationshipCollection /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $toAdd = []; /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $toRemove = []; /** * @var IEntity[] - * @phpstan-var array + * @phpstan-var array */ protected $tracked = []; @@ -348,14 +348,15 @@ protected function getCollection(bool $forceNew = false): ICollection } if (count($this->toAdd) > 0 || count($this->toRemove) > 0) { - /** @phpstan-var callable():array{array, array} $diffCb */ - $diffCb = function (): array { - return [$this->toAdd, $this->toRemove]; - }; - $collection = $collection->resetOrderBy(); /** @var ICollection $collection */ - $collection = new HasManyCollection($this->getTargetRepository(), $collection, $diffCb); + $collection = new HasManyCollection( + $this->getTargetRepository(), + $collection, + function (): array { + return [$this->toAdd, $this->toRemove]; + } + ); $collection = $this->applyDefaultOrder($collection); } diff --git a/src/Relationships/IRelationshipCollection.php b/src/Relationships/IRelationshipCollection.php index e29a6415..fd623048 100644 --- a/src/Relationships/IRelationshipCollection.php +++ b/src/Relationships/IRelationshipCollection.php @@ -88,7 +88,7 @@ public function trackEntity(IEntity $entity): void; /** * Returns IEntity for persistence. * @return IEntity[] - * @phpstan-return array + * @phpstan-return array * @ignore * @internal */ diff --git a/src/Relationships/ManyHasMany.php b/src/Relationships/ManyHasMany.php index 1057df49..62696aac 100644 --- a/src/Relationships/ManyHasMany.php +++ b/src/Relationships/ManyHasMany.php @@ -66,22 +66,23 @@ protected function modify(): void protected function createCollection(): ICollection { - /** @phpstan-var callable(Traversable):void $subscribeCb */ - $subscribeCb = function (Traversable $entities): void { + $mapper = $this->parent->getRepository()->getMapper(); + + /** @var ICollection $collection */ + $collection = $this->getTargetRepository()->getMapper()->createCollectionManyHasMany($mapper, $this->metadata); + $collection = $collection->setRelationshipParent($this->parent); + $collection->subscribeOnEntityFetch(function (Traversable $entities): void { if ($this->metadataRelationship->property === null) { return; } foreach ($entities as $entity) { - $entity->getProperty($this->metadataRelationship->property)->trackEntity($this->parent); + assert($this->metadataRelationship->property !== null); + $property = $entity->getProperty($this->metadataRelationship->property); + assert($property instanceof HasMany); + $property->trackEntity($this->parent); $this->trackEntity($entity); } - }; - $mapper = $this->parent->getRepository()->getMapper(); - - /** @var ICollection $collection */ - $collection = $this->getTargetRepository()->getMapper()->createCollectionManyHasMany($mapper, $this->metadata); - $collection = $collection->setRelationshipParent($this->parent); - $collection->subscribeOnEntityFetch($subscribeCb); + }); return $this->applyDefaultOrder($collection); } diff --git a/src/Relationships/OneHasMany.php b/src/Relationships/OneHasMany.php index 38673da7..c96c705e 100644 --- a/src/Relationships/OneHasMany.php +++ b/src/Relationships/OneHasMany.php @@ -57,17 +57,14 @@ protected function modify(): void protected function createCollection(): ICollection { - /** @phpstan-var callable(\Traversable):void $subscribeCb */ - $subscribeCb = function ($entities): void { - foreach ($entities as $entity) { - $this->trackEntity($entity); - } - }; - /** @var ICollection $collection */ $collection = $this->getTargetRepository()->getMapper()->createCollectionOneHasMany($this->metadata); $collection = $collection->setRelationshipParent($this->parent); - $collection->subscribeOnEntityFetch($subscribeCb); + $collection->subscribeOnEntityFetch(function ($entities): void { + foreach ($entities as $entity) { + $this->trackEntity($entity); + } + }); return $this->applyDefaultOrder($collection); } diff --git a/tests/inc/phpstan/AssertTypeSpecifierExtension.php b/tests/inc/phpstan/AssertTypeSpecifierExtension.php index d500d1d3..e659dd52 100644 --- a/tests/inc/phpstan/AssertTypeSpecifierExtension.php +++ b/tests/inc/phpstan/AssertTypeSpecifierExtension.php @@ -11,9 +11,9 @@ use PHPStan\Analyser\TypeSpecifierContext; use PHPStan\Reflection\MethodReflection; use PHPStan\ShouldNotHappenException; -use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\StaticMethodTypeSpecifyingExtension; use Tester\Assert; +use function count; class AssertTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension @@ -66,13 +66,14 @@ public function specifyTypes( $class = $node->getArgs()[0]; $classType = $scope->getType($class->value); - if (!$classType instanceof ConstantStringType) { + $value = $classType->getConstantStrings(); + if (count($value) !== 1) { return new \PHPStan\Analyser\SpecifiedTypes(); } $expression = new \PhpParser\Node\Expr\Instanceof_( $expr->value, - new \PhpParser\Node\Name($classType->getValue()) + new \PhpParser\Node\Name($value[0]->getValue()) ); } else { throw new ShouldNotHappenException();