This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHPStanRules] Skip multi ifs (#3855)
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
cf125cf
commit ce091a8
Showing
6 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/phpstan-rules/src/NodeAnalyzer/IfEnumAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...pstan-rules/tests/Rules/Spotter/IfElseToMatchSpotterRule/Fixture/SkipAboveSameCompare.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters