Skip to content

Commit f8e46f8

Browse files
committed
skip custom functions in NoBareAndSecurityIsGrantedContentsRule
1 parent 0a60606 commit f8e46f8

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Rules/Symfony/NoBareAndSecurityIsGrantedContentsRule.php

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

54+
if ($this->usesCustomFunctoins($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 usesCustomFunctoins(String_ $string): bool
66+
{
67+
$joinedItems = preg_split('# (and|&&|or) #', $string->value, -1, PREG_SPLIT_NO_EMPTY);
68+
69+
foreach ($joinedItems as $joinedItem) {
70+
if (str_contains($joinedItem, 'is_granted')) {
71+
continue;
72+
}
73+
74+
if (str_contains($joinedItem, 'has_role')) {
75+
continue;
76+
}
77+
78+
return true;
79+
}
80+
81+
return false;
82+
}
6083
}
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)