Skip to content

Commit 7cca318

Browse files
authored
[symfony] Add RequireIsGrantedEnumRule (#213)
* [symfony] kick of require is granted rule * [symfony] Add RequireIsGrantedEnumRule
1 parent a920b42 commit 7cca318

File tree

15 files changed

+156
-5
lines changed

15 files changed

+156
-5
lines changed

config/symfony-rules.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ rules:
2323
- Symplify\PHPStanRules\Rules\Symfony\SingleRequiredMethodRule
2424
- Symplify\PHPStanRules\Rules\Symfony\RequiredOnlyInAbstractRule
2525
- Symplify\PHPStanRules\Rules\Symfony\NoConstructorAndRequiredTogetherRule
26+
27+
# attributes
28+
- Symplify\PHPStanRules\Rules\Symfony\RequireIsGrantedEnumRule

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<exclude>tests/Rules/ClassNameRespectsParentSuffixRule/Fixture/</exclude>
1313
<exclude>tests/Rules/PHPUnit/PublicStaticDataProviderRule</exclude>
1414
<exclude>tests/Rules/PHPUnit/NoAssertFuncCallInTestsRule/Fixture</exclude>
15+
<exclude>tests/Rules/PHPUnit/NoMockOnlyTestRule/Fixture/SkipConstraintValidatorTest.php</exclude>
1516
</testsuite>
1617
</phpunit>

src/Enum/RuleIdentifier/SymfonyRuleIdentifier.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ final class SymfonyRuleIdentifier
5353
public const NO_DUPLICATE_ARG_AUTOWIRE_BY_TYPE = 'symfony.noDuplicateArgAutowireByType';
5454

5555
public const NO_SERVICE_SAME_NAME_SET_CLASS = 'symfony.noServiceSameNameSetClass';
56+
57+
public const REQUIRED_IS_GRANTED_ENUM = 'symfony.requiredIsGrantedEnum';
5658
}

src/Enum/SensioClass.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Enum;
6+
7+
final class SensioClass
8+
{
9+
public const IS_GRANTED = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted';
10+
}

src/Enum/SymfonyClass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ final class SymfonyClass
4242
public const VALIDATOR_TEST_CASE = 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase';
4343

4444
public const CONTAINER_CONFIGURATOR = 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator';
45+
46+
public const IS_GRANTED = 'Symfony\Component\Security\Http\Attribute\IsGranted';
4547
}

src/Rules/Doctrine/NoDocumentMockingRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
9-
use PhpParser\Node\Identifier;
109
use PHPStan\Analyser\Scope;
1110
use PHPStan\Rules\Rule;
1211
use PHPStan\Rules\RuleErrorBuilder;

src/Rules/Doctrine/NoGetRepositoryOnServiceRepositoryEntityRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Nette\Utils\Strings;
88
use PhpParser\Node;
99
use PhpParser\Node\Expr\MethodCall;
10-
use PhpParser\Node\Identifier;
1110
use PHPStan\Analyser\Scope;
1211
use PHPStan\Reflection\ClassReflection;
1312
use PHPStan\Reflection\ReflectionProvider;

src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
9-
use PhpParser\Node\Identifier;
109
use PHPStan\Analyser\Scope;
1110
use PHPStan\Rules\IdentifierRuleError;
1211
use PHPStan\Rules\Rule;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Rules\Symfony;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Attribute;
9+
use PhpParser\Node\Scalar\String_;
10+
use PHPStan\Analyser\Scope;
11+
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleErrorBuilder;
13+
use Symplify\PHPStanRules\Enum\RuleIdentifier\SymfonyRuleIdentifier;
14+
use Symplify\PHPStanRules\Enum\SensioClass;
15+
use Symplify\PHPStanRules\Enum\SymfonyClass;
16+
17+
/**
18+
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\RequireIsGrantedEnumRule\RequireIsGrantedEnumRuleTest
19+
*
20+
* @implements Rule<Attribute>
21+
*/
22+
final class RequireIsGrantedEnumRule implements Rule
23+
{
24+
public const ERROR_MESSAGE = 'Instead of "%s" string, use enum constant for #[IsGranted]';
25+
26+
public function getNodeType(): string
27+
{
28+
return Attribute::class;
29+
}
30+
31+
/**
32+
* @param Attribute $node
33+
*/
34+
public function processNode(Node $node, Scope $scope): array
35+
{
36+
if (! in_array($node->name->toString(), [SensioClass::IS_GRANTED, SymfonyClass::IS_GRANTED], true)) {
37+
return [];
38+
}
39+
40+
$isGrantedExpr = $node->args[0]->value;
41+
if (! $isGrantedExpr instanceof String_) {
42+
return [];
43+
}
44+
45+
$identifierRuleError = RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $isGrantedExpr->value))
46+
->identifier(SymfonyRuleIdentifier::REQUIRED_IS_GRANTED_ENUM)
47+
->build();
48+
49+
return [$identifierRuleError];
50+
}
51+
}

src/TypeAnalyzer/CallableTypeAnalyzer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use PhpParser\Node\Expr;
88
use PHPStan\Analyser\Scope;
9-
use PHPStan\Type\CallableType;
10-
use PHPStan\Type\ClosureType;
119
use PHPStan\Type\ObjectType;
1210
use PHPStan\Type\Type;
1311
use PHPStan\Type\TypeCombinator;

0 commit comments

Comments
 (0)