-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
disallowedEnums
, they use DisallowedConstant
internally
`disallowedEnums` are configured with `enum` & `case` fields which are just aliases for `constant` & `class` fields, and these new fields would also work for constants but luckily `parametersSchema` config in `extension.neon` would prevent it. Besides the aliases, this adds tests & docs, mostly.
- Loading branch information
Showing
10 changed files
with
192 additions
and
12 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
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,18 @@ | ||
## Disallowing enums | ||
|
||
Similar to disallowing constants, enums have some limitations, because internally, enums are disallowed as constants. | ||
|
||
First, disallowing enums doesn't support wildcards. The only use case I could think of would be disallowing all cases, e.g. `Enum::*`, which can be achieved by adding the enum to `disallowedClasses`. | ||
|
||
Second, enums have to be specified using two keys: `enum` and `case`: | ||
```neon | ||
parameters: | ||
disallowedEnums: | ||
- | ||
enum: 'Suit' | ||
case: 'Hearts' | ||
message: 'use Diamonds instead' | ||
``` | ||
This is to prevent the enum case being treated like a real enum by the config parser. | ||
|
||
Both pure enums and backed enums are supported. |
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
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
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,100 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Usages; | ||
|
||
use Enums\BackedEnum; | ||
use Enums\Enum; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors; | ||
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class ClassConstantEnumUsagesTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$container = self::getContainer(); | ||
return new ClassConstantUsages( | ||
$container->getByType(DisallowedConstantRuleErrors::class), | ||
$container->getByType(DisallowedConstantFactory::class), | ||
$container->getByType(TypeResolver::class), | ||
$container->getByType(Formatter::class), | ||
[ | ||
[ | ||
'enum' => Enum::class, | ||
'constant' => 'ENUM_CONST', | ||
], | ||
[ | ||
'enum' => Enum::class, | ||
'case' => 'Foo', | ||
], | ||
[ | ||
'class' => Enum::class, | ||
'constant' => 'Bar', | ||
], | ||
[ | ||
'enum' => BackedEnum::class, | ||
'constant' => 'ENUM_CONST', | ||
], | ||
[ | ||
'enum' => BackedEnum::class, | ||
'case' => 'Waldo', | ||
], | ||
[ | ||
'class' => BackedEnum::class, | ||
'constant' => 'Quux', | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
public function testRule(): void | ||
{ | ||
// Based on the configuration above, no errors in this file: | ||
$this->analyse([__DIR__ . '/../src/Enums.php'], [ | ||
[ | ||
// expect this error message: | ||
'Using Enums\Enum::ENUM_CONST is forbidden.', | ||
// on this line: | ||
16, | ||
], | ||
[ | ||
'Using Enums\Enum::Foo is forbidden.', | ||
17, | ||
], | ||
[ | ||
'Using Enums\Enum::Bar is forbidden.', | ||
18, | ||
], | ||
[ | ||
'Using Enums\BackedEnum::ENUM_CONST is forbidden.', | ||
31, | ||
], | ||
[ | ||
'Using Enums\BackedEnum::Waldo is forbidden.', | ||
32, | ||
], | ||
[ | ||
'Using Enums\BackedEnum::Quux is forbidden.', | ||
33, | ||
], | ||
]); | ||
} | ||
|
||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Enums; | ||
|
||
enum Enum | ||
{ | ||
|
||
public const ENUM_CONST = true; | ||
case Foo; | ||
case Bar; | ||
case Baz; | ||
|
||
} | ||
|
||
Enum::ENUM_CONST; | ||
Enum::Foo; | ||
Enum::Bar; | ||
Enum::Baz; | ||
|
||
enum BackedEnum: int | ||
{ | ||
|
||
public const ENUM_CONST = true; | ||
case Waldo = 1; | ||
case Quux = 2; | ||
case Fred = 3; | ||
|
||
} | ||
|
||
BackedEnum::ENUM_CONST; | ||
BackedEnum::Waldo; | ||
BackedEnum::Quux; | ||
BackedEnum::Fred; |