Skip to content

Commit

Permalink
feat: Add PHPStan-Rule MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bigelmayr committed Nov 14, 2024
1 parent 3371142 commit 0df23ef
Show file tree
Hide file tree
Showing 2 changed files with 47 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

## v5.7.0

### Added

- Add PHPStan-Rule `MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule`

## v5.6.0

### Added
Expand Down
41 changes: 41 additions & 0 deletions src/PHPStan/Rules/VariableNameIdToIDRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace MLL\Utils\PHPStan\Rules;

use Illuminate\Support\Str;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node\Expr\Variable>
*/
class VariableNameIdToIDRule implements Rule
{
private const WHITELIST = ['Identifier'];

public function getNodeType(): string
{
return Node\Expr\Variable::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (
$node instanceof Node\Expr\Variable

Check failure on line 26 in src/PHPStan/Rules/VariableNameIdToIDRule.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

Instanceof between PhpParser\Node\Expr\Variable and PhpParser\Node\Expr\Variable will always evaluate to true.

Check failure on line 26 in src/PHPStan/Rules/VariableNameIdToIDRule.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

Only booleans are allowed in &&, int given on the right side.
&& is_string($node->name)
&& \Safe\preg_match('/Id/', $node->name)
&& ! Str::contains($node->name, self::WHITELIST)
) {
return [
RuleErrorBuilder::message(sprintf(
'Variable name "$%s" should use "ID" instead of "Id".',
$node->name,
))->build(),
];
}

return [];
}
}

0 comments on commit 0df23ef

Please sign in to comment.