Skip to content

Commit c796900

Browse files
committed
Updated Rector to commit 3be6fd7636ee24d82606f6166f94d8372fee800d
rectorphp/rector-src@3be6fd7 Clean up repetitive recursive call and flag on ArrayKeyFirstLastRector (#6889)
1 parent fd2182b commit c796900

11 files changed

Lines changed: 230 additions & 8 deletions

File tree

vendor/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,7 @@
23062306
'Rector\\Symfony\\Enum\\SymfonyAnnotation' => $vendorDir . '/rector/rector-symfony/src/Enum/SymfonyAnnotation.php',
23072307
'Rector\\Symfony\\Enum\\SymfonyAttribute' => $vendorDir . '/rector/rector-symfony/src/Enum/SymfonyAttribute.php',
23082308
'Rector\\Symfony\\Enum\\SymfonyClass' => $vendorDir . '/rector/rector-symfony/src/Enum/SymfonyClass.php',
2309+
'Rector\\Symfony\\Enum\\TwigClass' => $vendorDir . '/rector/rector-symfony/src/Enum/TwigClass.php',
23092310
'Rector\\Symfony\\Exception\\XmlContainerNotExistsException' => $vendorDir . '/rector/rector-symfony/src/Exception/XmlContainerNotExistsException.php',
23102311
'Rector\\Symfony\\FormHelper\\FormTypeStringToTypeProvider' => $vendorDir . '/rector/rector-symfony/src/FormHelper/FormTypeStringToTypeProvider.php',
23112312
'Rector\\Symfony\\Helper\\MessengerHelper' => $vendorDir . '/rector/rector-symfony/src/Helper/MessengerHelper.php',
@@ -2425,6 +2426,7 @@
24252426
'Rector\\Symfony\\Symfony73\\NodeAnalyzer\\CommandArgumentsAndOptionsResolver' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/NodeAnalyzer/CommandArgumentsAndOptionsResolver.php',
24262427
'Rector\\Symfony\\Symfony73\\NodeFactory\\CommandInvokeParamsFactory' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php',
24272428
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\CommandHelpToAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php',
2429+
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFiltersToAsTwigFilterAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFiltersToAsTwigFilterAttributeRector.php',
24282430
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\InvokableCommandInputAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php',
24292431
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandArgument' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandArgument.php',
24302432
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandOption' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandOption.php',

vendor/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,7 @@ class ComposerStaticInit505aae23228c95129299728105788b45
25252525
'Rector\\Symfony\\Enum\\SymfonyAnnotation' => __DIR__ . '/..' . '/rector/rector-symfony/src/Enum/SymfonyAnnotation.php',
25262526
'Rector\\Symfony\\Enum\\SymfonyAttribute' => __DIR__ . '/..' . '/rector/rector-symfony/src/Enum/SymfonyAttribute.php',
25272527
'Rector\\Symfony\\Enum\\SymfonyClass' => __DIR__ . '/..' . '/rector/rector-symfony/src/Enum/SymfonyClass.php',
2528+
'Rector\\Symfony\\Enum\\TwigClass' => __DIR__ . '/..' . '/rector/rector-symfony/src/Enum/TwigClass.php',
25282529
'Rector\\Symfony\\Exception\\XmlContainerNotExistsException' => __DIR__ . '/..' . '/rector/rector-symfony/src/Exception/XmlContainerNotExistsException.php',
25292530
'Rector\\Symfony\\FormHelper\\FormTypeStringToTypeProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/FormHelper/FormTypeStringToTypeProvider.php',
25302531
'Rector\\Symfony\\Helper\\MessengerHelper' => __DIR__ . '/..' . '/rector/rector-symfony/src/Helper/MessengerHelper.php',
@@ -2644,6 +2645,7 @@ class ComposerStaticInit505aae23228c95129299728105788b45
26442645
'Rector\\Symfony\\Symfony73\\NodeAnalyzer\\CommandArgumentsAndOptionsResolver' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/NodeAnalyzer/CommandArgumentsAndOptionsResolver.php',
26452646
'Rector\\Symfony\\Symfony73\\NodeFactory\\CommandInvokeParamsFactory' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php',
26462647
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\CommandHelpToAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/CommandHelpToAttributeRector.php',
2648+
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFiltersToAsTwigFilterAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFiltersToAsTwigFilterAttributeRector.php',
26472649
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\InvokableCommandInputAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php',
26482650
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandArgument' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandArgument.php',
26492651
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandOption' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandOption.php',

vendor/composer/installed.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,12 +1866,12 @@
18661866
"source": {
18671867
"type": "git",
18681868
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
1869-
"reference": "db1760fd7cc1f149dae5ba835b2e70b4027ed2e7"
1869+
"reference": "821bc12342a91ca4f767002320b62ca51a9441e0"
18701870
},
18711871
"dist": {
18721872
"type": "zip",
1873-
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/db1760fd7cc1f149dae5ba835b2e70b4027ed2e7",
1874-
"reference": "db1760fd7cc1f149dae5ba835b2e70b4027ed2e7",
1873+
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/821bc12342a91ca4f767002320b62ca51a9441e0",
1874+
"reference": "821bc12342a91ca4f767002320b62ca51a9441e0",
18751875
"shasum": ""
18761876
},
18771877
"require": {
@@ -1900,7 +1900,7 @@
19001900
"tomasvotruba\/unused-public": "^2.0",
19011901
"tracy\/tracy": "^2.10"
19021902
},
1903-
"time": "2025-05-07T15:54:01+00:00",
1903+
"time": "2025-05-09T06:50:14+00:00",
19041904
"default-branch": true,
19051905
"type": "rector-extension",
19061906
"extra": {

vendor/composer/installed.php

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

vendor/rector/extension-installer/src/GeneratedConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
final class GeneratedConfig
1111
{
12-
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 826f808'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 2b141c8'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 462d1a9'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main db1760f'));
12+
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 826f808'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 2b141c8'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 462d1a9'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 821bc12'));
1313
private function __construct()
1414
{
1515
}

