Skip to content

Commit 949591f

Browse files
staabmspazeruudk
committed
added test
Co-Authored-By: Michal Špaček <[email protected]> Co-Authored-By: Ruud Kamphuis <[email protected]>
1 parent 150b731 commit 949591f

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 function sprintf;
10+
11+
/**
12+
* @implements Rule<FullyQualified>
13+
*/
14+
class InstanceMethodsParameterRule implements Rule
15+
{
16+
17+
public function getNodeType(): string
18+
{
19+
return FullyQualified::class;
20+
}
21+
22+
public function processNode(Node $node, Scope $scope): array
23+
{
24+
return [
25+
RuleErrorBuilder::message(sprintf('Name %s found in method %s', $node->toString(), $scope->getFunction()?->getName() ?? 'null'))->identifier('test.instanceOfMethodsParameterRule')->build(),
26+
];
27+
}
28+
29+
}
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<InstanceMethodsParameterRule>
11+
*/
12+
class InstanceMethodsParameterScopeTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new InstanceMethodsParameterRule();
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 method null',
34+
12,
35+
],
36+
[
37+
'Name Baz\Waldo found in method 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)