Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit 6383101

Browse files
authored
[PHPStanRules] Remove parent/previous from NoMultiArrayAssignRule (#4134)
1 parent 1fa07a4 commit 6383101

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

packages/phpstan-rules/src/Rules/NoMultiArrayAssignRule.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44

55
namespace Symplify\PHPStanRules\Rules;
66

7+
use PhpParser\Node\Stmt\ClassMethod;
78
use PhpParser\Node;
8-
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\ArrayDimFetch;
1010
use PhpParser\Node\Expr\Assign;
11+
use PhpParser\Node\Expr\Closure;
12+
use PhpParser\Node\Stmt;
13+
use PhpParser\Node\Stmt\Else_;
1114
use PhpParser\Node\Stmt\Expression;
15+
use PhpParser\Node\Stmt\Function_;
16+
use PhpParser\Node\Stmt\If_;
1217
use PHPStan\Analyser\Scope;
1318
use PHPStan\Rules\Rule;
14-
use Symplify\Astral\ValueObject\AttributeKey;
19+
use PHPStan\Rules\RuleError;
20+
use PHPStan\Rules\RuleErrorBuilder;
1521
use Symplify\PHPStanRules\Printer\NodeComparator;
1622
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
1723
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1824
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1925

2026
/**
2127
* @see \Symplify\PHPStanRules\Tests\Rules\NoMultiArrayAssignRule\NoMultiArrayAssignRuleTest
28+
* @implements Rule<Node>
2229
*/
2330
final class NoMultiArrayAssignRule implements Rule, DocumentedRuleInterface
2431
{
@@ -32,35 +39,49 @@ public function __construct(
3239
) {
3340
}
3441

35-
/**
36-
* @return class-string<Node>
37-
*/
3842
public function getNodeType(): string
3943
{
40-
return Assign::class;
44+
return Node::class;
4145
}
4246

4347
/**
44-
* @param Assign $node
45-
* @return string[]
48+
* @return RuleError[]
4649
*/
4750
public function processNode(Node $node, Scope $scope): array
4851
{
49-
if (! $node->var instanceof ArrayDimFetch) {
52+
// check all of stmts aware nodes, see https://github.com/nikic/PHP-Parser/pull/836
53+
if (! $node instanceof ClassMethod && ! $node instanceof Function_ && ! $node instanceof Closure && ! $node instanceof If_ && ! $node instanceof Else_) {
5054
return [];
5155
}
5256

53-
// is previous array dim assign too? - print the exprt conteont
54-
$previousArrayDimFetch = $this->matchParentArrayDimFetch($node);
55-
if (! $previousArrayDimFetch instanceof ArrayDimFetch) {
56-
return [];
57-
}
57+
foreach ((array) $node->stmts as $key => $stmt) {
58+
$firstArrayDimFetch = $this->matchAssignToArrayDimFetch($stmt);
59+
if (! $firstArrayDimFetch instanceof ArrayDimFetch) {
60+
continue;
61+
}
5862

59-
if (! $this->haveSameArrayDimFetchNonEmptyRoot($node->var, $previousArrayDimFetch)) {
60-
return [];
63+
$nextStmt = $node->stmts[$key + 1] ?? null;
64+
if (! $nextStmt instanceof Stmt) {
65+
return [];
66+
}
67+
68+
$secondArrayDimFetch = $this->matchAssignToArrayDimFetch($nextStmt);
69+
if (! $secondArrayDimFetch instanceof ArrayDimFetch) {
70+
continue;
71+
}
72+
73+
if (! $this->haveSameArrayDimFetchNonEmptyRoot($firstArrayDimFetch, $secondArrayDimFetch)) {
74+
continue;
75+
}
76+
77+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
78+
->line($nextStmt->getLine())
79+
->build();
80+
81+
return [$ruleError];
6182
}
6283

63-
return [self::ERROR_MESSAGE];
84+
return [];
6485
}
6586

6687
public function getRuleDefinition(): RuleDefinition
@@ -105,27 +126,21 @@ private function resolveSingleNestedArrayDimFetch(ArrayDimFetch $arrayDimFetch):
105126
return $arrayDimFetch;
106127
}
107128

108-
private function matchParentArrayDimFetch(Assign $assign): ?Expr
129+
private function matchAssignToArrayDimFetch(Stmt $stmt): ?ArrayDimFetch
109130
{
110-
$parent = $assign->getAttribute(AttributeKey::PARENT);
111-
if (! $parent instanceof Expression) {
112-
return null;
113-
}
114-
115-
$previous = $parent->getAttribute(AttributeKey::PREVIOUS);
116-
if (! $previous instanceof Expression) {
131+
if (! $stmt instanceof Expression) {
117132
return null;
118133
}
119134

120-
if (! $previous->expr instanceof Assign) {
135+
if (! $stmt->expr instanceof Assign) {
121136
return null;
122137
}
123138

124-
$previousAssign = $previous->expr;
125-
if (! $previousAssign->var instanceof ArrayDimFetch) {
139+
$assign = $stmt->expr;
140+
if (! $assign->var instanceof ArrayDimFetch) {
126141
return null;
127142
}
128143

129-
return $previousAssign->var;
144+
return $assign->var;
130145
}
131146
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
includes:
22
- ../../../config/included_services.neon
33

4-
services:
5-
-
6-
class: Symplify\PHPStanRules\Rules\NoMultiArrayAssignRule
7-
tags: [phpstan.rules.rule]
4+
rules:
5+
- Symplify\PHPStanRules\Rules\NoMultiArrayAssignRule

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,7 @@ parameters:
512512
-
513513
message: '#Cannot call method toString\(\) on PhpParser\\Node\\Identifier\|null#'
514514
path: packages/phpstan-rules/src/Rules/RequireConstantInAttributeArgumentRule.php
515+
516+
-
517+
message: '#Cognitive complexity for "Symplify\\PHPStanRules\\Rules\\NoMultiArrayAssignRule\:\:processNode\(\)" is 11, keep it under 8#'
518+
path: packages/phpstan-rules/src/Rules/NoMultiArrayAssignRule.php

0 commit comments

Comments
 (0)