Skip to content

Commit 693c356

Browse files
committed
Reword from has to with, add test with ::entityQuery static method
1 parent 8522a3c commit 693c356

9 files changed

+54
-20
lines changed

extension.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ services:
288288
class: mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryDynamicReturnTypeExtension
289289
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
290290
-
291-
class: mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryHasAccessCheckDynamicReturnTypeExtension
291+
class: mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryAccessCheckDynamicReturnTypeExtension
292292
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
293293
-
294294
class: mglaman\PHPStanDrupal\Type\UrlToStringDynamicReturnTypeExtension

src/Rules/Drupal/EntityQuery/EntityQueryHasAccessCheckRule.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

55
namespace mglaman\PHPStanDrupal\Rules\Drupal\EntityQuery;
66

7-
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteDoesNotHaveAccessCheckCountType;
8-
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteDoesNotHaveAccessCheckType;
9-
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryType;
7+
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteWithoutAccessCheckCountType;
8+
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteWithoutAccessCheckType;
109
use PhpParser\Node;
1110
use PHPStan\Analyser\Scope;
1211
use PHPStan\Rules\Rule;
1312
use PHPStan\Rules\RuleErrorBuilder;
14-
use function PHPStan\dumpType;
1513

1614
final class EntityQueryHasAccessCheckRule implements Rule
1715
{
1816
public function getNodeType(): string
1917
{
20-
return \PhpParser\Node\Expr\MethodCall::class;
18+
return Node\Expr\MethodCall::class;
2119
}
2220

2321
public function processNode(Node $node, Scope $scope): array
@@ -36,7 +34,7 @@ public function processNode(Node $node, Scope $scope): array
3634

3735
$type = $scope->getType($node);
3836

39-
if (!$type instanceof EntityQueryExecuteDoesNotHaveAccessCheckCountType && !$type instanceof EntityQueryExecuteDoesNotHaveAccessCheckType) {
37+
if (!$type instanceof EntityQueryExecuteWithoutAccessCheckCountType && !$type instanceof EntityQueryExecuteWithoutAccessCheckType) {
4038
return [];
4139
}
4240

src/Type/EntityQuery/EntityQueryHasAccessCheckDynamicReturnTypeExtension.php renamed to src/Type/EntityQuery/EntityQueryAccessCheckDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1313
use PHPStan\Type\Type;
1414

15-
class EntityQueryHasAccessCheckDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
15+
class EntityQueryAccessCheckDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1616
{
1717
public function getClass(): string
1818
{

src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ public function getTypeFromMethodCall(
5454
if ($varType instanceof EntityQueryCountType) {
5555
return $varType->hasAccessCheck()
5656
? new IntegerType()
57-
: new EntityQueryExecuteDoesNotHaveAccessCheckCountType();
57+
: new EntityQueryExecuteWithoutAccessCheckCountType();
5858
}
5959
if ($varType instanceof ConfigEntityQueryType) {
6060
return $varType->hasAccessCheck()
6161
? new ArrayType(new StringType(), new StringType())
62-
: new EntityQueryExecuteDoesNotHaveAccessCheckType(new StringType(), new StringType());
62+
: new EntityQueryExecuteWithoutAccessCheckType(new StringType(), new StringType());
6363
}
6464
if ($varType instanceof ContentEntityQueryType) {
6565
return $varType->hasAccessCheck()
6666
? new ArrayType(new IntegerType(), new StringType())
67-
: new EntityQueryExecuteDoesNotHaveAccessCheckType(new IntegerType(), new StringType());
67+
: new EntityQueryExecuteWithoutAccessCheckType(new IntegerType(), new StringType());
6868
}
6969
return $defaultReturnType;
7070
}

src/Type/EntityQuery/EntityQueryExecuteDoesNotHaveAccessCheckCountType.php renamed to src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHPStan\Type\IntegerType;
88

9-
final class EntityQueryExecuteDoesNotHaveAccessCheckCountType extends IntegerType
9+
final class EntityQueryExecuteWithoutAccessCheckCountType extends IntegerType
1010
{
1111

1212
}

src/Type/EntityQuery/EntityQueryExecuteDoesNotHaveAccessCheckType.php renamed to src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use PHPStan\Type\StringType;
1010

11-
final class EntityQueryExecuteDoesNotHaveAccessCheckType extends ArrayType
11+
final class EntityQueryExecuteWithoutAccessCheckType extends ArrayType
1212
{
1313

1414
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace fixtures\drupal\modules\phpstan_fixtures\src;
6+
7+
final class EntityQueryWithAccessRule
8+
{
9+
public function foo(): void
10+
{
11+
\Drupal::entityTypeManager()->getStorage('node')
12+
->getQuery()
13+
->accessCheck(FALSE)
14+
->condition('field_test', 'foo', '=')
15+
->execute();
16+
}
17+
18+
public function bar(): void
19+
{
20+
\Drupal::entityQuery('node')
21+
->accessCheck(FALSE)
22+
->condition('field_test', 'foo', '=')
23+
->execute();
24+
}
25+
}

tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryDoesNotHaveAccessRule.php renamed to tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithoutAccessRule.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace fixtures\drupal\modules\phpstan_fixtures\src;
66

7-
use function PHPStan\dumpType;
8-
9-
final class EntityQueryDoesNotHaveAccessRule
7+
final class EntityQueryWithoutAccessRule
108
{
119
public function foo(): void
1210
{
@@ -15,4 +13,12 @@ public function foo(): void
1513
->condition('field_test', 'foo', '=')
1614
->execute();
1715
}
16+
17+
public function bar(): void
18+
{
19+
\Drupal::entityQuery('node')
20+
->condition('field_test', 'foo', '=')
21+
->execute();
22+
}
23+
1824
}

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,23 @@ public function test(array $files, array $errors): void
2525
public function cases(): \Generator
2626
{
2727
yield [
28-
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryHasAccessRule.php'],
28+
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithAccessRule.php'],
2929
[],
3030
];
3131

3232
yield [
33-
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryDoesNotHaveAccessRule.php'],
33+
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithoutAccessRule.php'],
3434
[
3535
[
3636
'Missing explicit access check on entity query.',
37-
13,
37+
11,
3838
'See https://www.drupal.org/node/3201242',
39-
]
39+
],
40+
[
41+
'Missing explicit access check on entity query.',
42+
19,
43+
'See https://www.drupal.org/node/3201242',
44+
],
4045
],
4146
];
4247
}

0 commit comments

Comments
 (0)