|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Type; |
| 15 | + |
| 16 | +use CodeIgniter\Model; |
| 17 | +use PhpParser\Node\Expr\MethodCall; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\Reflection\ClassReflection; |
| 20 | +use PHPStan\Reflection\MethodReflection; |
| 21 | +use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 22 | +use PHPStan\Type\ArrayType; |
| 23 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 24 | +use PHPStan\Type\IntegerType; |
| 25 | +use PHPStan\Type\Type; |
| 26 | +use PHPStan\Type\TypeCombinator; |
| 27 | + |
| 28 | +final class ModelFindReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly ModelFetchedReturnTypeHelper $modelFetchedReturnTypeHelper |
| 32 | + ) {} |
| 33 | + |
| 34 | + public function getClass(): string |
| 35 | + { |
| 36 | + return Model::class; |
| 37 | + } |
| 38 | + |
| 39 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 40 | + { |
| 41 | + return in_array($methodReflection->getName(), ['find', 'findAll', 'first'], true); |
| 42 | + } |
| 43 | + |
| 44 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 45 | + { |
| 46 | + $methodName = $methodReflection->getName(); |
| 47 | + |
| 48 | + if ($methodName === 'find') { |
| 49 | + return $this->getTypeFromFind($methodReflection, $methodCall, $scope); |
| 50 | + } |
| 51 | + |
| 52 | + if ($methodName === 'findAll') { |
| 53 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 54 | + } |
| 55 | + |
| 56 | + $classReflection = $this->getClassReflection($methodCall, $scope); |
| 57 | + |
| 58 | + return TypeCombinator::addNull($this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope)); |
| 59 | + } |
| 60 | + |
| 61 | + private function getClassReflection(MethodCall $methodCall, Scope $scope): ClassReflection |
| 62 | + { |
| 63 | + $classTypes = $scope->getType($methodCall->var)->getObjectClassReflections(); |
| 64 | + assert(count($classTypes) === 1); |
| 65 | + |
| 66 | + return current($classTypes); |
| 67 | + } |
| 68 | + |
| 69 | + private function getTypeFromFind(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 70 | + { |
| 71 | + $args = $methodCall->getArgs(); |
| 72 | + |
| 73 | + if (! isset($args[0])) { |
| 74 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 75 | + } |
| 76 | + |
| 77 | + $idType = $scope->getType($args[0]->value); |
| 78 | + |
| 79 | + if ($idType->isNull()->yes()) { |
| 80 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 81 | + } |
| 82 | + |
| 83 | + if ($idType->isInteger()->yes() || $idType->isString()->yes()) { |
| 84 | + $classReflection = $this->getClassReflection($methodCall, $scope); |
| 85 | + |
| 86 | + return TypeCombinator::addNull($this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope)); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->getTypeFromFindAll($methodReflection, $methodCall, $scope); |
| 90 | + } |
| 91 | + |
| 92 | + private function getTypeFromFindAll(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 93 | + { |
| 94 | + $classReflection = $this->getClassReflection($methodCall, $scope); |
| 95 | + |
| 96 | + return AccessoryArrayListType::intersectWith( |
| 97 | + new ArrayType( |
| 98 | + new IntegerType(), |
| 99 | + $this->modelFetchedReturnTypeHelper->getFetchedReturnType($classReflection, $scope) |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments