Skip to content

Commit 0bae5bb

Browse files
committed
Entity query count and accessCheck order should not matter
Fixes #381
1 parent b934beb commit 0bae5bb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function getTypeFromMethodCall(
4040
$methodName = $methodReflection->getName();
4141

4242
if ($methodName === 'count') {
43+
if ($varType instanceof EntityQueryType) {
44+
$returnType = new EntityQueryCountType(
45+
$varType->getClassName(),
46+
$varType->getSubtractedType(),
47+
$varType->getClassReflection()
48+
);
49+
50+
if ($varType->hasAccessCheck()) {
51+
return $returnType->withAccessCheck();
52+
}
53+
54+
return $returnType;
55+
}
56+
4357
if ($varType instanceof ObjectType) {
4458
return new EntityQueryCountType(
4559
$varType->getClassName(),

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)