Skip to content

Commit

Permalink
Add PHPStan extension with NoAssignmentInIfRule
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jul 17, 2023
1 parent 39b3d0c commit 9bcba5a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).

## Unreleased

## v1.8.0

### Added

- Add PHPStan extension with `NoAssignmentInIfRule`

## v1.7.0

### Added
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ composer require mll-lab/php-utils

See [tests](tests).

### PHPStan extension

This library provides a PHPStan extension that is either registered through [PHPStan Extension Installer](https://github.com/phpstan/extension-installer)
or registered manually by adding the following to your `phpstan.neon`:

```diff
includes:
+- vendor/mll-lab/php-utils/phpstan-extension.neon
```

## Changelog

See [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@
"infection/extension-installer": true
},
"sort-packages": true
},
"extra": {
"phpstan": {
"includes": [
"phpstan-extension.neon"
]
}
}
}
3 changes: 3 additions & 0 deletions phpstan-extension.neon
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 ]
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
includes:
- phpstan-extension.neon
parameters:
level: max
paths:
Expand Down
40 changes: 40 additions & 0 deletions src/PHPStan/Rules/NoAssignmentInIfRule.php
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.'];
}
}

0 comments on commit 9bcba5a

Please sign in to comment.