|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Shopware\PhpStan\Type; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Analyser\SpecifiedTypes; |
| 8 | +use PHPStan\Analyser\TypeSpecifier; |
| 9 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 10 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 11 | +use PHPStan\Reflection\MethodReflection; |
| 12 | +use PHPStan\Type\MethodTypeSpecifyingExtension; |
| 13 | +use PHPStan\Type\NullType; |
| 14 | +use PHPStan\Type\TypeCombinator; |
| 15 | +use Shopware\Core\Framework\Struct\Collection; |
| 16 | + |
| 17 | +/** |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +class CollectionHasSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 21 | +{ |
| 22 | + private TypeSpecifier $typeSpecifier; |
| 23 | + |
| 24 | + public function getClass(): string |
| 25 | + { |
| 26 | + return Collection::class; |
| 27 | + } |
| 28 | + |
| 29 | + public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool |
| 30 | + { |
| 31 | + $declaringClass = $methodReflection->getDeclaringClass(); |
| 32 | + |
| 33 | + return ( |
| 34 | + $declaringClass->getName() === Collection::class |
| 35 | + || $declaringClass->isSubclassOf(Collection::class) |
| 36 | + ) |
| 37 | + && $methodReflection->getName() === 'has' && !$context->null(); |
| 38 | + } |
| 39 | + |
| 40 | + public function specifyTypes( |
| 41 | + MethodReflection $methodReflection, |
| 42 | + MethodCall $node, |
| 43 | + Scope $scope, |
| 44 | + TypeSpecifierContext $context, |
| 45 | + ): SpecifiedTypes { |
| 46 | + $getExpr = new MethodCall($node->var, 'get', $node->args); |
| 47 | + |
| 48 | + $getterTypes = $this->typeSpecifier->create( |
| 49 | + $getExpr, |
| 50 | + TypeCombinator::removeNull($scope->getType($getExpr)), |
| 51 | + $context, |
| 52 | + $scope, |
| 53 | + ); |
| 54 | + |
| 55 | + return $getterTypes->unionWith( |
| 56 | + $this->typeSpecifier->create( |
| 57 | + $getExpr, |
| 58 | + new NullType(), |
| 59 | + $context->negate(), |
| 60 | + $scope, |
| 61 | + ), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 66 | + { |
| 67 | + $this->typeSpecifier = $typeSpecifier; |
| 68 | + } |
| 69 | +} |
0 commit comments