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
Expand Up @@ -27,7 +27,7 @@ namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterA

use Twig\Extension\AbstractExtension;

final class SomeGetFilter extends AbstractExtension
final class SomeGetFilter
{
#[\Twig\Attribute\AsTwigFilter('some_filter')]
public function someFilter($value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterA

use Twig\Extension\AbstractExtension;

final class WithFirstClassCallableFilter extends AbstractExtension
final class WithFirstClassCallableFilter
{
#[\Twig\Attribute\AsTwigFilter('some_filter')]
public function someFilter($value)
Expand Down
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 SkipDifferentClass extends AbstractExtension
{
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('some_function', [DifferentClass::class, 'someFunction']),
new \Twig\TwigFunction('some_function', $this->someFunction(...)),
];
}

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

?>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFunctionsToAsTwigFunct

use Twig\Extension\AbstractExtension;

final class SomeGetFunctions extends AbstractExtension
final class SomeGetFunctions
{
#[\Twig\Attribute\AsTwigFunction('some_function')]
public function someFunction($value)
Expand Down
11 changes: 9 additions & 2 deletions rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Symfony\Enum\TwigClass;
use Rector\Symfony\Symfony73\NodeAnalyzer\LocalArrayMethodCallableMatcher;
use Rector\Symfony\Symfony73\NodeRemover\ReturnEmptyArrayMethodRemover;

Expand Down Expand Up @@ -52,8 +53,9 @@ public function transformClassGetMethodToAttributeMarker(
return false;
}

$hasChanged = false;
$originalMethod = clone $getMethod;

$hasChanged = false;
foreach ((array) $getMethod->stmts as $stmt) {
// handle return array simple case
if (! $stmt instanceof Return_) {
Expand Down Expand Up @@ -89,7 +91,8 @@ public function transformClassGetMethodToAttributeMarker(
if ($this->isLocalCallable($secondArg->value)) {
$localMethodName = $this->localArrayMethodCallableMatcher->match($secondArg->value, $objectType);
if (! is_string($localMethodName)) {
continue;
$getMethod = $originalMethod;
return false;
}

$localMethod = $class->getMethod($localMethodName);
Expand All @@ -110,6 +113,10 @@ public function transformClassGetMethodToAttributeMarker(
$this->returnEmptyArrayMethodRemover->removeClassMethodIfArrayEmpty($class, $returnArray, $methodName);
}

if ($hasChanged && $class->extends instanceof FullyQualified && $class->extends->toString() === TwigClass::TWIG_EXTENSION) {
$class->extends = null;
}

return $hasChanged;
}

Expand Down