Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Symfony73] Skip different object on GetFunctionsToAsTwigFunctionAttributeRector #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
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,23 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFunctionsToAsTwigFunctionAttributeRector\Fixture;

use Twig\Extension\AbstractExtension;

final class SkipDifferentObject extends AbstractExtension
{
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('some_function', [$differentObject, 'someFunction']),
new \Twig\TwigFunction('some_function', $differentObject->someFunction(...)),
];
}

public function someFunction($value)
{
return $value;
}
}

?>
12 changes: 9 additions & 3 deletions rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Symfony\Symfony73\NodeAnalyzer\LocalArrayMethodCallableMatcher;
use Rector\Symfony\Symfony73\NodeRemover\ReturnEmptyArrayMethodRemover;

Expand All @@ -26,14 +27,15 @@
public function __construct(
private LocalArrayMethodCallableMatcher $localArrayMethodCallableMatcher,
private ReturnEmptyArrayMethodRemover $returnEmptyArrayMethodRemover,
private ReflectionProvider $reflectionProvider,
private ReflectionProvider $reflectionProvider
) {
}

public function transformClassGetMethodToAttributeMarker(
Class_ $class,
string $methodName,
string $attributeClass
string $attributeClass,
ObjectType $objectType
): bool {

// check if attribute even exists
Expand Down Expand Up @@ -64,6 +66,10 @@ public function transformClassGetMethodToAttributeMarker(
continue;
}

if ($arrayItem->value->isFirstClassCallable()) {
continue;
}

$new = $arrayItem->value;
if (count($new->getArgs()) !== 2) {
continue;
Expand All @@ -72,7 +78,7 @@ public function transformClassGetMethodToAttributeMarker(
$secondArg = $new->getArgs()[1];

if ($this->isLocalCallable($secondArg->value)) {
$localMethodName = $this->localArrayMethodCallableMatcher->match($secondArg->value);
$localMethodName = $this->localArrayMethodCallableMatcher->match($secondArg->value, $objectType);
if (! is_string($localMethodName)) {
continue;
}
Expand Down
28 changes: 14 additions & 14 deletions rules/Symfony73/NodeAnalyzer/LocalArrayMethodCallableMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\NodeTypeResolver;

final class LocalArrayMethodCallableMatcher
final readonly class LocalArrayMethodCallableMatcher
{
public function match(Expr $expr): ?string
public function __construct(
private NodeTypeResolver $nodeTypeResolver
) {
}

public function match(Expr $expr, ObjectType $objectType): ?string
{
if ($expr instanceof MethodCall) {
if (! $expr->name instanceof Identifier) {
return null;
}

return $expr->name->toString();
}

if ($expr instanceof Array_) {
$firstItem = $expr->items[0];
if (! $firstItem->value instanceof Variable) {
if (! $this->nodeTypeResolver->isObjectType($expr->var, $objectType)) {
return null;
}

$methodName = $firstItem->value->name;
if (! is_string($methodName)) {
return null;
}
return $expr->name->toString();
}

if ($methodName !== 'this') {
if ($expr instanceof Array_) {
if (! $this->nodeTypeResolver->isObjectType($expr->items[0]->value, $objectType)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function refactor(Node $node): ?Class_
$hasChanged = $this->getMethodToAsTwigAttributeTransformer->transformClassGetMethodToAttributeMarker(
$node,
'getFilters',
TwigClass::AS_TWIG_FILTER_ATTRIBUTE
TwigClass::AS_TWIG_FILTER_ATTRIBUTE,
$twigExtensionObjectType
);

if ($hasChanged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ public function refactor(Node $node): ?Class_
return null;
}

if (! $this->isObjectType($node, new ObjectType(TwigClass::TWIG_EXTENSION))) {
$twigExtensionObjectType = new ObjectType(TwigClass::TWIG_EXTENSION);
if (! $this->isObjectType($node, $twigExtensionObjectType)) {
return null;
}

$hasChanged = $this->getMethodToAsTwigAttributeTransformer->transformClassGetMethodToAttributeMarker(
$node,
'getFunctions',
TwigClass::AS_TWIG_FUNCTION_ATTRIBUTE
TwigClass::AS_TWIG_FUNCTION_ATTRIBUTE,
$twigExtensionObjectType
);

if (! $hasChanged) {
Expand Down
Loading