Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Rules/Symfony/NoBareAndSecurityIsGrantedContentsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,37 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($this->usesCustomFunctios($attributeExpr)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

return [];
}

$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(SymfonyRuleIdentifier::REQUIRED_IS_GRANTED_ENUM)
->build();

return [$identifierRuleError];
}

private function usesCustomFunctios(String_ $string): bool
{
$joinedItems = preg_split('# (and|&&|or) #', $string->value, -1, PREG_SPLIT_NO_EMPTY);

if ($joinedItems === false) {
return false;
}

foreach ($joinedItems as $joinedItem) {
if (str_contains($joinedItem, 'is_granted')) {
continue;
}

if (str_contains($joinedItem, 'has_role')) {
continue;
}

return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoBareAndSecurityIsGrantedContentsRule\Fixture;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

#[IsGranted('custom_check("some_resource") && custom_check("another_resource")')]
final class SkipCustomFunction
{
public function run()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function provideData(): Iterator
]];

yield [__DIR__ . '/Fixture/SkipInnerOr.php', []];
yield [__DIR__ . '/Fixture/SkipCustomFunction.php', []];
yield [__DIR__ . '/Fixture/SkipSplitOne.php', []];
}

Expand Down