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/Parser/NewAssignedToPropertyVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract
#[Override]
public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) {
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef || $node instanceof Node\Expr\AssignOp) {
if (
($node->var instanceof Node\Expr\PropertyFetch || $node->var instanceof Node\Expr\StaticPropertyFetch)
&& $node->expr instanceof Node\Expr\New_
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}
if ($node instanceof Node\Stmt\Expression) {
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
if (
!$node->expr instanceof Node\Expr\Assign
&& !$node->expr instanceof Node\Expr\AssignRef
&& !$node->expr instanceof Node\Expr\AssignOp
) {
return [];
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/PhpDoc/InvalidPhpDocTagValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}
if ($node instanceof Node\Stmt\Expression) {
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
if (
!$node->expr instanceof Node\Expr\Assign
&& !$node->expr instanceof Node\Expr\AssignRef
&& !$node->expr instanceof Node\Expr\AssignOp
) {
return [];
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ private function processForeach(Scope $scope, Node\Expr $iterateeExpr, ?Node\Exp
*/
private function processExpression(Scope $scope, Expr $expr, array $varTags): array
{
if ($expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignRef) {
if (
$expr instanceof Node\Expr\Assign
|| $expr instanceof Node\Expr\AssignRef
|| $expr instanceof Node\Expr\AssignOp
) {
return $this->processAssign($scope, $expr->var, $expr->expr, $varTags);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidPHPStanDocTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public function testBug8697(): void
$this->analyse([__DIR__ . '/data/bug-8697.php'], []);
}

public function testAssignOperator(): void
{
$this->analyse([__DIR__ . '/data/invalid-phpstan-doc-assign-operator.php'], [
[
'Unknown PHPDoc tag: @phpstan-va',
9,
],
]);
}

#[RequiresPhp('>= 8.4')]
public function testPropertyHooks(): void
{
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidPhpDocTagValueRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public function testIgnoreWithinPhpDoc(): void
$this->analyse([__DIR__ . '/data/ignore-line-within-phpdoc.php'], []);
}

public function testAssignOperator(): void
{
$this->analyse([__DIR__ . '/data/invalid-phpdoc-assign-operator.php'], [
[
'PHPDoc tag @var has invalid value (\\\\Foo|\Bar $test): Unexpected token "\\\\\\\\Foo|\\\\Bar", expected type at offset 9 on line 1',
8,
],
]);
}

public function testBug6299(): void
{
$this->analyse([__DIR__ . '/data/bug-6299.php'], [
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,28 @@ public function testReportWrongType(
$this->analyse([__DIR__ . '/data/wrong-var-native-type.php'], $expectedErrors);
}

public function testAssignOperator(): void
{
$this->analyse([__DIR__ . '/data/wrong-variable-name-var-assign-op.php'], [
[
'PHPDoc tag @var with type int is not subtype of native type void.',
11,
],
[
'PHPDoc tag @var with type int is not subtype of native type void.',
14,
],
[
'PHPDoc tag @var with type int is not subtype of native type void.',
20,
],
[
'PHPDoc tag @var with type int is not subtype of native type void.',
23,
],
]);
}

public function testBug12457(): void
{
$this->checkTypeAgainstPhpDocType = true;
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/invalid-phpdoc-assign-operator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace InvalidPhpDocAssignOperator;

function foo()
{

/** @var \\Foo|\Bar $test */
$test ??= doFoo();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace InvalidPHPStanDocAssignOperator;

class Boo extends Baz
{
function baz()
{
/** @phpstan-va */
$c ??= 'foo';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace WrongVariableNameVarTagAssignOperator;

class Foo
{

public function doFoo()
{
/** @var int $test */
$test ??= doFoo();

/** @var int */
$test ??= doFoo();
}

public function doBar(string $string)
{
/** @var int $string */
$string .= doFoo();

/** @var int */
$string .= doFoo();
}

}

function doFoo(): void
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,15 @@ public function testCloneWith(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug12250(): void
{
$this->analyse([__DIR__ . '/data/bug-12250.php'], []);
}

public function testBug4525(): void
{
$this->analyse([__DIR__ . '/data/bug-4525.php'], []);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12250.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php // lint >= 8.0

namespace Bug12250;

class HelloWorld
{
/**
* @var \WeakMap<\stdClass, \stdClass>
*/
protected \WeakMap $bug, $ok;

public function bug(): void
{
$this->bug ??= new \WeakMap();
$this->ok = new \WeakMap();
}
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-4525.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 7.4

namespace Bug4525;

use SplObjectStorage;

class HelloWorld
{
/**
* @var SplObjectStorage<\DateTime, \DateTimeImmutable>
*/
private SplObjectStorage $map;

public function sayHello(): void
{
$this->map = new SplObjectStorage();
}

/** @phpstan-return SplObjectStorage<\DateTime, \DateTimeImmutable> */
public function getMap(): SplObjectStorage
{
return $this->map ??= new SplObjectStorage();
}
}
Loading