Skip to content

Commit a68d6f2

Browse files
Boegiemglaman
andauthored
EntityQueryDynamicReturnTypeExtension ignore count if unknown type (#546)
Co-authored-by: Matt Glaman <[email protected]>
1 parent 7340cbc commit a68d6f2

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ public function getTypeFromMethodCall(
4747
if ($varType instanceof EntityQueryType) {
4848
return $varType->asCount();
4949
}
50-
return new EntityQueryCountType(
51-
$varType->getClassName(),
52-
$varType->getSubtractedType(),
53-
$varType->getClassReflection()
54-
);
50+
// By now we are sure we can't determine anything about what query
51+
// the count method is on, so we ignore it.
52+
return $defaultReturnType;
5553
}
5654

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

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,10 @@ public function cases(): \Generator
143143
[]
144144
];
145145

146+
yield 'bug-x.php' => [
147+
[__DIR__.'/data/bug-545.php'],
148+
[]
149+
];
150+
146151
}
147152
}

tests/src/Rules/data/bug-545.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Bug545;
4+
5+
/**
6+
* Code snippets from \Drupal\migrate_drupal\Plugin\migrate\source\ContentEntity.
7+
*/
8+
class TestClass {
9+
10+
/**
11+
* The entity type manager.
12+
*
13+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
14+
*/
15+
protected $entityTypeManager;
16+
17+
18+
/**
19+
* The entity type definition.
20+
*
21+
* @var \Drupal\Core\Entity\EntityTypeInterface
22+
*/
23+
protected $entityType;
24+
25+
/**
26+
* Query to retrieve the entities.
27+
*
28+
* @return \Drupal\Core\Entity\Query\QueryInterface
29+
* The query.
30+
*/
31+
public function query() {
32+
$query = $this->entityTypeManager
33+
->getStorage($this->entityType->id())
34+
->getQuery()
35+
->accessCheck(FALSE);
36+
if (!empty($this->configuration['bundle'])) {
37+
$query->condition($this->entityType->getKey('bundle'), $this->configuration['bundle']);
38+
}
39+
// Exclude anonymous user account.
40+
if ($this->entityType->id() === 'user' && !empty($this->entityType->getKey('id'))) {
41+
$query->condition($this->entityType->getKey('id'), 0, '>');
42+
}
43+
return $query;
44+
}
45+
46+
protected function doCountInline() {
47+
$query = $this->entityTypeManager
48+
->getStorage($this->entityType->id())
49+
->getQuery()
50+
->accessCheck(FALSE)
51+
->count();
52+
return $query->execute();
53+
}
54+
55+
protected function doQuery() {
56+
return $this->query()->execute();
57+
}
58+
59+
protected function doCount() {
60+
return $this->query()->count()->execute();
61+
}
62+
63+
}

0 commit comments

Comments
 (0)