Skip to content

Commit 3d19fe8

Browse files
authored
Merge pull request #382 from brambaud/issue/381
Entity query count and accessCheck order should not matter
2 parents b934beb + c3db004 commit 3d19fe8

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/Type/EntityQuery/EntityQueryCountType.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace mglaman\PHPStanDrupal\Type\EntityQuery;
44

5-
use PHPStan\Type\ObjectType;
6-
75
/**
86
* Type used to represent an entity query instance as count query.
97
*/

src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ public function getTypeFromMethodCall(
3939
$varType = $scope->getType($methodCall->var);
4040
$methodName = $methodReflection->getName();
4141

42+
if (!$varType instanceof ObjectType) {
43+
return $defaultReturnType;
44+
}
45+
4246
if ($methodName === 'count') {
43-
if ($varType instanceof ObjectType) {
44-
return new EntityQueryCountType(
45-
$varType->getClassName(),
46-
$varType->getSubtractedType(),
47-
$varType->getClassReflection()
48-
);
47+
$returnType = new EntityQueryCountType(
48+
$varType->getClassName(),
49+
$varType->getSubtractedType(),
50+
$varType->getClassReflection()
51+
);
52+
if ($varType instanceof EntityQueryType && $varType->hasAccessCheck()) {
53+
return $returnType->withAccessCheck();
4954
}
50-
return $defaultReturnType;
55+
56+
return $returnType;
5157
}
5258

5359
if ($methodName === 'execute') {

tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithAccessRule.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ public function bar(): void
2222
->condition('field_test', 'foo', '=')
2323
->execute();
2424
}
25+
26+
public function count(): void
27+
{
28+
\Drupal::entityTypeManager()->getStorage('node')
29+
->getQuery()
30+
->accessCheck(false)
31+
->count()
32+
->execute();
33+
}
34+
35+
public function bug381CountOrderShouldNotMatter(): void
36+
{
37+
$query = \Drupal::entityTypeManager()->getStorage('node')
38+
->getQuery()
39+
->accessCheck(FALSE)
40+
->count();
41+
$count = (int) $query->execute();
42+
43+
$query = \Drupal::entityTypeManager()->getStorage('node')
44+
->getQuery()
45+
->count()
46+
->accessCheck(FALSE);
47+
$count = (int) $query->execute();
48+
}
2549
}

0 commit comments

Comments
 (0)