|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2025, Cake Development Corporation (https://www.cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2025, Cake Development Corporation (https://www.cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\PHPStan\Type; |
| 15 | + |
| 16 | +use Cake\ORM\Association; |
| 17 | +use Cake\ORM\Query\SelectQuery; |
| 18 | +use CakeDC\PHPStan\Traits\EntityClassFromTableClassTrait; |
| 19 | +use CakeDC\PHPStan\Traits\RepositoryReferenceTrait; |
| 20 | +use PhpParser\Node\Expr\MethodCall; |
| 21 | +use PHPStan\Analyser\Scope; |
| 22 | +use PHPStan\Reflection\MethodReflection; |
| 23 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 24 | +use PHPStan\Type\Generic\GenericObjectType; |
| 25 | +use PHPStan\Type\ObjectType; |
| 26 | +use PHPStan\Type\Type; |
| 27 | + |
| 28 | +/** |
| 29 | + * Narrows the return type of {@see Association::find()} to |
| 30 | + * `SelectQuery<TargetEntity>`. |
| 31 | + * |
| 32 | + * Cake core declares `Association::find()` as |
| 33 | + * `\Cake\ORM\Query\SelectQuery<EntityInterface|array>` — it does not propagate |
| 34 | + * the target table's `TEntity` template parameter. As a result, chains such as |
| 35 | + * `$this->Articles->Users->find()->first()` resolve to `EntityInterface|null` |
| 36 | + * instead of `User|null`, forcing every call-site to add inline `@var` |
| 37 | + * annotations. |
| 38 | + * |
| 39 | + * This extension reads the association's target table type — once |
| 40 | + * {@see \CakeDC\PHPStan\PhpDoc\TableAssociationTypeNodeResolverExtension} has |
| 41 | + * converted the intersection `BelongsTo&UsersTable` into the generic |
| 42 | + * `BelongsTo<UsersTable>` — derives the entity class via the standard CakePHP |
| 43 | + * naming convention, and replaces the return type with |
| 44 | + * `SelectQuery<UserEntity>`. |
| 45 | + * |
| 46 | + * Hydration-disabled queries are not detected here; PHPStan cannot follow |
| 47 | + * `$query->disableHydration()` calls regardless of which extension produces |
| 48 | + * the type, so narrowing to the entity is at least as accurate as the current |
| 49 | + * `EntityInterface|array` union and strictly better for the common hydrated |
| 50 | + * case. |
| 51 | + */ |
| 52 | +class AssociationFindDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 53 | +{ |
| 54 | + use EntityClassFromTableClassTrait; |
| 55 | + use RepositoryReferenceTrait; |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritDoc |
| 59 | + */ |
| 60 | + public function getClass(): string |
| 61 | + { |
| 62 | + return Association::class; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritDoc |
| 67 | + */ |
| 68 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 69 | + { |
| 70 | + return $methodReflection->getName() === 'find'; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param \PHPStan\Reflection\MethodReflection $methodReflection |
| 75 | + * @param \PhpParser\Node\Expr\MethodCall $methodCall |
| 76 | + * @param \PHPStan\Analyser\Scope $scope |
| 77 | + * @return \PHPStan\Type\Type|null |
| 78 | + */ |
| 79 | + public function getTypeFromMethodCall( |
| 80 | + MethodReflection $methodReflection, |
| 81 | + MethodCall $methodCall, |
| 82 | + Scope $scope, |
| 83 | + ): ?Type { |
| 84 | + $tableClass = $this->getReferenceClass($scope, $methodCall); |
| 85 | + if ($tableClass === null) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $entityClass = $this->getEntityClassByTableClass($tableClass); |
| 90 | + if ($entityClass === null || !class_exists($entityClass)) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + return new GenericObjectType(SelectQuery::class, [new ObjectType($entityClass)]); |
| 95 | + } |
| 96 | +} |
0 commit comments