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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\SplitAndSecurityAttributeToIsGrantedRector\Fixture;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

#[Security("is_granted('ROLE_USER') && is_granted('ROLE_ADMIN')")]
final class WithAmpersandAttributes
{
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\SplitAndSecurityAttributeToIsGrantedRector\Fixture;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted('ROLE_USER')]
#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted('ROLE_ADMIN')]
final class WithAmpersandAttributes
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Rector\Symfony\CodeQuality\Rector\Class_;

use PhpParser\Node\Name\FullyQualified;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -82,11 +82,16 @@ public function refactor(Node $node): ?Node
}

// we look for "and"s
if (! str_contains($content, ' and')) {
if (! str_contains($content, ' and ') && ! str_contains($content, ' && ')) {
continue;
}

$andItems = explode(' and ', $content);
// split by && and "and"
if (str_contains($content, ' && ')) {
$andItems = explode(' && ', $content);
} else {
$andItems = explode(' and ', $content);
}

$accessRights = [];

Expand All @@ -106,9 +111,7 @@ public function refactor(Node $node): ?Node

foreach ($accessRights as $accessRight) {
$attributeGroup = new AttributeGroup([
new Attribute(new FullyQualified(IsGranted::class), [
new Arg(new String_($accessRight)),
]),
new Attribute(new FullyQualified(IsGranted::class), [new Arg(new String_($accessRight))]),
]);

$node->attrGroups[] = $attributeGroup;
Expand Down
Loading