Skip to content

Commit 7159f64

Browse files
Add similar behavior for all Assign nodes (#4996)
Co-authored-by: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Co-authored-by: phpstan-bot <ondrej+phpstanbot@mirtes.cz>
1 parent 1991098 commit 7159f64

13 files changed

+164
-4
lines changed

src/Parser/NewAssignedToPropertyVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract
1616
#[Override]
1717
public function enterNode(Node $node): ?Node
1818
{
19-
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) {
19+
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef || $node instanceof Node\Expr\AssignOp) {
2020
if (
2121
($node->var instanceof Node\Expr\PropertyFetch || $node->var instanceof Node\Expr\StaticPropertyFetch)
2222
&& $node->expr instanceof Node\Expr\New_

src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ public function processNode(Node $node, Scope $scope): array
8888
return [];
8989
}
9090
if ($node instanceof Node\Stmt\Expression) {
91-
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
91+
if (
92+
!$node->expr instanceof Node\Expr\Assign
93+
&& !$node->expr instanceof Node\Expr\AssignRef
94+
&& !$node->expr instanceof Node\Expr\AssignOp
95+
) {
9296
return [];
9397
}
9498
}

src/Rules/PhpDoc/InvalidPhpDocTagValueRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public function processNode(Node $node, Scope $scope): array
4747
return [];
4848
}
4949
if ($node instanceof Node\Stmt\Expression) {
50-
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
50+
if (
51+
!$node->expr instanceof Node\Expr\Assign
52+
&& !$node->expr instanceof Node\Expr\AssignRef
53+
&& !$node->expr instanceof Node\Expr\AssignOp
54+
) {
5155
return [];
5256
}
5357
}

src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ private function processForeach(Scope $scope, Node\Expr $iterateeExpr, ?Node\Exp
288288
*/
289289
private function processExpression(Scope $scope, Expr $expr, array $varTags): array
290290
{
291-
if ($expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignRef) {
291+
if (
292+
$expr instanceof Node\Expr\Assign
293+
|| $expr instanceof Node\Expr\AssignRef
294+
|| $expr instanceof Node\Expr\AssignOp
295+
) {
292296
return $this->processAssign($scope, $expr->var, $expr->expr, $varTags);
293297
}
294298

tests/PHPStan/Rules/PhpDoc/InvalidPHPStanDocTagRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public function testBug8697(): void
5454
$this->analyse([__DIR__ . '/data/bug-8697.php'], []);
5555
}
5656

57+
public function testAssignOperator(): void
58+
{
59+
$this->analyse([__DIR__ . '/data/invalid-phpstan-doc-assign-operator.php'], [
60+
[
61+
'Unknown PHPDoc tag: @phpstan-va',
62+
9,
63+
],
64+
]);
65+
}
66+
5767
#[RequiresPhp('>= 8.4')]
5868
public function testPropertyHooks(): void
5969
{

tests/PHPStan/Rules/PhpDoc/InvalidPhpDocTagValueRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ public function testIgnoreWithinPhpDoc(): void
126126
$this->analyse([__DIR__ . '/data/ignore-line-within-phpdoc.php'], []);
127127
}
128128

129+
public function testAssignOperator(): void
130+
{
131+
$this->analyse([__DIR__ . '/data/invalid-phpdoc-assign-operator.php'], [
132+
[
133+
'PHPDoc tag @var has invalid value (\\\\Foo|\Bar $test): Unexpected token "\\\\\\\\Foo|\\\\Bar", expected type at offset 9 on line 1',
134+
8,
135+
],
136+
]);
137+
}
138+
129139
public function testBug6299(): void
130140
{
131141
$this->analyse([__DIR__ . '/data/bug-6299.php'], [

tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,28 @@ public function testReportWrongType(
548548
$this->analyse([__DIR__ . '/data/wrong-var-native-type.php'], $expectedErrors);
549549
}
550550

551+
public function testAssignOperator(): void
552+
{
553+
$this->analyse([__DIR__ . '/data/wrong-variable-name-var-assign-op.php'], [
554+
[
555+
'PHPDoc tag @var with type int is not subtype of native type void.',
556+
11,
557+
],
558+
[
559+
'PHPDoc tag @var with type int is not subtype of native type void.',
560+
14,
561+
],
562+
[
563+
'PHPDoc tag @var with type int is not subtype of native type void.',
564+
20,
565+
],
566+
[
567+
'PHPDoc tag @var with type int is not subtype of native type void.',
568+
23,
569+
],
570+
]);
571+
}
572+
551573
public function testBug12457(): void
552574
{
553575
$this->checkTypeAgainstPhpDocType = true;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace InvalidPhpDocAssignOperator;
4+
5+
function foo()
6+
{
7+
8+
/** @var \\Foo|\Bar $test */
9+
$test ??= doFoo();
10+
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace InvalidPHPStanDocAssignOperator;
4+
5+
class Boo extends Baz
6+
{
7+
function baz()
8+
{
9+
/** @phpstan-va */
10+
$c ??= 'foo';
11+
}
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace WrongVariableNameVarTagAssignOperator;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo()
9+
{
10+
/** @var int $test */
11+
$test ??= doFoo();
12+
13+
/** @var int */
14+
$test ??= doFoo();
15+
}
16+
17+
public function doBar(string $string)
18+
{
19+
/** @var int $string */
20+
$string .= doFoo();
21+
22+
/** @var int */
23+
$string .= doFoo();
24+
}
25+
26+
}
27+
28+
function doFoo(): void
29+
{
30+
31+
}

0 commit comments

Comments
 (0)