Skip to content

Commit 4567fc4

Browse files
committed
Reuse shared association reference lookup
1 parent b5491ac commit 4567fc4

1 file changed

Lines changed: 3 additions & 33 deletions

File tree

src/Type/AssociationFindDynamicReturnTypeExtension.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\ORM\Association;
1717
use Cake\ORM\Query\SelectQuery;
1818
use CakeDC\PHPStan\Traits\EntityClassFromTableClassTrait;
19+
use CakeDC\PHPStan\Traits\RepositoryReferenceTrait;
1920
use PhpParser\Node\Expr\MethodCall;
2021
use PHPStan\Analyser\Scope;
2122
use PHPStan\Reflection\MethodReflection;
@@ -51,6 +52,7 @@
5152
class AssociationFindDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
5253
{
5354
use EntityClassFromTableClassTrait;
55+
use RepositoryReferenceTrait;
5456

5557
/**
5658
* @inheritDoc
@@ -79,7 +81,7 @@ public function getTypeFromMethodCall(
7981
MethodCall $methodCall,
8082
Scope $scope,
8183
): ?Type {
82-
$tableClass = $this->extractTargetTableClass($scope, $methodCall);
84+
$tableClass = $this->getReferenceClass($scope, $methodCall);
8385
if ($tableClass === null) {
8486
return null;
8587
}
@@ -91,36 +93,4 @@ public function getTypeFromMethodCall(
9193

9294
return new GenericObjectType(SelectQuery::class, [new ObjectType($entityClass)]);
9395
}
94-
95-
/**
96-
* Returns the target table FQCN from an `Association<TargetTable>` generic
97-
* type, or null if the association is not generic-typed.
98-
*
99-
* @param \PHPStan\Analyser\Scope $scope
100-
* @param \PhpParser\Node\Expr\MethodCall $methodCall
101-
* @return string|null
102-
*/
103-
protected function extractTargetTableClass(Scope $scope, MethodCall $methodCall): ?string
104-
{
105-
$calledOnType = $scope->getType($methodCall->var);
106-
// GenericObjectType is the only way to read template parameter types
107-
// from a typed Association — there is no non-deprecated equivalent in
108-
// PHPStan 2.x yet.
109-
// @phpstan-ignore-next-line phpstanApi.instanceofType
110-
if (!$calledOnType instanceof GenericObjectType) {
111-
return null;
112-
}
113-
114-
$typeArgs = $calledOnType->getTypes();
115-
if (count($typeArgs) === 0) {
116-
return null;
117-
}
118-
119-
$tableClassNames = $typeArgs[0]->getObjectClassNames();
120-
if (count($tableClassNames) === 0) {
121-
return null;
122-
}
123-
124-
return $tableClassNames[0];
125-
}
12696
}

0 commit comments

Comments
 (0)