Skip to content

Commit 49b947f

Browse files
committed
remove reference
1 parent 25b9bd9 commit 49b947f

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

rules/Symfony30/Rector/ClassMethod/FormTypeGetParentRector.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ public function refactor(Node $node): ?Node
120120
return null;
121121
}
122122

123-
$this->replaceStringWIthFormTypeClassConstIfFound($node->expr->value, $node, $hasChanged);
123+
$formClass = $this->formTypeStringToTypeProvider->matchClassForNameWithPrefix($node->expr->value);
124+
if ($formClass === null) {
125+
return null;
126+
}
127+
128+
$node->expr = $this->nodeFactory->createClassConstReference($formClass);
129+
$hasChanged = true;
124130

125131
return $node;
126132
});
@@ -136,7 +142,7 @@ public function refactor(Node $node): ?Node
136142
private function isClassAndMethodMatch(Class_ $class, ClassMethod $classMethod): bool
137143
{
138144
if ($this->isName($classMethod->name, 'getParent')) {
139-
return $this->isObjectType($class, new ObjectType('Symfony\Component\Form\AbstractType'));
145+
return $this->isObjectType($class, new ObjectType(SymfonyClass::ABSTRACT_TYPE));
140146
}
141147

142148
if ($this->isName($classMethod->name, 'getExtendedType')) {
@@ -145,18 +151,4 @@ private function isClassAndMethodMatch(Class_ $class, ClassMethod $classMethod):
145151

146152
return false;
147153
}
148-
149-
private function replaceStringWIthFormTypeClassConstIfFound(
150-
string $stringValue,
151-
Return_ $return,
152-
bool &$hasChanged
153-
): void {
154-
$formClass = $this->formTypeStringToTypeProvider->matchClassForNameWithPrefix($stringValue);
155-
if ($formClass === null) {
156-
return;
157-
}
158-
159-
$return->expr = $this->nodeFactory->createClassConstReference($formClass);
160-
$hasChanged = true;
161-
}
162154
}

rules/Symfony62/Rector/Class_/MessageSubscriberInterfaceToAttributeRector.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Rector\Symfony\Symfony62\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr;
98
use PhpParser\Node\Expr\Array_;
109
use PhpParser\Node\Expr\ClassConstFetch;
1110
use PhpParser\Node\Expr\Yield_;
@@ -131,14 +130,12 @@ public function refactor(Node $node): ?Node
131130
continue;
132131
}
133132

134-
$getHandledMessagesClassMethod = $classStmt;
135-
136-
$stmts = (array) $getHandledMessagesClassMethod->stmts;
133+
$stmts = (array) $classStmt->stmts;
137134
if ($stmts === []) {
138135
return null;
139136
}
140137

141-
$this->handleYields($node, $getHandledMessagesClassMethod);
138+
$this->handleYields($node, $classStmt);
142139

143140
$this->classManipulator->removeImplements($node, [MessengerHelper::MESSAGE_SUBSCRIBER_INTERFACE]);
144141

@@ -158,17 +155,16 @@ private function handleYields(Class_ $class, ClassMethod $getHandledMessagesClas
158155
continue;
159156
}
160157

161-
$method = MethodName::INVOKE;
162-
$arguments = [];
163-
164158
if ($stmt->expr->key instanceof ClassConstFetch) {
165159
$array = $stmt->expr->value;
166160
if (! $array instanceof Array_) {
167161
continue;
168162
}
169163

170-
$arguments = $this->parseArguments($array, $method);
171-
$this->addAttribute($class, $method, $arguments);
164+
$arguments = $this->parseArguments($array);
165+
$methodName = $this->resolveMethodName($array);
166+
167+
$this->addAttribute($class, $methodName, $arguments);
172168
continue;
173169
}
174170

@@ -182,24 +178,33 @@ private function handleYields(Class_ $class, ClassMethod $getHandledMessagesClas
182178

183179
$classParts = $value->class->getParts();
184180
$this->newInvokeMethodName = 'handle' . end($classParts);
185-
$this->addAttribute($class, $method, $arguments);
181+
182+
$this->addAttribute($class, MethodName::INVOKE, []);
183+
}
184+
}
185+
186+
private function resolveMethodName(Array_ $array): string
187+
{
188+
foreach ($array->items as $arrayItem) {
189+
$key = (string) $this->valueResolver->getValue($arrayItem->key);
190+
$value = $this->valueResolver->getValue($arrayItem->value);
191+
if ($key === 'method') {
192+
return $value;
193+
}
186194
}
195+
196+
return MethodName::INVOKE;
187197
}
188198

189199
/**
190200
* @return array<string, mixed>
191201
*/
192-
private function parseArguments(Array_ $array, string &$method): array
202+
private function parseArguments(Array_ $array): array
193203
{
194-
foreach ($array->items as $item) {
195-
if (! $item->value instanceof Expr) {
196-
continue;
197-
}
198-
199-
$key = (string) $this->valueResolver->getValue($item->key);
200-
$value = $this->valueResolver->getValue($item->value);
204+
foreach ($array->items as $arrayItem) {
205+
$key = (string) $this->valueResolver->getValue($arrayItem->key);
206+
$value = $this->valueResolver->getValue($arrayItem->value);
201207
if ($key === 'method') {
202-
$method = $value;
203208
continue;
204209
}
205210

rules/Twig134/Rector/Return_/SimpleFunctionAndFilterRector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ public function refactor(Node $node): ?Node
129129
}
130130

131131
$newObjectType = $this->nodeTypeResolver->getType($node->value);
132-
$this->processArrayItem($node, $newObjectType, $hasChanged);
132+
133+
$hasArrayItemChanged = $this->processArrayItem($node, $newObjectType);
134+
if ($hasArrayItemChanged) {
135+
$hasChanged = true;
136+
}
133137

134138
return $node;
135139
});
@@ -158,8 +162,10 @@ private function shouldSkip(ClassMethod $classMethod): bool
158162
return ! $this->isNames($classMethod, ['getFunctions', 'getFilters']);
159163
}
160164

161-
private function processArrayItem(ArrayItem $arrayItem, Type $newNodeType, bool &$hasChanged): void
165+
private function processArrayItem(ArrayItem $arrayItem, Type $newNodeType): bool
162166
{
167+
$hasChanged = false;
168+
163169
foreach (self::OLD_TO_NEW_CLASSES as $oldClass => $newClass) {
164170
$oldClassObjectType = new ObjectType($oldClass);
165171
if (! $oldClassObjectType->equals($newNodeType)) {
@@ -187,6 +193,8 @@ private function processArrayItem(ArrayItem $arrayItem, Type $newNodeType, bool
187193
$hasChanged = true;
188194
break;
189195
}
196+
197+
return $hasChanged;
190198
}
191199

192200
/**

src/Enum/SymfonyClass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,6 @@ final class SymfonyClass
101101
public const string CONTAINER = 'Symfony\Component\DependencyInjection\Container';
102102

103103
public const string ABSTRACT_TYPE_EXTENSION = 'Symfony\Component\Form\AbstractTypeExtension';
104+
105+
public const string ABSTRACT_TYPE = 'Symfony\Component\Form\AbstractType';
104106
}

0 commit comments

Comments
 (0)