Skip to content

Commit 8b6ff25

Browse files
committed
Implement EnumCaseNamingRule
1 parent 9420dbc commit 8b6ff25

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

src/Nexus/PHPStan/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The following rules will automatically be enabled once the `extension.neon` is a
3232
* Class properties - camelCase format with no underscores
3333
* Class methods - camelCase format with no underscores, except for magic methods where double
3434
underscores are allowed
35+
* Enum cases - PascalCase format
3536
* Functions - lower_snake_case format
3637
2. `@phpstan-`-prefixed PHPDoc tags should be avoided where possible:
3738
* `@phpstan-var` => `@var`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus framework.
7+
*
8+
* (c) John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\PHPStan\Rules\Enums;
15+
16+
use PhpParser\Node;
17+
use PHPStan\Analyser\Scope;
18+
use PHPStan\Rules\Rule;
19+
use PHPStan\Rules\RuleErrorBuilder;
20+
21+
/**
22+
* @implements Rule<Node\Stmt\EnumCase>
23+
*/
24+
final class EnumCaseNamingRule implements Rule
25+
{
26+
public function getNodeType(): string
27+
{
28+
return Node\Stmt\EnumCase::class;
29+
}
30+
31+
public function processNode(Node $node, Scope $scope): array
32+
{
33+
$enumCaseName = $node->name->name;
34+
35+
if (preg_match('/(?:[A-Z][a-z]+)+/', $enumCaseName) !== 1) {
36+
return [
37+
RuleErrorBuilder::message(\sprintf(
38+
'Enum case "%s" should be in PascalCase format.',
39+
$enumCaseName,
40+
))
41+
->identifier('nexus.enumCaseNaming')
42+
->build(),
43+
];
44+
}
45+
46+
return [];
47+
}
48+
}

src/Nexus/PHPStan/extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ rules:
22
- Nexus\PHPStan\Rules\CleanCode\AssignExprInCondRule
33
- Nexus\PHPStan\Rules\CleanCode\DisallowedErrorSuppressionOperatorRule
44
- Nexus\PHPStan\Rules\Constants\ClassConstantNamingRule
5+
- Nexus\PHPStan\Rules\Enums\EnumCaseNamingRule
56
- Nexus\PHPStan\Rules\Functions\FunctionNamingRule
67
- Nexus\PHPStan\Rules\Methods\MethodNamingRule
78
- Nexus\PHPStan\Rules\PhpDoc\DisallowedPhpstanDocTagRule
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus framework.
7+
*
8+
* (c) John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Tests\PHPStan\Rules\Enums;
15+
16+
use Nexus\PHPStan\Rules\Enums\EnumCaseNamingRule;
17+
use PHPStan\Rules\Rule;
18+
use PHPStan\Testing\RuleTestCase;
19+
use PHPUnit\Framework\Attributes\CoversClass;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*
25+
* @extends RuleTestCase<EnumCaseNamingRule>
26+
*/
27+
#[CoversClass(EnumCaseNamingRule::class)]
28+
#[Group('unit-test')]
29+
final class EnumCaseNamingRuleTest extends RuleTestCase
30+
{
31+
public function testRule(): void
32+
{
33+
$this->analyse([__DIR__.'/data/enum-case-naming.php'], [
34+
[
35+
'Enum case "HEARTSSUIT" should be in PascalCase format.',
36+
10,
37+
],
38+
[
39+
'Enum case "diamonds_suit" should be in PascalCase format.',
40+
11,
41+
],
42+
[
43+
'Enum case "clubs_SUIT" should be in PascalCase format.',
44+
12,
45+
],
46+
]);
47+
}
48+
49+
protected function getRule(): Rule
50+
{
51+
return new EnumCaseNamingRule();
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nexus\Tests\PHPStan\Rules\Enums;
6+
7+
enum CardSuits
8+
{
9+
case SpadesSuit;
10+
case HEARTSSUIT;
11+
case diamonds_suit;
12+
case clubs_SUIT;
13+
}

0 commit comments

Comments
 (0)