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.
feat: Add PHPStan-Rule `MLL\Utils\PHPStan\Rules\VariableNameIdToIDRul…
…e` (#40)
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\PHPStan\Rules; | ||
|
||
use Illuminate\Support\Str; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** @implements Rule<Variable> */ | ||
class VariableNameIdToIDRule implements Rule | ||
{ | ||
/** Lists words or phrases that contain "Id" but are fine. */ | ||
protected const FALSE_POSITIVES = ['Identifier']; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Variable::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$nodeName = $node->name; | ||
|
||
if (is_string($nodeName) | ||
&& static::containsWrongIDCapitalization($nodeName) | ||
) { | ||
$expectedName = static::fixIDCapitalization($nodeName); | ||
|
||
return [ | ||
RuleErrorBuilder::message(<<<TXT | ||
Variable name "\${$nodeName}" should use "ID" instead of "Id", rename it to "\${$expectedName}". | ||
TXT)->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public static function containsWrongIDCapitalization(string $nodeName): bool | ||
{ | ||
return \Safe\preg_match('/Id/', $nodeName) === 1 | ||
&& ! Str::contains($nodeName, self::FALSE_POSITIVES); | ||
} | ||
|
||
public static function fixIDCapitalization(string $nodeName): string | ||
{ | ||
if ($nodeName === 'Id') { | ||
return 'id'; | ||
} | ||
|
||
return str_replace('Id', 'ID', $nodeName); | ||
} | ||
} |
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,58 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\Tests\PHPStan\Rules; | ||
|
||
use MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class VariableNameIdToIDRuleTest extends TestCase | ||
{ | ||
/** @dataProvider wrongID */ | ||
#[DataProvider('wrongID')] | ||
public function testRecognizesWrongCapitalizations(string $variableName): void | ||
{ | ||
self::assertTrue(VariableNameIdToIDRule::containsWrongIDCapitalization($variableName)); | ||
} | ||
|
||
/** @return iterable<array{string}> */ | ||
public static function wrongID(): iterable | ||
{ | ||
yield ['Id']; | ||
yield ['labId']; | ||
yield ['labIds']; | ||
} | ||
|
||
/** @dataProvider correctID */ | ||
#[DataProvider('correctID')] | ||
public function testAllowsCorrectCapitalizations(string $variableName): void | ||
{ | ||
self::assertFalse(VariableNameIdToIDRule::containsWrongIDCapitalization($variableName)); | ||
} | ||
|
||
/** @return iterable<array{string}> */ | ||
public static function correctID(): iterable | ||
{ | ||
yield ['id']; | ||
yield ['ids']; | ||
yield ['test_id']; | ||
yield ['labID']; | ||
yield ['labIDs']; | ||
yield ['testIdentifier']; | ||
} | ||
|
||
/** @dataProvider wrongToRight */ | ||
#[DataProvider('wrongToRight')] | ||
public function testFixIDCapitalization(string $wrong, string $right): void | ||
{ | ||
self::assertSame($right, VariableNameIdToIDRule::fixIDCapitalization($wrong)); | ||
} | ||
|
||
/** @return iterable<array{string, string}> */ | ||
public static function wrongToRight(): iterable | ||
{ | ||
yield ['Id', 'id']; | ||
yield ['labId', 'labID']; | ||
yield ['labIds', 'labIDs']; | ||
} | ||
} |
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