Skip to content

Commit

Permalink
[Php80] Handle single quoted is_granted on AnnotationToAttributeRector (
Browse files Browse the repository at this point in the history
#2842)

Co-authored-by: Ghislain <ghislain@gambling-affiliation.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Aug 26, 2022
1 parent 7215c28 commit 21695e7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpAttribute\Enum\DocTagNodeState;
Expand All @@ -20,6 +24,10 @@ final class ArrayAnnotationToAttributeMapper implements AnnotationToAttributeMap
{
private AnnotationToAttributeMapper $annotationToAttributeMapper;

public function __construct(private readonly ValueResolver $valueResolver)
{
}

/**
* Avoid circular reference
*/
Expand Down Expand Up @@ -55,7 +63,8 @@ public function map($value): Expr
}

if ($valueExpr instanceof ArrayItem) {
$arrayItems[] = $valueExpr;
$valueExpr = $this->resolveValueExprWithSingleQuoteHandling($valueExpr);
$arrayItems[] = $this->resolveValueExprWithSingleQuoteHandling($valueExpr);
} else {
$keyExpr = null;
if (! is_int($key)) {
Expand All @@ -70,6 +79,19 @@ public function map($value): Expr
return new Array_($arrayItems);
}

private function resolveValueExprWithSingleQuoteHandling(ArrayItem $arrayItem): ArrayItem
{
if ($arrayItem->key === null && $arrayItem->value instanceof ClassConstFetch && $arrayItem->value->class instanceof Name && str_contains(
(string) $arrayItem->value->class,
"'"
)) {
$arrayItem->value = new String_($this->valueResolver->getValue($arrayItem->value));
return $arrayItem;
}

return $arrayItem;
}

private function isRemoveArrayPlaceholder(mixed $value): bool
{
if (! is_array($value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

final class SymfonySecurity
{
/**
* @Security("is_granted(constant('\\Rector\\Tests\\Php80\\Rector\\Class_\\AnnotationToAttributeRector\\Source\\ConstantReference::FIRST_NAME'))")
*/
public function action()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

final class SymfonySecurity
{
#[Security('is_granted(constant(\'\\\Rector\\\Tests\\\Php80\\\Rector\\\Class_\\\AnnotationToAttributeRector\\\Source\\\ConstantReference::FIRST_NAME\'))')]
public function action()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

final class SymfonySecurity2
{
/**
* @Security("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')")
*/
public function action()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

final class SymfonySecurity2
{
#[Security("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')")]
public function action()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
new AnnotationToAttribute(
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\UseAlias\TestOther'
),
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\Security'),
]);
};

0 comments on commit 21695e7

Please sign in to comment.