Skip to content

Commit 97ceb64

Browse files
authored
skip custom functions in NoBareAndSecurityIsGrantedContentsRule (#220)
1 parent 0a60606 commit 97ceb64

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/Rules/Symfony/NoBareAndSecurityIsGrantedContentsRule.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,37 @@ public function processNode(Node $node, Scope $scope): array
5151
return [];
5252
}
5353

54+
if ($this->usesCustomFunctios($attributeExpr)) {
55+
return [];
56+
}
57+
5458
$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
5559
->identifier(SymfonyRuleIdentifier::REQUIRED_IS_GRANTED_ENUM)
5660
->build();
5761

5862
return [$identifierRuleError];
5963
}
64+
65+
private function usesCustomFunctios(String_ $string): bool
66+
{
67+
$joinedItems = preg_split('# (and|&&|or) #', $string->value, -1, PREG_SPLIT_NO_EMPTY);
68+
69+
if ($joinedItems === false) {
70+
return false;
71+
}
72+
73+
foreach ($joinedItems as $joinedItem) {
74+
if (str_contains($joinedItem, 'is_granted')) {
75+
continue;
76+
}
77+
78+
if (str_contains($joinedItem, 'has_role')) {
79+
continue;
80+
}
81+
82+
return true;
83+
}
84+
85+
return false;
86+
}
6087
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoBareAndSecurityIsGrantedContentsRule\Fixture;
6+
7+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
8+
9+
#[IsGranted('custom_check("some_resource") && custom_check("another_resource")')]
10+
final class SkipCustomFunction
11+
{
12+
public function run()
13+
{
14+
}
15+
}

tests/Rules/Symfony/NoBareAndSecurityIsGrantedContentsRule/NoBareAndSecurityIsGrantedContentsRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static function provideData(): Iterator
3232
]];
3333

3434
yield [__DIR__ . '/Fixture/SkipInnerOr.php', []];
35+
yield [__DIR__ . '/Fixture/SkipCustomFunction.php', []];
3536
yield [__DIR__ . '/Fixture/SkipSplitOne.php', []];
3637
}
3738

0 commit comments

Comments
 (0)