File tree Expand file tree Collapse file tree 5 files changed +116
-0
lines changed
tests/PHPStan/Rules/Enums Expand file tree Collapse file tree 5 files changed +116
-0
lines changed Original file line number Diff line number Diff 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
36372. `@phpstan-`-prefixed PHPDoc tags should be avoided where possible :
3738 * `@phpstan-var` => `@var`
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments