-
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.
- Loading branch information
1 parent
9420dbc
commit 8b6ff25
Showing
5 changed files
with
116 additions
and
0 deletions.
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,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 []; | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nexus\Tests\PHPStan\Rules\Enums; | ||
|
||
enum CardSuits | ||
{ | ||
case SpadesSuit; | ||
case HEARTSSUIT; | ||
case diamonds_suit; | ||
case clubs_SUIT; | ||
} |