Skip to content

Commit 4fb95b6

Browse files
committed
Use type-specifying extension for accessCheck
1 parent ca5c60f commit 4fb95b6

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,7 @@ services:
297297
-
298298
class: mglaman\PHPStanDrupal\Drupal\DrupalStubFilesExtension
299299
tags: [phpstan.stubFilesExtension]
300+
-
301+
class: mglaman\PHPStanDrupal\Type\EntityQuery\AccessCheckTypeSpecifyingExtension
302+
tags:
303+
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Type\EntityQuery;
4+
5+
use Drupal\Core\Entity\Query\QueryInterface;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Analyser\SpecifiedTypes;
9+
use PHPStan\Analyser\TypeSpecifier;
10+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
11+
use PHPStan\Analyser\TypeSpecifierContext;
12+
use PHPStan\Reflection\MethodReflection;
13+
use PHPStan\Reflection\ParametersAcceptorSelector;
14+
use PHPStan\Type\MethodTypeSpecifyingExtension;
15+
16+
final class AccessCheckTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
17+
{
18+
private TypeSpecifier $typeSpecifier;
19+
public function setTypeSpecifier(TypeSpecifier $typeSpecifier) : void
20+
{
21+
$this->typeSpecifier = $typeSpecifier;
22+
}
23+
24+
public function getClass(): string
25+
{
26+
return QueryInterface::class;
27+
}
28+
29+
public function isMethodSupported(
30+
MethodReflection $methodReflection,
31+
MethodCall $node,
32+
TypeSpecifierContext $context
33+
): bool {
34+
return $methodReflection->getName() === 'accessCheck';
35+
}
36+
37+
public function specifyTypes(
38+
MethodReflection $methodReflection,
39+
MethodCall $node,
40+
Scope $scope,
41+
TypeSpecifierContext $context
42+
): SpecifiedTypes {
43+
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
$expr = $node->var;
45+
if (!$returnType instanceof EntityQueryType) {
46+
return $this->typeSpecifier->create($expr, $returnType, TypeSpecifierContext::createTruthy());
47+
}
48+
return $this->typeSpecifier->create($expr, $returnType->withAccessCheck(), TypeSpecifierContext::createTruthy());
49+
}
50+
}

0 commit comments

Comments
 (0)