Skip to content

Commit 488b65f

Browse files
authored
Scope->rememberConstructorScope should not remember the function scope
1 parent b3d3386 commit 488b65f

4 files changed

+97
-1
lines changed

Diff for: src/Analyser/MutatingScope.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public function rememberConstructorScope(): self
348348
return $this->scopeFactory->create(
349349
$this->context,
350350
$this->isDeclareStrictTypes(),
351-
$this->getFunction(),
351+
null,
352352
$this->getNamespace(),
353353
$this->rememberConstructorExpressions($this->expressionTypes),
354354
$this->rememberConstructorExpressions($this->nativeExpressionTypes),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Name\FullyQualified;
7+
use PHPStan\Rules\Rule;
8+
use PHPStan\Rules\RuleErrorBuilder;
9+
use PHPStan\ShouldNotHappenException;
10+
use function sprintf;
11+
12+
/**
13+
* @implements Rule<FullyQualified>
14+
*/
15+
class InstanceMethodsParameterScopeFunctionRule implements Rule
16+
{
17+
18+
public function getNodeType(): string
19+
{
20+
return FullyQualified::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
if ($scope->getFunction() !== null) {
26+
throw new ShouldNotHappenException('All names in the tests should not have a function scope.');
27+
}
28+
29+
return [
30+
RuleErrorBuilder::message(sprintf('Name %s found in function scope null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(),
31+
];
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
8+
9+
/**
10+
* @extends RuleTestCase<InstanceMethodsParameterScopeFunctionRule>
11+
*/
12+
class InstanceMethodsParameterScopeFunctionTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new InstanceMethodsParameterScopeFunctionRule();
18+
}
19+
20+
protected function shouldNarrowMethodScopeFromConstructor(): bool
21+
{
22+
return true;
23+
}
24+
25+
public function testRule(): void
26+
{
27+
if (PHP_VERSION_ID < 80000) {
28+
$this->markTestSkipped('Test requires PHP 8.0.');
29+
}
30+
31+
$this->analyse([__DIR__ . '/data/instance-methods-parameter-scope.php'], [
32+
[
33+
'Name DateTime found in function scope null',
34+
12,
35+
],
36+
[
37+
'Name Baz\Waldo found in function scope null',
38+
16,
39+
],
40+
]);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace InstanceMethodsParameterScopeTest;
5+
6+
class HelloWorld
7+
{
8+
public function __construct()
9+
{
10+
}
11+
12+
public function foo(\DateTime $d): void
13+
{
14+
}
15+
16+
public function bar(\Baz\Waldo $d): void
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)