Skip to content

Commit 88efcbe

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents c008e03 + 05092ed commit 88efcbe

File tree

6 files changed

+181
-27
lines changed

6 files changed

+181
-27
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ lint:
133133
--exclude tests/PHPStan/Rules/Operators/data/bug-3585.php \
134134
--exclude tests/PHPStan/Rules/EnumCases/data/bug-14252.php \
135135
--exclude tests/PHPStan/Rules/Functions/data/bug-14241.php \
136+
--exclude tests/PHPStan/Rules/Variables/data/bug-14349.php \
136137
src tests
137138

138139
install-paratest:

src/Rules/Operators/InvalidAssignVarRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7272
private function containsNonAssignableExpression(Expr $expr): bool
7373
{
7474
if ($expr instanceof Expr\Variable) {
75-
return $expr->name === 'this';
75+
return false;
7676
}
7777

7878
if ($expr instanceof Expr\PropertyFetch) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Variables;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\RegisteredRule;
8+
use PHPStan\Node\VariableAssignNode;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
use function is_string;
12+
13+
/**
14+
* @implements Rule<VariableAssignNode>
15+
*/
16+
#[RegisteredRule(level: 0)]
17+
final class InvalidVariableAssignRule implements Rule
18+
{
19+
20+
public function getNodeType(): string
21+
{
22+
return VariableAssignNode::class;
23+
}
24+
25+
public function processNode(Node $node, Scope $scope): array
26+
{
27+
$variable = $node->getVariable();
28+
if (!is_string($variable->name)) {
29+
return [];
30+
}
31+
32+
if ($variable->name === 'this') {
33+
return [
34+
RuleErrorBuilder::message('Cannot re-assign $this.')
35+
->identifier('assign.this')
36+
->nonIgnorable()
37+
->build(),
38+
];
39+
}
40+
41+
return [];
42+
}
43+
44+
}

tests/PHPStan/Rules/Operators/InvalidAssignVarRuleTest.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,7 @@ public function testRule(): void
6161

6262
public function testBug3585(): void
6363
{
64-
$this->analyse([__DIR__ . '/data/bug-3585.php'], [
65-
[
66-
'Expression on left side of assignment is not assignable.',
67-
9,
68-
],
69-
[
70-
'Expression on left side of assignment is not assignable.',
71-
10,
72-
],
73-
[
74-
'Expression on left side of assignment is not assignable.',
75-
11,
76-
],
77-
[
78-
'Expression on left side of assignment is not assignable.',
79-
12,
80-
],
81-
[
82-
'Expression on left side of assignment is not assignable.',
83-
17,
84-
],
85-
[
86-
'Expression on left side of assignment is not assignable.',
87-
23,
88-
],
89-
]);
64+
$this->analyse([__DIR__ . '/data/bug-3585.php'], []);
9065
}
9166

9267
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Variables;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<InvalidVariableAssignRule>
10+
*/
11+
class InvalidVariableAssignRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new InvalidVariableAssignRule();
17+
}
18+
19+
public function testBug3585(): void
20+
{
21+
$this->analyse([__DIR__ . '/../Operators/data/bug-3585.php'], [
22+
[
23+
'Cannot re-assign $this.',
24+
9,
25+
],
26+
[
27+
'Cannot re-assign $this.',
28+
10,
29+
],
30+
[
31+
'Cannot re-assign $this.',
32+
11,
33+
],
34+
[
35+
'Cannot re-assign $this.',
36+
12,
37+
],
38+
[
39+
'Cannot re-assign $this.',
40+
17,
41+
],
42+
[
43+
'Cannot re-assign $this.',
44+
23,
45+
],
46+
]);
47+
}
48+
49+
public function testBug14349(): void
50+
{
51+
$this->analyse([__DIR__ . '/data/bug-14349.php'], [
52+
[
53+
'Cannot re-assign $this.',
54+
11,
55+
],
56+
[
57+
'Cannot re-assign $this.',
58+
15,
59+
],
60+
[
61+
'Cannot re-assign $this.',
62+
19,
63+
],
64+
[
65+
'Cannot re-assign $this.',
66+
27,
67+
],
68+
[
69+
'Cannot re-assign $this.',
70+
28,
71+
],
72+
[
73+
'Cannot re-assign $this.',
74+
29,
75+
],
76+
[
77+
'Cannot re-assign $this.',
78+
30,
79+
],
80+
[
81+
'Cannot re-assign $this.',
82+
35,
83+
],
84+
[
85+
'Cannot re-assign $this.',
86+
42,
87+
],
88+
]);
89+
}
90+
91+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14349;
4+
5+
class Foo
6+
{
7+
8+
/** @param array<int> $a */
9+
public function doFoo(array $a): void
10+
{
11+
foreach ($a as $this) {
12+
var_dump($this);
13+
}
14+
15+
foreach ($a as &$this) {
16+
var_dump($this);
17+
}
18+
19+
foreach ($a as $this => $v) {
20+
var_dump($this);
21+
}
22+
23+
foreach ($a as $ok) {
24+
var_dump($ok);
25+
}
26+
27+
$this = 1;
28+
$this = new self();
29+
$this .= 'foo';
30+
[$this] = [1];
31+
}
32+
33+
public static function doBar(): void
34+
{
35+
$this = 1;
36+
}
37+
38+
}
39+
40+
function baz(): void
41+
{
42+
$this = 1;
43+
}

0 commit comments

Comments
 (0)