vendor/rector/rector-symfony/config/sets/symfony/symfony7/symfony73.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
// @see https://github.com/symfony/symfony/blame/7.3/UPGRADE-7.3.md
88
return static function (RectorConfig $rectorConfig) : void {
99
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-console.php');
10+
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-twig-bundle.php');
1011
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace RectorPrefix202505;
5+
6+
use Rector\Config\RectorConfig;
7+
use Rector\Symfony\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterAttributeRector;
8+
return static function (RectorConfig $rectorConfig) : void {
9+
$rectorConfig->rules([GetFiltersToAsTwigFilterAttributeRector::class]);
10+
};

vendor/rector/rector-symfony/rules/CodeQuality/Rector/ClassMethod/TemplateAnnotationToThisRenderRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use Rector\Symfony\NodeFinder\EmptyReturnNodeFinder;
3838
use Rector\Symfony\TypeAnalyzer\ArrayUnionResponseTypeAnalyzer;
3939
use Rector\Symfony\TypeDeclaration\ReturnTypeDeclarationUpdater;
40-
use RectorPrefix202505\Symfony\Component\HttpFoundation\Response;
4140
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
4241
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
4342
/**
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Symfony\Symfony73\Rector\Class_;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Arg;
8+
use PhpParser\Node\Attribute;
9+
use PhpParser\Node\AttributeGroup;
10+
use PhpParser\Node\Expr\Array_;
11+
use PhpParser\Node\Expr\MethodCall;
12+
use PhpParser\Node\Expr\New_;
13+
use PhpParser\Node\Expr\Variable;
14+
use PhpParser\Node\Name\FullyQualified;
15+
use PhpParser\Node\Scalar\String_;
16+
use PhpParser\Node\Stmt\Class_;
17+
use PhpParser\Node\Stmt\ClassMethod;
18+
use PhpParser\Node\Stmt\Return_;
19+
use PHPStan\Reflection\ReflectionProvider;
20+
use PHPStan\Type\ObjectType;
21+
use Rector\Rector\AbstractRector;
22+
use Rector\Symfony\Enum\TwigClass;
23+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
24+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
25+
/**
26+
* @see https://symfony.com/blog/new-in-symfony-7-3-twig-extension-attributes
27+
*
28+
* @see \Rector\Symfony\Tests\Symfony73\Rector\Class_\GetFiltersToAsTwigFilterAttributeRector\GetFiltersToAsTwigFilterAttributeRectorTest
29+
*/
30+
final class GetFiltersToAsTwigFilterAttributeRector extends AbstractRector
31+
{
32+
/**
33+
* @readonly
34+
*/
35+
private ReflectionProvider $reflectionProvider;
36+
public function __construct(ReflectionProvider $reflectionProvider)
37+
{
38+
$this->reflectionProvider = $reflectionProvider;
39+
}
40+
public function getRuleDefinition() : RuleDefinition
41+
{
42+
return new RuleDefinition('Changes getFilters() in TwigExtension to #[TwigFilter] marker attribute above function', [new CodeSample(<<<'CODE_SAMPLE'
43+
use Twig\Extension\AbstractExtension;
44+
45+
class SomeClass extends AbstractExtension
46+
{
47+
public function getFilters()
48+
{
49+
return [
50+
new \Twig\TwigFilter('filter_name', [$this, 'localMethod']),
51+
];
52+
}
53+
54+
public function localMethod($value)
55+
{
56+
return $value;
57+
}
58+
}
59+
CODE_SAMPLE
60+
, <<<'CODE_SAMPLE'
61+
use Twig\Extension\AbstractExtension;
62+
use Twig\TwigFilter;
63+
64+
class SomeClass extends AbstractExtension
65+
{
66+
#[TwigFilter('filter_name')]
67+
public function localMethod($value)
68+
{
69+
return $value;
70+
}
71+
}
72+
CODE_SAMPLE
73+
)]);
74+
}
75+
public function getNodeTypes() : array
76+
{
77+
return [Class_::class];
78+
}
79+
/**
80+
* @param Class_ $node
81+
*/
82+
public function refactor(Node $node) : ?Class_
83+
{
84+
if (!$this->reflectionProvider->hasClass(TwigClass::TWIG_EXTENSION)) {
85+
return null;
86+
}
87+
if ($node->isAbstract() || $node->isAnonymous()) {
88+
return null;
89+
}
90+
$twigExtensionObjectType = new ObjectType(TwigClass::TWIG_EXTENSION);
91+
if (!$this->isObjectType($node, $twigExtensionObjectType)) {
92+
return null;
93+
}
94+
$getFilterMethod = $node->getMethod('getFilters');
95+
if (!$getFilterMethod instanceof ClassMethod) {
96+
return null;
97+
}
98+
$hasChanged = \false;
99+
foreach ((array) $getFilterMethod->stmts as $stmt) {
100+
// handle return array simple case
101+
if (!$stmt instanceof Return_) {
102+
continue;
103+
}
104+
if (!$stmt->expr instanceof Array_) {
105+
continue;
106+
}
107+
$returnArray = $stmt->expr;
108+
foreach ($returnArray->items as $key => $arrayItem) {
109+
if (!$arrayItem->value instanceof New_) {
110+
continue;
111+
}
112+
$new = $arrayItem->value;
113+
if (!$this->isObjectType($new->class, new ObjectType(TwigClass::TWIG_FILTER))) {
114+
continue;
115+
}
116+
if (\count($new->getArgs()) !== 2) {
117+
continue;
118+
}
119+
$secondArg = $new->getArgs()[1];
120+
if ($secondArg->value instanceof MethodCall && $secondArg->value->isFirstClassCallable() && $this->isObjectType($secondArg->value->var, $twigExtensionObjectType)) {
121+
if ($this->processSetAttribute($secondArg, $node, $returnArray, $key)) {
122+
$hasChanged = \true;
123+
}
124+
} elseif ($secondArg->value instanceof Array_ && \count($secondArg->value->items) === 2) {
125+
if ($this->processSetAttribute($secondArg, $node, $returnArray, $key)) {
126+
$hasChanged = \true;
127+
}
128+
}
129+
}
130+
if ($hasChanged) {
131+
$this->removeGetFilterMethodIfEmpty($returnArray, $node);
132+
}
133+
}
134+
if ($hasChanged) {
135+
return $node;
136+
}
137+
return null;
138+
}
139+
private function processSetAttribute(Arg $secondArg, Class_ $class, Array_ $returnArray, int $key) : bool
140+
{
141+
$localMethodName = $this->matchLocalMethodName($secondArg->value);
142+
if (!\is_string($localMethodName)) {
143+
return \false;
144+
}
145+
$localMethod = $class->getMethod($localMethodName);
146+
if (!$localMethod instanceof ClassMethod) {
147+
return \false;
148+
}
149+
$localMethod->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified(TwigClass::AS_TWIG_FILTER_ATTRIBUTE))]);
150+
// remove old new fuction instance
151+
unset($returnArray->items[$key]);
152+
return \true;
153+
}
154+
/**
155+
* @param \PhpParser\Node\Expr\Array_|\PhpParser\Node\Expr\MethodCall $callable
156+
*/
157+
private function matchLocalMethodName($callable) : ?string
158+
{
159+
if ($callable instanceof Array_) {
160+
$firstItem = $callable->items[0];
161+
if (!$firstItem->value instanceof Variable) {
162+
return null;
163+
}
164+
if (!$this->isName($firstItem->value, 'this')) {
165+
return null;
166+
}
167+
$secondItem = $callable->items[1];
168+
if (!$secondItem->value instanceof String_) {
169+
return null;
170+
}
171+
return $secondItem->value->value;
172+
}
173+
return $this->getName($callable->name);
174+
}
175+
private function removeGetFilterMethodIfEmpty(Array_ $getFilterReturnArray, Class_ $class) : void
176+
{
177+
if (\count($getFilterReturnArray->items) !== 0) {
178+
return;
179+
}
180+
// remove "getFilters()" method
181+
foreach ($class->stmts as $key => $classStmt) {
182+
if (!$classStmt instanceof ClassMethod) {
183+
continue;
184+
}
185+
if (!$this->isName($classStmt, 'getFilters')) {
186+
continue;
187+
}
188+
unset($class->stmts[$key]);
189+
}
190+
}
191+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Symfony\Enum;
5+
6+
final class TwigClass
7+
{
8+
/**
9+
* @var string
10+
*/
11+
public const TWIG_EXTENSION = 'Twig\\Extension\\AbstractExtension';
12+
/**
13+
* @var string
14+
*/
15+
public const AS_TWIG_FILTER_ATTRIBUTE = 'Twig\\Attribute\\AsTwigFilter';
16+
public const TWIG_FILTER = 'Twig\\TwigFilter';
17+
}

0 commit comments

Comments
 (0)