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

Commit

Permalink
[PHPStanRules] Skip multi ifs (#3855)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user authored Dec 23, 2021
1 parent cf125cf commit ce091a8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- Symplify\PHPStanRules\NodeAnalyzer\Duplicates\DuplicatedStringArgValueResolver
- Symplify\PHPStanRules\NodeAnalyzer\EnumAnalyzer
- Symplify\PHPStanRules\NodeAnalyzer\IfElseBranchAnalyzer
- Symplify\PHPStanRules\NodeAnalyzer\IfEnumAnalyzer
- Symplify\PHPStanRules\NodeAnalyzer\IfResemblingMatchAnalyzer
- Symplify\PHPStanRules\NodeAnalyzer\IfReturnAnalyzer
- Symplify\PHPStanRules\NodeAnalyzer\InitializedExprAnalyzer
Expand Down
3 changes: 3 additions & 0 deletions packages/phpstan-rules/src/NodeAnalyzer/CacheIfAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function isDefaultNullAssign(If_ $if): bool
return $this->simpleNodeFinder->hasByTypes($if->cond, [Empty_::class, Isset_::class]);
}

/**
* @param BinaryOp[] $binaryOps
*/
private function hasIdenticalToNull(array $binaryOps): bool
{
foreach ($binaryOps as $binaryOp) {
Expand Down
32 changes: 32 additions & 0 deletions packages/phpstan-rules/src/NodeAnalyzer/IfEnumAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Stmt\If_;
use Symplify\Astral\NodeFinder\SimpleNodeFinder;
use Symplify\Astral\ValueObject\AttributeKey;

final class IfEnumAnalyzer
{
public function __construct(
private SimpleNodeFinder $simpleNodeFinder
) {
}

public function isMultipleIf(Node $node): bool
{
if (! $node instanceof If_) {
return false;
}

$parent = $node->getAttribute(AttributeKey::PARENT);

$ifs = $this->simpleNodeFinder->findByType($parent, If_::class);

// might be dangerous
return count($ifs) > 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symplify\PHPStanRules\Rules\Spotter;

use Symplify\PHPStanRules\NodeAnalyzer\IfEnumAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Else_;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function __construct(
private IfElseBranchAnalyzer $ifElseBranchAnalyzer,
private IfResemblingMatchAnalyzer $ifResemblingMatchAnalyzer,
private CacheIfAnalyzer $cacheIfAnalyzer,
private IfEnumAnalyzer $ifEnumAnalyzer
) {
}

Expand Down Expand Up @@ -67,9 +69,15 @@ public function process(Node $node, Scope $scope): array
return [];
}

// is multiple if with same variable - skip it, we need else/if here
if ($this->ifEnumAnalyzer->isMultipleIf($branch)) {
return [];
}

// the conditioned parameters must be the same
if ($branch instanceof If_ || $branch instanceof ElseIf_) {
$ifsAndConds[] = new IfAndCondExpr($branch->stmts[0], $branch->cond);

continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Spotter\IfElseToMatchSpotterRule\Fixture;

use Amateri\Chat\Enum\ChatRoomCategory;
use Amateri\Chat\Enum\ChatRoomType;

final class SkipAboveSameCompare
{
public function run(object $search)
{
$cond = [];
if ($search->getIds() !== null) {
$cond[] = $search->getIds();
}
if ($search->getNames() === false) {
$cond[] = ['names'];
}
return $cond;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function provideData(): Iterator
yield [__DIR__ . '/Fixture/MultiBinaryWithCompare.php', [[IfElseToMatchSpotterRule::ERROR_MESSAGE, 13]]];
yield [__DIR__ . '/Fixture/MultiMultiBinaryWithCompare.php', [[IfElseToMatchSpotterRule::ERROR_MESSAGE, 13]]];

yield [__DIR__ . '/Fixture/SkipAboveSameCompare.php', []];
yield [__DIR__ . '/Fixture/SkipMultiBinaryAnd.php', []];
yield [__DIR__ . '/Fixture/SkipMultiBinary.php', []];
yield [__DIR__ . '/Fixture/SkipVariableNullCompare.php', []];
Expand Down

0 comments on commit ce091a8

Please sign in to comment.