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
28 changes: 25 additions & 3 deletions src/Rules/Classes/ClassConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace PHPStan\Rules\Classes;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\ClassNameNodePair;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand Down Expand Up @@ -47,11 +50,30 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
$errors = [];
if ($node->name instanceof Node\Identifier) {
$constantNameScopes = [$node->name->name => $scope];
} else {
$nameType = $scope->getType($node->name);
$constantNameScopes = [];
foreach ($nameType->getConstantStrings() as $constantString) {
$name = $constantString->getValue();
$constantNameScopes[$name] = $scope->filterByTruthyValue(new Identical($node->name, new String_($name)));
}
}

foreach ($constantNameScopes as $constantName => $constantScope) {
$errors = array_merge($errors, $this->processSingleClassConstFetch($constantScope, $node, $constantName));
}
$constantName = $node->name->name;

return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function processSingleClassConstFetch(Scope $scope, ClassConstFetch $node, string $constantName): array
{
$class = $node->class;
$messages = [];
if ($class instanceof Node\Name) {
Expand Down
27 changes: 24 additions & 3 deletions src/Rules/Methods/CallMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace PHPStan\Rules\Methods;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use function array_merge;

Expand All @@ -31,12 +34,30 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
$errors = [];
if ($node->name instanceof Node\Identifier) {
$methodNameScopes = [$node->name->name => $scope];
} else {
$nameType = $scope->getType($node->name);
$methodNameScopes = [];
foreach ($nameType->getConstantStrings() as $constantString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Now please do it consistently for all the touched rules. Thanks.

$name = $constantString->getValue();
$methodNameScopes[$name] = $scope->filterByTruthyValue(new Identical($node->name, new String_($name)));
}
}

$methodName = $node->name->name;
foreach ($methodNameScopes as $methodName => $methodScope) {
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
}

return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function processSingleMethodCall(Scope $scope, MethodCall $node, string $methodName): array
{
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodName, $node->var);
if ($methodReflection === null) {
return $errors;
Expand Down
28 changes: 25 additions & 3 deletions src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace PHPStan\Rules\Methods;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use function array_merge;
use function sprintf;
Expand All @@ -32,11 +35,30 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
$errors = [];
if ($node->name instanceof Node\Identifier) {
$methodNameScopes = [$node->name->name => $scope];
} else {
$nameType = $scope->getType($node->name);
$methodNameScopes = [];
foreach ($nameType->getConstantStrings() as $constantString) {
$name = $constantString->getValue();
$methodNameScopes[$name] = $scope->filterByTruthyValue(new Identical($node->name, new String_($name)));
}
}
$methodName = $node->name->name;

foreach ($methodNameScopes as $methodName => $methodScope) {
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
}

return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function processSingleMethodCall(Scope $scope, StaticCall $node, string $methodName): array
{
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class);
if ($method === null) {
return $errors;
Expand Down
38 changes: 31 additions & 7 deletions src/Rules/Variables/DefinedVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace PHPStan\Rules\Variables;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function array_merge;
use function in_array;
use function is_string;
use function sprintf;
Expand All @@ -31,11 +35,31 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!is_string($node->name)) {
return [];
$errors = [];
if (is_string($node->name)) {
$variableNameScopes = [$node->name => $scope];
} else {
$nameType = $scope->getType($node->name);
$variableNameScopes = [];
foreach ($nameType->getConstantStrings() as $constantString) {
$name = $constantString->getValue();
$variableNameScopes[$name] = $scope->filterByTruthyValue(new Identical($node->name, new String_($name)));
}
}

foreach ($variableNameScopes as $name => $variableScope) {
$errors = array_merge($errors, $this->processSingleVariable($variableScope, $node, $name));
}

if ($this->cliArgumentsVariablesRegistered && in_array($node->name, [
return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function processSingleVariable(Scope $scope, Variable $node, string $variableName): array
{
if ($this->cliArgumentsVariablesRegistered && in_array($variableName, [
'argc',
'argv',
], true)) {
Expand All @@ -49,18 +73,18 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($scope->hasVariableType($node->name)->no()) {
if ($scope->hasVariableType($variableName)->no()) {
return [
RuleErrorBuilder::message(sprintf('Undefined variable: $%s', $node->name))
RuleErrorBuilder::message(sprintf('Undefined variable: $%s', $variableName))
->identifier('variable.undefined')
->build(),
];
} elseif (
$this->checkMaybeUndefinedVariables
&& !$scope->hasVariableType($node->name)->yes()
&& !$scope->hasVariableType($variableName)->yes()
) {
return [
RuleErrorBuilder::message(sprintf('Variable $%s might not be defined.', $node->name))
RuleErrorBuilder::message(sprintf('Variable $%s might not be defined.', $variableName))
->identifier('variable.undefined')
->build(),
];
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,48 @@ public function testClassConstantAccessedOnTrait(): void
]);
}

public function testDynamicAccess(): void
{
if (PHP_VERSION_ID < 80300) {
$this->markTestSkipped('Test requires PHP 8.3.');
}

$this->phpVersion = PHP_VERSION_ID;

$this->analyse([__DIR__ . '/data/dynamic-constant-access.php'], [
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::FOO.',
20,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::BUZ.',
20,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::FOO.',
37,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::BUZ.',
39,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::QUX.',
41,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::QUX.',
44,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::BUZ.',
44,
],
[
'Access to undefined constant ClassConstantDynamicAccess\Foo::FOO.',
44,
],
]);
}

}
48 changes: 48 additions & 0 deletions tests/PHPStan/Rules/Classes/data/dynamic-constant-access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php // lint >= 8.3

namespace ClassConstantDynamicAccess;

final class Foo
{

private const BAR = 'BAR';

/** @var 'FOO'|'BAR'|'BUZ' */
public $name;

public function test(string $string, object $obj): void
{
$bar = 'FOO';

echo self::{$foo};
echo self::{$string};
echo self::{$obj};
echo self::{$this->name};
}

public function testScope(): void
{
$name1 = 'FOO';
$rand = rand();
if ($rand === 1) {
$foo = 1;
$name = $name1;
} elseif ($rand === 2) {
$name = 'BUZ';
} else {
$name = 'QUX';
}

if ($name === 'FOO') {
echo self::{$name};
} elseif ($name === 'BUZ') {
echo self::{$name};
} else {
echo self::{$name};
}

echo self::{$name};
}


}
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3552,4 +3552,39 @@ public function testBug6828(): void
$this->analyse([__DIR__ . '/data/bug-6828.php'], []);
}

public function testDynamicCall(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;

$this->analyse([__DIR__ . '/data/dynamic-call.php'], [
[
'Call to an undefined method MethodsDynamicCall\Foo::bar().',
23,
],
[
'Call to an undefined method MethodsDynamicCall\Foo::doBar().',
26,
],
[
'Call to an undefined method MethodsDynamicCall\Foo::doBuz().',
26,
],
[
'Parameter #1 $n of method MethodsDynamicCall\Foo::doFoo() expects int, int|string given.',
53,
],
[
'Parameter #1 $s of method MethodsDynamicCall\Foo::doQux() expects string, int given.',
54,
],
[
'Parameter #1 $n of method MethodsDynamicCall\Foo::doFoo() expects int, string given.',
55,
],
]);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,38 @@ public function testBug12015(): void
$this->analyse([__DIR__ . '/data/bug-12015.php'], []);
}

public function testDynamicCall(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/dynamic-call.php'], [
[
'Call to an undefined static method MethodsDynamicCall\Foo::bar().',
33,
],
[
'Call to an undefined static method MethodsDynamicCall\Foo::doBar().',
36,
],
[
'Call to an undefined static method MethodsDynamicCall\Foo::doBuz().',
36,
],
[
'Parameter #1 $n of method MethodsDynamicCall\Foo::doFoo() expects int, int|string given.',
58,
],
[
'Parameter #1 $s of static method MethodsDynamicCall\Foo::doQux() expects string, int given.',
59,
],
[
'Parameter #1 $n of method MethodsDynamicCall\Foo::doFoo() expects int, string given.',
60,
],
]);
}

}
Loading
Loading