Skip to content

Commit c144f25

Browse files
authored
Twig attributes: make methods public (#805)
1 parent 39316cc commit c144f25

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

rules-tests/Symfony73/Rector/Class_/GetFiltersToAsTwigFilterAttributeRector/Fixture/with_first_class_callable_filter.php.inc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ final class WithFirstClassCallableFilter extends AbstractExtension
1010
{
1111
return [
1212
new \Twig\TwigFilter('some_filter', $this->someFilter(...)),
13+
new \Twig\TwigFilter('another_filter', $this->anotherFilter(...)),
14+
new \Twig\TwigFilter('third_filter', $this->thirdFilter(...)),
1315
];
1416
}
1517

1618
public function someFilter($value)
1719
{
1820
return $value;
1921
}
22+
23+
protected function anotherFilter($value)
24+
{
25+
return $value;
26+
}
27+
28+
private function thirdFilter($value)
29+
{
30+
return $value;
31+
}
2032
}
2133

2234
?>
@@ -34,6 +46,18 @@ final class WithFirstClassCallableFilter extends AbstractExtension
3446
{
3547
return $value;
3648
}
49+
50+
#[\Twig\Attribute\AsTwigFilter('another_filter')]
51+
public function anotherFilter($value)
52+
{
53+
return $value;
54+
}
55+
56+
#[\Twig\Attribute\AsTwigFilter('third_filter')]
57+
public function thirdFilter($value)
58+
{
59+
return $value;
60+
}
3761
}
3862

3963
?>

rules-tests/Symfony73/Rector/Class_/GetFunctionsToAsTwigFunctionAttributeRector/Fixture/some_get_functions.php.inc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ final class SomeGetFunctions extends AbstractExtension
1111
return [
1212
new \Twig\TwigFunction('some_function', [$this, 'someFunction']),
1313
new \Twig\TwigFunction('another_function', $this->anotherFunction(...)),
14+
new \Twig\TwigFunction('third_function', $this->thirdFunction(...)),
1415
];
1516
}
1617

@@ -19,7 +20,12 @@ final class SomeGetFunctions extends AbstractExtension
1920
return $value;
2021
}
2122

22-
public function anotherFunction($value)
23+
protected function anotherFunction($value)
24+
{
25+
return $value;
26+
}
27+
28+
private function thirdFunction($value)
2329
{
2430
return $value;
2531
}
@@ -46,6 +52,12 @@ final class SomeGetFunctions extends AbstractExtension
4652
{
4753
return $value;
4854
}
55+
56+
#[\Twig\Attribute\AsTwigFunction('third_function')]
57+
public function thirdFunction($value)
58+
{
59+
return $value;
60+
}
4961
}
5062

5163
?>

rules/Symfony73/GetMethodToAsTwigAttributeTransformer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpParser\Node\Stmt\Return_;
1919
use PHPStan\Reflection\ReflectionProvider;
2020
use PHPStan\Type\ObjectType;
21+
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
2122
use Rector\Symfony\Symfony73\NodeAnalyzer\LocalArrayMethodCallableMatcher;
2223
use Rector\Symfony\Symfony73\NodeRemover\ReturnEmptyArrayMethodRemover;
2324

@@ -29,7 +30,8 @@
2930
public function __construct(
3031
private LocalArrayMethodCallableMatcher $localArrayMethodCallableMatcher,
3132
private ReturnEmptyArrayMethodRemover $returnEmptyArrayMethodRemover,
32-
private ReflectionProvider $reflectionProvider
33+
private ReflectionProvider $reflectionProvider,
34+
private VisibilityManipulator $visibilityManipulator
3335
) {
3436
}
3537

@@ -96,6 +98,7 @@ public function transformClassGetMethodToAttributeMarker(
9698
}
9799

98100
$this->decorateMethodWithAttribute($localMethod, $attributeClass, $nameArg);
101+
$this->visibilityManipulator->makePublic($localMethod);
99102

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

0 commit comments

Comments
 (0)