Skip to content

Commit 578fd31

Browse files
committed
Scope::getNativeType() also cannot be called in PHPStan\Analyser\Generator namespace
1 parent 80f4734 commit 578fd31

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

build/PHPStan/Build/ScopeGetTypeInGeneratorNamespaceRule.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10+
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\ObjectType;
1112
use PHPUnit\Framework\TestCase;
13+
use function in_array;
1214
use function sprintf;
1315
use function str_starts_with;
1416

@@ -39,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
3941
return [];
4042
}
4143

42-
if ($node->name->toLowerString() !== 'gettype') {
44+
if (!in_array($node->name->toLowerString(), ['gettype', 'getnativetype'], true)) {
4345
return [];
4446
}
4547

@@ -56,9 +58,15 @@ public function processNode(Node $node, Scope $scope): array
5658
}
5759
}
5860

61+
$methodReflection = $scope->getMethodReflection($calledOnType, $node->name->toString());
62+
if ($methodReflection === null) {
63+
throw new ShouldNotHappenException();
64+
}
65+
5966
return [
6067
RuleErrorBuilder::message(sprintf(
61-
'Scope::getType() cannot be called in %s namespace.',
68+
'Scope::%s() cannot be called in %s namespace.',
69+
$methodReflection->getName(),
6270
$invalidNamespace,
6371
))
6472
->identifier('phpstan.scopeGetType')

tests/PHPStan/Build/ScopeGetTypeInGeneratorNamespaceRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function testRule(): void
3434
48,
3535
'Use new ExprAnalysisRequest instead.',
3636
],
37+
[
38+
'Scope::getNativeType() cannot be called in PHPStan\Analyser\Generator namespace.',
39+
49,
40+
'Use new ExprAnalysisRequest instead.',
41+
],
3742
]);
3843
}
3944

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function doFoo(Scope $scope): void
4646
public function doBar(GeneratorScope $scope): void
4747
{
4848
$scope->getType();
49+
$scope->getNativeType();
4950
}
5051

5152
}

0 commit comments

Comments
 (0)