|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SaschaEgerer\PhpstanTypo3\Type; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 10 | +use PHPStan\Type\IntegerType; |
| 11 | +use PHPStan\Type\MixedType; |
| 12 | +use PHPStan\Type\StringType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use TYPO3\CMS\Extbase\Persistence\ObjectStorage; |
| 15 | + |
| 16 | +class ObjectStorageDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 17 | +{ |
| 18 | + |
| 19 | + public function getClass(): string |
| 20 | + { |
| 21 | + return ObjectStorage::class; |
| 22 | + } |
| 23 | + |
| 24 | + public function isMethodSupported( |
| 25 | + MethodReflection $methodReflection |
| 26 | + ): bool |
| 27 | + { |
| 28 | + return $methodReflection->getName() === 'offsetGet'; |
| 29 | + } |
| 30 | + |
| 31 | + public function getTypeFromMethodCall( |
| 32 | + MethodReflection $methodReflection, |
| 33 | + MethodCall $methodCall, |
| 34 | + Scope $scope |
| 35 | + ): ?Type |
| 36 | + { |
| 37 | + $firstArgument = $methodCall->args[0] ?? null; |
| 38 | + |
| 39 | + if (!$firstArgument instanceof Arg) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + $argumentType = $scope->getType($firstArgument->value); |
| 44 | + |
| 45 | + if ((new StringType())->isSuperTypeOf($argumentType)->yes()) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + if ((new IntegerType())->isSuperTypeOf($argumentType)->yes()) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + return new MixedType(); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments