Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function rememberConstructorScope(): self
return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
null,
$this->getNamespace(),
$this->rememberConstructorExpressions($this->expressionTypes),
$this->rememberConstructorExpressions($this->nativeExpressionTypes),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function sprintf;

/**
* @implements Rule<FullyQualified>
*/
class InstanceMethodsParameterScopeFunctionRule implements Rule
{

public function getNodeType(): string
{
return FullyQualified::class;
}

public function processNode(Node $node, Scope $scope): array
{
if ($scope->getFunction() !== null) {
throw new ShouldNotHappenException('All names in the tests should not have a function scope.');
}

return [
RuleErrorBuilder::message(sprintf('Name %s found in function scope null', $node->toString()))->identifier('test.instanceOfMethodsParameterRule')->build(),
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<InstanceMethodsParameterScopeFunctionRule>
*/
class InstanceMethodsParameterScopeFunctionTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new InstanceMethodsParameterScopeFunctionRule();
}

protected function shouldNarrowMethodScopeFromConstructor(): bool
{
return true;
}

public function testRule(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/instance-methods-parameter-scope.php'], [
[
'Name DateTime found in function scope null',
12,
],
[
'Name Baz\Waldo found in function scope null',
16,
],
]);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/data/instance-methods-parameter-scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types = 1);

namespace InstanceMethodsParameterScopeTest;

class HelloWorld
{
public function __construct()
{
}

public function foo(\DateTime $d): void
{
}

public function bar(\Baz\Waldo $d): void
{
}
}
Loading