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 @@ -10,13 +10,25 @@ final class WithFirstClassCallableFilter extends AbstractExtension
{
return [
new \Twig\TwigFilter('some_filter', $this->someFilter(...)),
new \Twig\TwigFilter('another_filter', $this->anotherFilter(...)),
new \Twig\TwigFilter('third_filter', $this->thirdFilter(...)),
];
}

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

protected function anotherFilter($value)
{
return $value;
}

private function thirdFilter($value)
{
return $value;
}
}

?>
Expand All @@ -34,6 +46,18 @@ final class WithFirstClassCallableFilter extends AbstractExtension
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('another_filter')]
public function anotherFilter($value)
{
return $value;
}

#[\Twig\Attribute\AsTwigFilter('third_filter')]
public function thirdFilter($value)
{
return $value;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class SomeGetFunctions extends AbstractExtension
return [
new \Twig\TwigFunction('some_function', [$this, 'someFunction']),
new \Twig\TwigFunction('another_function', $this->anotherFunction(...)),
new \Twig\TwigFunction('third_function', $this->thirdFunction(...)),
];
}

Expand All @@ -19,7 +20,12 @@ final class SomeGetFunctions extends AbstractExtension
return $value;
}

public function anotherFunction($value)
protected function anotherFunction($value)
{
return $value;
}

private function thirdFunction($value)
{
return $value;
}
Expand All @@ -46,6 +52,12 @@ final class SomeGetFunctions extends AbstractExtension
{
return $value;
}

#[\Twig\Attribute\AsTwigFunction('third_function')]
public function thirdFunction($value)
{
return $value;
}
}

?>
5 changes: 4 additions & 1 deletion rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Symfony\Symfony73\NodeAnalyzer\LocalArrayMethodCallableMatcher;
use Rector\Symfony\Symfony73\NodeRemover\ReturnEmptyArrayMethodRemover;

Expand All @@ -29,7 +30,8 @@
public function __construct(
private LocalArrayMethodCallableMatcher $localArrayMethodCallableMatcher,
private ReturnEmptyArrayMethodRemover $returnEmptyArrayMethodRemover,
private ReflectionProvider $reflectionProvider
private ReflectionProvider $reflectionProvider,
private VisibilityManipulator $visibilityManipulator
) {
}

Expand Down Expand Up @@ -96,6 +98,7 @@ public function transformClassGetMethodToAttributeMarker(
}

$this->decorateMethodWithAttribute($localMethod, $attributeClass, $nameArg);
$this->visibilityManipulator->makePublic($localMethod);

// remove old new fuction instance
unset($returnArray->items[$key]);
Expand Down