Skip to content

Commit 178939a

Browse files
committed
Do not allow Scope::filterByTruthyValue() in the Generator namespace
1 parent 243d098 commit 178939a

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

build/PHPStan/Build/ScopeGetTypeInGeneratorNamespaceRule.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPStan\ShouldNotHappenException;
1110
use PHPStan\Type\ObjectType;
1211
use PHPUnit\Framework\TestCase;
1312
use function in_array;
@@ -41,10 +40,6 @@ public function processNode(Node $node, Scope $scope): array
4140
return [];
4241
}
4342

44-
if (!in_array($node->name->toLowerString(), ['gettype', 'getnativetype'], true)) {
45-
return [];
46-
}
47-
4843
$scopeType = new ObjectType(Scope::class);
4944
$calledOnType = $scope->getType($node->var);
5045
if (!$scopeType->isSuperTypeOf($calledOnType)->yes()) {
@@ -60,19 +55,33 @@ public function processNode(Node $node, Scope $scope): array
6055

6156
$methodReflection = $scope->getMethodReflection($calledOnType, $node->name->toString());
6257
if ($methodReflection === null) {
63-
throw new ShouldNotHappenException();
58+
return [];
59+
}
60+
61+
$message = sprintf(
62+
'Scope::%s() cannot be called in %s namespace.',
63+
$methodReflection->getName(),
64+
$invalidNamespace,
65+
);
66+
67+
if (in_array($methodReflection->getName(), ['getType', 'getNativeType'], true)) {
68+
return [
69+
RuleErrorBuilder::message($message)
70+
->identifier('phpstan.scopeGetType')
71+
->tip('Use yield new ExprAnalysisRequest or query the ExprAnalysisResultStorage instead.')
72+
->build(),
73+
];
74+
}
75+
76+
if (in_array($methodReflection->getName(), ['filterByTruthyValue', 'filterByFalseyValue'], true)) {
77+
return [
78+
RuleErrorBuilder::message($message)
79+
->identifier('phpstan.scopeFilter')
80+
->build(),
81+
];
6482
}
6583

66-
return [
67-
RuleErrorBuilder::message(sprintf(
68-
'Scope::%s() cannot be called in %s namespace.',
69-
$methodReflection->getName(),
70-
$invalidNamespace,
71-
))
72-
->identifier('phpstan.scopeGetType')
73-
->tip('Use yield new ExprAnalysisRequest or query the ExprAnalysisResultStorage instead.')
74-
->build(),
75-
];
84+
return [];
7685
}
7786

7887
}

tests/PHPStan/Build/ScopeGetTypeInGeneratorNamespaceRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public function testRule(): void
3939
49,
4040
'Use yield new ExprAnalysisRequest or query the ExprAnalysisResultStorage instead.',
4141
],
42+
[
43+
'Scope::filterByTruthyValue() cannot be called in PHPStan\Analyser\Generator namespace.',
44+
59,
45+
],
46+
[
47+
'Scope::filterByFalseyValue() cannot be called in PHPStan\Analyser\Generator namespace.',
48+
60,
49+
],
50+
[
51+
'Scope::filterByTruthyValue() cannot be called in PHPStan\Analyser\Generator namespace.',
52+
65,
53+
],
54+
[
55+
'Scope::filterByFalseyValue() cannot be called in PHPStan\Analyser\Generator namespace.',
56+
66,
57+
],
4258
]);
4359
}
4460

tests/PHPStan/Build/data/scope-get-type-generator-ns.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,21 @@ public function doBar(GeneratorScope $scope): void
5050
}
5151

5252
}
53+
54+
class Bar
55+
{
56+
57+
public function doFoo(Scope $scope): void
58+
{
59+
$scope->filterByTruthyValue();
60+
$scope->filterByFalseyValue();
61+
}
62+
63+
public function doBar(GeneratorScope $scope): void
64+
{
65+
$scope->filterByTruthyValue();
66+
$scope->filterByFalseyValue();
67+
}
68+
69+
}
5370
}

0 commit comments

Comments
 (0)