Skip to content

Commit 0015fbf

Browse files
authored
[twig] add explicit attribute name arg, to improve context (#907)
* add explicit attribute name arg, to improve context * tidy up
1 parent 55e6ba9 commit 0015fbf

16 files changed

Lines changed: 52 additions & 50 deletions

File tree

rector.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
'*/Source/*',
2424
'*/Source*/*',
2525
'*/tests/*/Fixture*/Expected/*',
26-
StringClassNameToClassConstantRector::class => [__DIR__ . '/config', __DIR__ . '/src/Enum'],
26+
StringClassNameToClassConstantRector::class => [
27+
__DIR__ . '/config', __DIR__ . '/src/Enum',
28+
__DIR__ . '/rules/CodeQuality/Enum/',
29+
],
2730
UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config'],
2831

2932
RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class => [
@@ -34,19 +37,8 @@
3437
// marked as skipped
3538
ReturnNeverTypeRector::class => ['*/tests/*'],
3639
])
37-
->withConfiguredRule(StringClassNameToClassConstantRector::class, [
38-
'Error',
39-
'Exception',
40-
'Symfony\*',
41-
'Twig_*',
42-
'Twig*',
43-
'Swift_*',
44-
'Doctrine\*',
45-
// loaded from project itself
46-
'Psr\Container\ContainerInterface',
47-
'Symfony\Component\Routing\RouterInterface',
48-
'Symfony\Component\DependencyInjection\Container',
49-
])
40+
// @todo cleanup rest and move to enum classes ass single place for class names
41+
->withConfiguredRule(StringClassNameToClassConstantRector::class, ['Symfony\*', 'Twig_*', 'Twig*'])
5042
->withPhpSets()
5143
->withPreparedSets(
5244
deadCode: true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use Twig\Extension\AbstractExtension;
2929

3030
final class SomeGetFilter
3131
{
32-
#[\Twig\Attribute\AsTwigFilter('some_filter')]
32+
#[\Twig\Attribute\AsTwigFilter(name: 'some_filter')]
3333
public function someFilter($value)
3434
{
3535
return $value;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ use Twig\Extension\AbstractExtension;
4141

4242
final class WithFirstClassCallableFilter
4343
{
44-
#[\Twig\Attribute\AsTwigFilter('some_filter')]
44+
#[\Twig\Attribute\AsTwigFilter(name: 'some_filter')]
4545
public function someFilter($value)
4646
{
4747
return $value;
4848
}
4949

50-
#[\Twig\Attribute\AsTwigFilter('another_filter')]
50+
#[\Twig\Attribute\AsTwigFilter(name: 'another_filter')]
5151
public function anotherFilter($value)
5252
{
5353
return $value;
5454
}
5555

56-
#[\Twig\Attribute\AsTwigFilter('third_filter')]
56+
#[\Twig\Attribute\AsTwigFilter(name: 'third_filter')]
5757
public function thirdFilter($value)
5858
{
5959
return $value;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,49 +82,49 @@ use Twig\Extension\AbstractExtension;
8282

8383
final class WithOptionsParameter
8484
{
85-
#[\Twig\Attribute\AsTwigFilter('with_environment', needsEnvironment: true)]
85+
#[\Twig\Attribute\AsTwigFilter(name: 'with_environment', needsEnvironment: true)]
8686
public function withEnvironment(Environment $env, $value)
8787
{
8888
return $value;
8989
}
9090

91-
#[\Twig\Attribute\AsTwigFilter('with_context', needsContext: true)]
91+
#[\Twig\Attribute\AsTwigFilter(name: 'with_context', needsContext: true)]
9292
public function withContext(array $context, $value)
9393
{
9494
return $value;
9595
}
9696

97-
#[\Twig\Attribute\AsTwigFilter('with_charset', needsCharset: true)]
97+
#[\Twig\Attribute\AsTwigFilter(name: 'with_charset', needsCharset: true)]
9898
public function withCharset(string $charset, $value)
9999
{
100100
return $value;
101101
}
102102

103-
#[\Twig\Attribute\AsTwigFilter('with_pre_escape', preEscape: 'html')]
103+
#[\Twig\Attribute\AsTwigFilter(name: 'with_pre_escape', preEscape: 'html')]
104104
public function withPreEscape($value)
105105
{
106106
return $value;
107107
}
108108

109-
#[\Twig\Attribute\AsTwigFilter('with_preserves_safety', preservesSafety: ['html'])]
109+
#[\Twig\Attribute\AsTwigFilter(name: 'with_preserves_safety', preservesSafety: ['html'])]
110110
public function withPreservesSafety($value)
111111
{
112112
return $value;
113113
}
114114

115-
#[\Twig\Attribute\AsTwigFilter('with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
115+
#[\Twig\Attribute\AsTwigFilter(name: 'with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
116116
public function withSafeCallback($value)
117117
{
118118
return $value;
119119
}
120120

121-
#[\Twig\Attribute\AsTwigFilter('with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
121+
#[\Twig\Attribute\AsTwigFilter(name: 'with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
122122
public function withDeprecationInfo($value)
123123
{
124124
return $value;
125125
}
126126

127-
#[\Twig\Attribute\AsTwigFilter('with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true, preEscape: 'html', preservesSafety: ['html'])]
127+
#[\Twig\Attribute\AsTwigFilter(name: 'with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true, preEscape: 'html', preservesSafety: ['html'])]
128128
public function withEverything(string $charset, Environment $env, array $context, $value)
129129
{
130130
return $value;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ use Twig\Extension\AbstractExtension;
4141

4242
final class SomeGetFunctions
4343
{
44-
#[\Twig\Attribute\AsTwigFunction('some_function')]
44+
#[\Twig\Attribute\AsTwigFunction(name: 'some_function')]
4545
public function someFunction($value)
4646
{
4747
return $value;
4848
}
4949

50-
#[\Twig\Attribute\AsTwigFunction('another_function')]
50+
#[\Twig\Attribute\AsTwigFunction(name: 'another_function')]
5151
public function anotherFunction($value)
5252
{
5353
return $value;
5454
}
5555

56-
#[\Twig\Attribute\AsTwigFunction('third_function')]
56+
#[\Twig\Attribute\AsTwigFunction(name: 'third_function')]
5757
public function thirdFunction($value)
5858
{
5959
return $value;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,37 @@ use Twig\Extension\AbstractExtension;
7070

7171
final class WithOptionsParameter
7272
{
73-
#[\Twig\Attribute\AsTwigFunction('with_environment', needsEnvironment: true)]
73+
#[\Twig\Attribute\AsTwigFunction(name: 'with_environment', needsEnvironment: true)]
7474
public function withEnvironment(Environment $env, $value)
7575
{
7676
return $value;
7777
}
7878

79-
#[\Twig\Attribute\AsTwigFunction('with_context', needsContext: true)]
79+
#[\Twig\Attribute\AsTwigFunction(name: 'with_context', needsContext: true)]
8080
public function withContext(array $context, $value)
8181
{
8282
return $value;
8383
}
8484

85-
#[\Twig\Attribute\AsTwigFunction('with_charset', needsCharset: true)]
85+
#[\Twig\Attribute\AsTwigFunction(name: 'with_charset', needsCharset: true)]
8686
public function withCharset(string $charset, $value)
8787
{
8888
return $value;
8989
}
9090

91-
#[\Twig\Attribute\AsTwigFunction('with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
91+
#[\Twig\Attribute\AsTwigFunction(name: 'with_safe_callback', isSafeCallback: [self::class, 'checkSafeCallback'])]
9292
public function withSafeCallback($value)
9393
{
9494
return $value;
9595
}
9696

97-
#[\Twig\Attribute\AsTwigFunction('with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
97+
#[\Twig\Attribute\AsTwigFunction(name: 'with_deprecation_info', deprecationInfo: new DeprecatedCallableInfo('package', 'version'))]
9898
public function withDeprecationInfo($value)
9999
{
100100
return $value;
101101
}
102102

103-
#[\Twig\Attribute\AsTwigFunction('with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true)]
103+
#[\Twig\Attribute\AsTwigFunction(name: 'with_everything', isSafe: ['html'], needsContext: true, needsCharset: true, needsEnvironment: true)]
104104
public function withEverything(string $charset, Environment $env, array $context, $value)
105105
{
106106
return $value;

rules/CodeQuality/Rector/ClassMethod/RemoveUnusedRequestParamRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\Rector\AbstractRector;
1818
use Rector\Reflection\ReflectionResolver;
1919
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
20+
use Symplify\PHPStanRules\Enum\SymfonyClass;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2223

@@ -110,7 +111,7 @@ public function refactor(Node $node): ?Node
110111
continue;
111112
}
112113

113-
if (! $this->isObjectType($param->type, new ObjectType('Symfony\Component\HttpFoundation\Request'))) {
114+
if (! $this->isObjectType($param->type, new ObjectType(SymfonyClass::REQUEST))) {
114115
continue;
115116
}
116117

rules/CodeQuality/Rector/ClassMethod/ResponseReturnTypeControllerActionRector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Rector\Symfony\CodeQuality\Enum\ResponseClass;
2626
use Rector\Symfony\Enum\SensioAttribute;
2727
use Rector\Symfony\Enum\SymfonyAnnotation;
28+
use Rector\Symfony\Enum\SymfonyClass;
2829
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
2930
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
3031
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
@@ -125,7 +126,8 @@ public function refactor(Node $node): ?Node
125126

126127
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
127128
$types = $returnType instanceof UnionType ? $returnType->getTypes() : [$returnType];
128-
$objectType = new ObjectType('Symfony\Component\HttpFoundation\Response');
129+
130+
$objectType = new ObjectType(SymfonyClass::RESPONSE);
129131

130132
foreach ($types as $type) {
131133
if ($type instanceof ObjectType && $objectType->isSuperTypeOf($type)->yes()) {

rules/Symfony30/Rector/ClassMethod/GetRequestRector.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\PhpParser\Node\BetterNodeFinder;
1717
use Rector\Rector\AbstractRector;
1818
use Rector\Symfony\Bridge\NodeAnalyzer\ControllerMethodAnalyzer;
19+
use Rector\Symfony\Enum\SymfonyClass;
1920
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -25,11 +26,6 @@
2526
*/
2627
final class GetRequestRector extends AbstractRector
2728
{
28-
/**
29-
* @var string
30-
*/
31-
private const REQUEST_CLASS = 'Symfony\Component\HttpFoundation\Request';
32-
3329
private ?string $requestVariableAndParamName = null;
3430

3531
public function __construct(
@@ -230,7 +226,7 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
230226
return null;
231227
}
232228

233-
$fullyQualified = new FullyQualified(self::REQUEST_CLASS);
229+
$fullyQualified = new FullyQualified(SymfonyClass::REQUEST);
234230
$classMethod->params[] = new Param(new Variable(
235231
$this->getRequestVariableAndParamName()
236232
), null, $fullyQualified);

rules/Symfony51/Rector/ClassMethod/CommandConstantReturnCodeRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\PhpParser\Node\BetterNodeFinder;
1414
use Rector\Rector\AbstractRector;
1515
use Rector\Reflection\ReflectionResolver;
16+
use Rector\Symfony\Enum\SymfonyClass;
1617
use Rector\Symfony\ValueObject\ConstantMap\SymfonyCommandConstantMap;
1718
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1819
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -80,7 +81,7 @@ public function refactor(Node $node): ?Node
8081
return null;
8182
}
8283

83-
if (! $classReflection->is('Symfony\Component\Console\Command\Command')) {
84+
if (! $classReflection->is(SymfonyClass::COMMAND)) {
8485
return null;
8586
}
8687

@@ -121,7 +122,7 @@ private function convertNumberToConstant(Int_ $int): ?ClassConstFetch
121122
}
122123

123124
return $this->nodeFactory->createClassConstFetch(
124-
'Symfony\Component\Console\Command\Command',
125+
SymfonyClass::COMMAND,
125126
SymfonyCommandConstantMap::RETURN_TO_CONST[$int->value]
126127
);
127128
}

0 commit comments

Comments
 (0)