Skip to content

Commit

Permalink
Implement EnumCaseNamingRule
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Dec 19, 2024
1 parent 9420dbc commit 8b6ff25
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Nexus/PHPStan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following rules will automatically be enabled once the `extension.neon` is a
* Class properties - camelCase format with no underscores
* Class methods - camelCase format with no underscores, except for magic methods where double
underscores are allowed
* Enum cases - PascalCase format
* Functions - lower_snake_case format
2. `@phpstan-`-prefixed PHPDoc tags should be avoided where possible:
* `@phpstan-var` => `@var`
Expand Down
48 changes: 48 additions & 0 deletions src/Nexus/PHPStan/Rules/Enums/EnumCaseNamingRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Nexus framework.
*
* (c) John Paul E. Balandan, CPA <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Nexus\PHPStan\Rules\Enums;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node\Stmt\EnumCase>
*/
final class EnumCaseNamingRule implements Rule
{
public function getNodeType(): string
{
return Node\Stmt\EnumCase::class;
}

public function processNode(Node $node, Scope $scope): array
{
$enumCaseName = $node->name->name;

if (preg_match('/(?:[A-Z][a-z]+)+/', $enumCaseName) !== 1) {
return [
RuleErrorBuilder::message(\sprintf(
'Enum case "%s" should be in PascalCase format.',
$enumCaseName,
))
->identifier('nexus.enumCaseNaming')
->build(),
];
}

return [];
}
}
1 change: 1 addition & 0 deletions src/Nexus/PHPStan/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rules:
- Nexus\PHPStan\Rules\CleanCode\AssignExprInCondRule
- Nexus\PHPStan\Rules\CleanCode\DisallowedErrorSuppressionOperatorRule
- Nexus\PHPStan\Rules\Constants\ClassConstantNamingRule
- Nexus\PHPStan\Rules\Enums\EnumCaseNamingRule
- Nexus\PHPStan\Rules\Functions\FunctionNamingRule
- Nexus\PHPStan\Rules\Methods\MethodNamingRule
- Nexus\PHPStan\Rules\PhpDoc\DisallowedPhpstanDocTagRule
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Rules/Enums/EnumCaseNamingRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Nexus framework.
*
* (c) John Paul E. Balandan, CPA <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Nexus\Tests\PHPStan\Rules\Enums;

use Nexus\PHPStan\Rules\Enums\EnumCaseNamingRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*
* @extends RuleTestCase<EnumCaseNamingRule>
*/
#[CoversClass(EnumCaseNamingRule::class)]
#[Group('unit-test')]
final class EnumCaseNamingRuleTest extends RuleTestCase
{
public function testRule(): void
{
$this->analyse([__DIR__.'/data/enum-case-naming.php'], [
[
'Enum case "HEARTSSUIT" should be in PascalCase format.',
10,
],
[
'Enum case "diamonds_suit" should be in PascalCase format.',
11,
],
[
'Enum case "clubs_SUIT" should be in PascalCase format.',
12,
],
]);
}

protected function getRule(): Rule
{
return new EnumCaseNamingRule();
}
}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Enums/data/enum-case-naming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Nexus\Tests\PHPStan\Rules\Enums;

enum CardSuits
{
case SpadesSuit;
case HEARTSSUIT;
case diamonds_suit;
case clubs_SUIT;
}

0 comments on commit 8b6ff25

Please sign in to comment.