Skip to content

Commit b5f953a

Browse files
authored
add ampersand support (#716)
1 parent 456b20a commit b5f953a

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\SplitAndSecurityAttributeToIsGrantedRector\Fixture;
4+
5+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
6+
7+
#[Security("is_granted('ROLE_USER') && is_granted('ROLE_ADMIN')")]
8+
final class WithAmpersandAttributes
9+
{
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\SplitAndSecurityAttributeToIsGrantedRector\Fixture;
17+
18+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
19+
20+
#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted('ROLE_USER')]
21+
#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted('ROLE_ADMIN')]
22+
final class WithAmpersandAttributes
23+
{
24+
}
25+
26+
?>

rules/CodeQuality/Rector/Class_/SplitAndSecurityAttributeToIsGrantedRector.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Rector\Symfony\CodeQuality\Rector\Class_;
66

7-
use PhpParser\Node\Name\FullyQualified;
87
use Nette\Utils\Strings;
98
use PhpParser\Node;
109
use PhpParser\Node\Arg;
1110
use PhpParser\Node\Attribute;
1211
use PhpParser\Node\AttributeGroup;
12+
use PhpParser\Node\Name\FullyQualified;
1313
use PhpParser\Node\Scalar\String_;
1414
use PhpParser\Node\Stmt\Class_;
1515
use PhpParser\Node\Stmt\ClassMethod;
@@ -82,11 +82,16 @@ public function refactor(Node $node): ?Node
8282
}
8383

8484
// we look for "and"s
85-
if (! str_contains($content, ' and')) {
85+
if (! str_contains($content, ' and ') && ! str_contains($content, ' && ')) {
8686
continue;
8787
}
8888

89-
$andItems = explode(' and ', $content);
89+
// split by && and "and"
90+
if (str_contains($content, ' && ')) {
91+
$andItems = explode(' && ', $content);
92+
} else {
93+
$andItems = explode(' and ', $content);
94+
}
9095

9196
$accessRights = [];
9297

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

107112
foreach ($accessRights as $accessRight) {
108113
$attributeGroup = new AttributeGroup([
109-
new Attribute(new FullyQualified(IsGranted::class), [
110-
new Arg(new String_($accessRight)),
111-
]),
114+
new Attribute(new FullyQualified(IsGranted::class), [new Arg(new String_($accessRight))]),
112115
]);
113116

114117
$node->attrGroups[] = $attributeGroup;

0 commit comments

Comments
 (0)