Skip to content

Commit 4f745da

Browse files
authored
feat: Add PHPStan-Rule MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule (#40)
1 parent 3371142 commit 4f745da

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).
99

1010
## Unreleased
1111

12+
## v5.7.0
13+
14+
### Added
15+
16+
- Add PHPStan-Rule `MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule`
17+
1218
## v5.6.0
1319

1420
### Added
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\Utils\PHPStan\Rules;
4+
5+
use Illuminate\Support\Str;
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr\Variable;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
12+
/** @implements Rule<Variable> */
13+
class VariableNameIdToIDRule implements Rule
14+
{
15+
/** Lists words or phrases that contain "Id" but are fine. */
16+
protected const FALSE_POSITIVES = ['Identifier'];
17+
18+
public function getNodeType(): string
19+
{
20+
return Variable::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
$nodeName = $node->name;
26+
27+
if (is_string($nodeName)
28+
&& static::containsWrongIDCapitalization($nodeName)
29+
) {
30+
$expectedName = static::fixIDCapitalization($nodeName);
31+
32+
return [
33+
RuleErrorBuilder::message(<<<TXT
34+
Variable name "\${$nodeName}" should use "ID" instead of "Id", rename it to "\${$expectedName}".
35+
TXT)->build(),
36+
];
37+
}
38+
39+
return [];
40+
}
41+
42+
public static function containsWrongIDCapitalization(string $nodeName): bool
43+
{
44+
return \Safe\preg_match('/Id/', $nodeName) === 1
45+
&& ! Str::contains($nodeName, self::FALSE_POSITIVES);
46+
}
47+
48+
public static function fixIDCapitalization(string $nodeName): string
49+
{
50+
if ($nodeName === 'Id') {
51+
return 'id';
52+
}
53+
54+
return str_replace('Id', 'ID', $nodeName);
55+
}
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\Utils\Tests\PHPStan\Rules;
4+
5+
use MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class VariableNameIdToIDRuleTest extends TestCase
10+
{
11+
/** @dataProvider wrongID */
12+
#[DataProvider('wrongID')]
13+
public function testRecognizesWrongCapitalizations(string $variableName): void
14+
{
15+
self::assertTrue(VariableNameIdToIDRule::containsWrongIDCapitalization($variableName));
16+
}
17+
18+
/** @return iterable<array{string}> */
19+
public static function wrongID(): iterable
20+
{
21+
yield ['Id'];
22+
yield ['labId'];
23+
yield ['labIds'];
24+
}
25+
26+
/** @dataProvider correctID */
27+
#[DataProvider('correctID')]
28+
public function testAllowsCorrectCapitalizations(string $variableName): void
29+
{
30+
self::assertFalse(VariableNameIdToIDRule::containsWrongIDCapitalization($variableName));
31+
}
32+
33+
/** @return iterable<array{string}> */
34+
public static function correctID(): iterable
35+
{
36+
yield ['id'];
37+
yield ['ids'];
38+
yield ['test_id'];
39+
yield ['labID'];
40+
yield ['labIDs'];
41+
yield ['testIdentifier'];
42+
}
43+
44+
/** @dataProvider wrongToRight */
45+
#[DataProvider('wrongToRight')]
46+
public function testFixIDCapitalization(string $wrong, string $right): void
47+
{
48+
self::assertSame($right, VariableNameIdToIDRule::fixIDCapitalization($wrong));
49+
}
50+
51+
/** @return iterable<array{string, string}> */
52+
public static function wrongToRight(): iterable
53+
{
54+
yield ['Id', 'id'];
55+
yield ['labId', 'labID'];
56+
yield ['labIds', 'labIDs'];
57+
}
58+
}

tests/Tecan/TecanProtocolTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testProtocolUuidName(): void
6565
self::assertTrue(Str::isUuid($fileName));
6666

6767
$fileSuffix = Str::after($tecanProtocol->fileName(), $fileName);
68-
self::assertSame($fileSuffix, TecanProtocol::GEMINI_WORKLIST_FILENAME_SUFFIX);
68+
self::assertSame(TecanProtocol::GEMINI_WORKLIST_FILENAME_SUFFIX, $fileSuffix);
6969
}
7070

7171
public function testProtocolWithForFourTips(): void

0 commit comments

Comments
 (0)