generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PHPStan extension with
NoAssignmentInIfRule
- Loading branch information
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
services: | ||
- class: MLL\Utils\PHPStan\Rules\NoAssignmentInIfRule | ||
tags: [ phpstan.rules.rule ] |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
includes: | ||
- phpstan-extension.neon | ||
parameters: | ||
level: max | ||
paths: | ||
|
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,40 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\PHPStan\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\NodeFinder; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* Inspired by https://github.com/odan/phpstan-rules/blob/388db4cdf7c99e2978f20b1ed8801d748984812a/src/Rules/AssignmentInConditionRule.php. | ||
* | ||
* @implements Rule<If_> | ||
*/ | ||
class NoAssignmentInIfRule implements Rule | ||
{ | ||
private NodeFinder $nodeFinder; | ||
|
||
public function __construct() | ||
{ | ||
$this->nodeFinder = new NodeFinder(); | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return If_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$assignNode = $this->nodeFinder->findFirstInstanceOf($node->cond, Assign::class); | ||
if (! $assignNode instanceof Assign) { | ||
return []; | ||
} | ||
|
||
return ['Assignment in conditional expression is not allowed.']; | ||
} | ||
} |