Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 68 additions & 11 deletions src/Type/EntityQuery/AccessCheckTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
}
}
11 changes: 11 additions & 0 deletions tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]
];

}
}
93 changes: 93 additions & 0 deletions tests/src/Rules/data/bug-613.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Bug613;

function access_check_first(): array {
$query = \Drupal::entityTypeManager()->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();
}
Loading