Skip to content

Commit 88b723f

Browse files
committed
Variable variables - reworded error messages so that they're ignorable easier
1 parent b03ff03 commit 88b723f

10 files changed

+54
-14
lines changed

src/Rules/VariableVariables/VariableMethodCallRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\Type\VerbosityLevel;
89

910
class VariableMethodCallRule implements Rule
1011
{
@@ -26,7 +27,10 @@ public function processNode(Node $node, Scope $scope): array
2627
}
2728

2829
return [
29-
'Variable method calls are not allowed.',
30+
sprintf(
31+
'Variable method call on %s.',
32+
$scope->getType($node->var)->describe(VerbosityLevel::typeOnly())
33+
),
3034
];
3135
}
3236

src/Rules/VariableVariables/VariablePropertyFetchRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\Type\VerbosityLevel;
89

910
class VariablePropertyFetchRule implements Rule
1011
{
@@ -26,7 +27,10 @@ public function processNode(Node $node, Scope $scope): array
2627
}
2728

2829
return [
29-
'Variable properties are not allowed.',
30+
sprintf(
31+
'Variable property access on %s.',
32+
$scope->getType($node->var)->describe(VerbosityLevel::typeOnly())
33+
),
3034
];
3135
}
3236

src/Rules/VariableVariables/VariableStaticMethodCallRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\Type\VerbosityLevel;
89

910
class VariableStaticMethodCallRule implements Rule
1011
{
@@ -25,8 +26,17 @@ public function processNode(Node $node, Scope $scope): array
2526
return [];
2627
}
2728

29+
if ($node->class instanceof Node\Name) {
30+
$methodCalledOn = $scope->resolveName($node->class);
31+
} else {
32+
$methodCalledOn = $scope->getType($node->class)->describe(VerbosityLevel::typeOnly());
33+
}
34+
2835
return [
29-
'Variable static method calls are not allowed.',
36+
sprintf(
37+
'Variable static method call on %s.',
38+
$methodCalledOn
39+
),
3040
];
3141
}
3242

src/Rules/VariableVariables/VariableStaticPropertyFetchRule.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\Rule;
8+
use PHPStan\Type\VerbosityLevel;
89

910
class VariableStaticPropertyFetchRule implements Rule
1011
{
@@ -21,12 +22,21 @@ public function getNodeType(): string
2122
*/
2223
public function processNode(Node $node, Scope $scope): array
2324
{
24-
if ($node->name instanceof Node\VarLikeIdentifier) {
25+
if ($node->name instanceof Node\Identifier) {
2526
return [];
2627
}
2728

29+
if ($node->class instanceof Node\Name) {
30+
$propertyAccessedOn = $scope->resolveName($node->class);
31+
} else {
32+
$propertyAccessedOn = $scope->getType($node->class)->describe(VerbosityLevel::typeOnly());
33+
}
34+
2835
return [
29-
'Variable static properties are not allowed.',
36+
sprintf(
37+
'Variable static property access on %s.',
38+
$propertyAccessedOn
39+
),
3040
];
3141
}
3242

tests/Rules/VariableVariables/VariableMethodCallRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testRule(): void
1717
{
1818
$this->analyse([__DIR__ . '/data/methods.php'], [
1919
[
20-
'Variable method calls are not allowed.',
20+
'Variable method call on stdClass.',
2121
7,
2222
],
2323
]);

tests/Rules/VariableVariables/VariablePropertyFetchRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public function testRule(): void
1717
{
1818
$this->analyse([__DIR__ . '/data/properties.php'], [
1919
[
20-
'Variable properties are not allowed.',
20+
'Variable property access on stdClass.',
2121
6,
2222
],
2323
[
24-
'Variable properties are not allowed.',
24+
'Variable property access on stdClass.',
2525
9,
2626
],
2727
]);

tests/Rules/VariableVariables/VariableStaticMethodCallRuleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ public function testRule(): void
1717
{
1818
$this->analyse([__DIR__ . '/data/staticMethods.php'], [
1919
[
20-
'Variable static method calls are not allowed.',
20+
'Variable static method call on Foo.',
2121
7,
2222
],
23+
[
24+
'Variable static method call on stdClass.',
25+
9,
26+
],
2327
]);
2428
}
2529

tests/Rules/VariableVariables/VariableStaticPropertyFetchRuleTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@ public function testRule(): void
1717
{
1818
$this->analyse([__DIR__ . '/data/staticProperties.php'], [
1919
[
20-
'Variable static properties are not allowed.',
20+
'Variable static property access on Foo.',
2121
7,
2222
],
2323
[
24-
'Variable static properties are not allowed.',
24+
'Variable static property access on Foo.',
2525
8,
2626
],
2727
[
28-
'Variable static properties are not allowed.',
28+
'Variable static property access on Foo.',
2929
10,
3030
],
3131
[
32-
'Variable static properties are not allowed.',
32+
'Variable static property access on Foo.',
3333
11,
3434
],
35+
[
36+
'Variable static property access on stdClass.',
37+
13,
38+
],
3539
]);
3640
}
3741

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
function () {
3+
function (stdClass $std) {
44
Foo::doFoo();
55

66
$foo = 'doBar';
77
Foo::$foo();
8+
9+
$std::$foo();
810
};

tests/Rules/VariableVariables/data/staticProperties.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ function (stdClass $std) {
99

1010
echo Foo::${$bar};
1111
Foo::${$bar} = 123;
12+
13+
$std::$$bar = 123;
1214
};

0 commit comments

Comments
 (0)