diff --git a/src/Type/EntityQuery/AccessCheckTypeSpecifyingExtension.php b/src/Type/EntityQuery/AccessCheckTypeSpecifyingExtension.php index aab81a15..410d9047 100644 --- a/src/Type/EntityQuery/AccessCheckTypeSpecifyingExtension.php +++ b/src/Type/EntityQuery/AccessCheckTypeSpecifyingExtension.php @@ -3,14 +3,16 @@ namespace mglaman\PHPStanDrupal\Type\EntityQuery; use Drupal\Core\Entity\Query\QueryInterface; +use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\NullsafeMethodCall; +use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Analyser\SpecifiedTypes; use PHPStan\Analyser\TypeSpecifier; use PHPStan\Analyser\TypeSpecifierAwareExtension; use PHPStan\Analyser\TypeSpecifierContext; use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\MethodTypeSpecifyingExtension; final class AccessCheckTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension @@ -26,34 +28,89 @@ public function getClass(): string return QueryInterface::class; } + /** + * Supports every query method, because accessCheck() can be anywhere in a + * chain like $query->accessCheck(FALSE)->condition('status', 1); + */ public function isMethodSupported( MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context ): bool { - return $methodReflection->getName() === 'accessCheck'; + return true; } + /** + * Marks the query a method chain starts from as access checked. + * + * Query methods return the query itself, so in + * $query->condition('status', 1)->accessCheck(FALSE); the accessCheck() + * call applies to $query, not only to the value of the chain. + */ public function specifyTypes( MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context ): SpecifiedTypes { - $returnType = ParametersAcceptorSelector::selectFromArgs( - $scope, - [], - $methodReflection->getVariants() - )->getReturnType(); - $expr = $node->var; - if (!$returnType instanceof EntityQueryType) { + // This runs for every query method call, such as each separate + // $query->condition() statement. Only a chain that includes + // accessCheck() needs types, so check the method names first. + if (!$this->chainCallsAccessCheck($node)) { + return new SpecifiedTypes([]); + } + + $hasAccessCheck = false; + $call = $node; + while (true) { + if ($this->isAccessCheckCall($call)) { + $hasAccessCheck = true; + } + $receiver = $call->var; + if ((!$receiver instanceof MethodCall && !$receiver instanceof NullsafeMethodCall) + || !$scope->getType($receiver) instanceof EntityQueryType + ) { + break; + } + $call = $receiver; + } + if (!$hasAccessCheck) { + return new SpecifiedTypes([]); + } + + // A call like \Drupal::entityQuery('node') returns a new query each + // time, so narrowing it would mark later, unrelated queries as + // checked. Variables, properties and array elements keep the query. + $query = $call->var; + if ($query instanceof CallLike) { + return new SpecifiedTypes([]); + } + $queryType = $scope->getType($query); + if (!$queryType instanceof EntityQueryType) { return new SpecifiedTypes([]); } return $this->typeSpecifier->create( - $expr, - $returnType->withAccessCheck(), + $query, + $queryType->withAccessCheck(), TypeSpecifierContext::createTruthy(), $scope )->setAlwaysOverwriteTypes(); } + + private function chainCallsAccessCheck(MethodCall $node): bool + { + $call = $node; + while ($call instanceof MethodCall || $call instanceof NullsafeMethodCall) { + if ($this->isAccessCheckCall($call)) { + return true; + } + $call = $call->var; + } + return false; + } + + private function isAccessCheckCall(MethodCall|NullsafeMethodCall $call): bool + { + return $call->name instanceof Identifier && $call->name->toLowerString() === 'accesscheck'; + } } diff --git a/tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php b/tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php index 5fa84310..f94eeb05 100644 --- a/tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php +++ b/tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php @@ -160,5 +160,16 @@ public static function cases(): \Generator [] ]; + yield 'bug-613.php' => [ + [__DIR__ . '/data/bug-613.php'], + [ + [ + 'Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \\Drupal\\Core\\Entity\\Query\\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.', + 45, + 'See https://www.drupal.org/node/3201242', + ], + ] + ]; + } } diff --git a/tests/src/Rules/data/bug-613.php b/tests/src/Rules/data/bug-613.php new file mode 100644 index 00000000..5a528049 --- /dev/null +++ b/tests/src/Rules/data/bug-613.php @@ -0,0 +1,93 @@ +getStorage('node')->getQuery(); + $query + ->accessCheck(FALSE) + ->condition('field_myfield', TRUE); + return $query->execute(); +} + +function access_check_last(): array { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query + ->condition('field_myfield', TRUE) + ->accessCheck(FALSE); + return $query->execute(); +} + +function access_check_with_condition_group(): array { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query + ->accessCheck(FALSE) + ->condition($query->andConditionGroup() + ->condition('status', 1) + ->condition('promote', 1)); + return $query->execute(); +} + +function access_check_then_several_conditions(): array { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query->accessCheck(FALSE) + ->condition('type', 'article') + ->condition('status', 1) + ->condition('promote', 1); + return $query->execute(); +} + +function no_access_check(): array { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query + ->condition('type', 'article') + ->condition('status', 1); + return $query->execute(); +} + +final class QueryHolder +{ + private \Drupal\Core\Entity\Query\QueryInterface $query; + + private static \Drupal\Core\Entity\Query\QueryInterface $staticQuery; + + public function __construct() + { + $this->query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + self::$staticQuery = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + } + + public function property(): array + { + $this->query + ->condition('status', 1) + ->accessCheck(FALSE); + return $this->query->execute(); + } + + public function staticProperty(): array + { + self::$staticQuery + ->condition('status', 1) + ->accessCheck(FALSE); + return self::$staticQuery->execute(); + } +} + +function array_element(): array { + $queries = ['node' => \Drupal::entityTypeManager()->getStorage('node')->getQuery()]; + $queries['node']->accessCheck(FALSE); + return $queries['node']->execute(); +} + +function nullsafe_link(): array { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query?->condition('status', 1)->accessCheck(FALSE); + return $query->execute(); +} + +function count_chain(): int { + $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery(); + $query->accessCheck(FALSE)->count(); + return $query->execute(); +}