Skip to content

Commit d0b41b9

Browse files
committed
refactor ColumnTypeExtractor to use early return pattern which is common for AST based logic
1 parent 2741d61 commit d0b41b9

1 file changed

Lines changed: 77 additions & 44 deletions

File tree

src/CodeGen/ColumnTypeExtractor.php

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,42 @@ public function leaveNode(Node $node)
129129
*/
130130
protected function processMethodCall(MethodCall $methodCall): void
131131
{
132-
// Check if this is a setColumnType call
133-
// Check if it's called on getSchema()
134-
// Extract the column name and type expression
135-
if ($methodCall->name instanceof Node\Identifier && $methodCall->name->name === 'setColumnType' && ($methodCall->var instanceof MethodCall && $methodCall->var->name instanceof Node\Identifier && $methodCall->var->name->name === 'getSchema' && $methodCall->var->var instanceof Variable && $methodCall->var->var->name === 'this') && count($methodCall->args) >= 2) {
136-
$columnArgNode = $methodCall->args[0];
137-
$typeArgNode = $methodCall->args[1];
138-
if (!$columnArgNode instanceof Node\Arg || !$typeArgNode instanceof Node\Arg) {
139-
return;
140-
}
141-
$columnArg = $columnArgNode->value;
142-
$typeArg = $typeArgNode->value;
143-
// Get column name
144-
$columnName = $this->getStringValue($columnArg);
145-
if ($columnName === null) {
146-
return;
147-
}
148-
// Get the type expression as a string
149-
$typeExpression = $this->getTypeExpression($typeArg);
150-
if ($typeExpression !== null) {
151-
$this->columnTypes[$columnName] = $typeExpression;
152-
}
132+
$isSetColumnTypeCall = $methodCall->name instanceof Node\Identifier
133+
&& $methodCall->name->name === 'setColumnType';
134+
$schemaCall = $methodCall->var;
135+
$isSchemaMethodCall = $schemaCall instanceof MethodCall;
136+
$hasEnoughArguments = count($methodCall->args) >= 2;
137+
138+
if (!$isSetColumnTypeCall || !$isSchemaMethodCall || !$hasEnoughArguments) {
139+
return;
140+
}
141+
142+
$isGetSchemaCall = $schemaCall->name instanceof Node\Identifier
143+
&& $schemaCall->name->name === 'getSchema';
144+
$isCalledOnThis = $schemaCall->var instanceof Variable
145+
&& $schemaCall->var->name === 'this';
146+
147+
if (!$isGetSchemaCall || !$isCalledOnThis) {
148+
return;
149+
}
150+
151+
$columnArgNode = $methodCall->args[0];
152+
$typeArgNode = $methodCall->args[1];
153+
if (!$columnArgNode instanceof Node\Arg || !$typeArgNode instanceof Node\Arg) {
154+
return;
155+
}
156+
157+
$columnArg = $columnArgNode->value;
158+
$typeArg = $typeArgNode->value;
159+
160+
$columnName = $this->getStringValue($columnArg);
161+
if ($columnName === null) {
162+
return;
163+
}
164+
165+
$typeExpression = $this->getTypeExpression($typeArg);
166+
if ($typeExpression !== null) {
167+
$this->columnTypes[$columnName] = $typeExpression;
153168
}
154169
}
155170

@@ -176,32 +191,50 @@ protected function getStringValue(Node $node): ?string
176191
*/
177192
protected function getTypeExpression(Node $node): ?string
178193
{
179-
// Handle EnumType::from() calls
180-
if (
181-
$node instanceof Node\Expr\StaticCall &&
182-
$node->class instanceof Node\Name &&
183-
$node->name instanceof Node\Identifier
184-
) {
185-
$className = $node->class->toString();
186-
$methodName = $node->name->name;
187-
188-
// Handle EnumType::from() calls
189-
if (($className === 'EnumType' || str_ends_with($className, '\\EnumType')) && ($methodName === 'from' && $node->args !== [])) {
190-
// Extract the enum class name
191-
$argNode = $node->args[0];
192-
if (!$argNode instanceof Node\Arg) {
193-
return null;
194-
}
195-
$arg = $argNode->value;
196-
if ($arg instanceof Node\Expr\ClassConstFetch && ($arg->class instanceof Node\Name && $arg->name instanceof Node\Identifier && $arg->name->name === 'class')) {
197-
$enumClass = $arg->class->toString();
198-
// Return the full EnumType::from() expression
199-
return 'EnumType::from(' . $enumClass . '::class)';
200-
}
194+
if ($node instanceof Node\Expr\StaticCall) {
195+
$staticCall = $node;
196+
$calledClass = $staticCall->class;
197+
$calledMethod = $staticCall->name;
198+
199+
$hasNamedClass = $calledClass instanceof Node\Name;
200+
$hasIdentifierMethod = $calledMethod instanceof Node\Identifier;
201+
if (!$hasNamedClass || !$hasIdentifierMethod) {
202+
return null;
203+
}
204+
205+
$className = $calledClass->toString();
206+
$methodName = $calledMethod->name;
207+
$isEnumTypeClass = $className === 'EnumType' || str_ends_with($className, '\\EnumType');
208+
$isFromMethod = $methodName === 'from';
209+
$hasArguments = $staticCall->args !== [];
210+
if (!$isEnumTypeClass || !$isFromMethod || !$hasArguments) {
211+
return null;
201212
}
213+
214+
$argNode = $staticCall->args[0];
215+
if (!$argNode instanceof Node\Arg) {
216+
return null;
217+
}
218+
219+
$arg = $argNode->value;
220+
if (!$arg instanceof Node\Expr\ClassConstFetch) {
221+
return null;
222+
}
223+
224+
$enumClassNode = $arg->class;
225+
$constantName = $arg->name;
226+
$hasNamedEnumClass = $enumClassNode instanceof Node\Name;
227+
$isClassConstant = $constantName instanceof Node\Identifier
228+
&& $constantName->name === 'class';
229+
if (!$hasNamedEnumClass || !$isClassConstant) {
230+
return null;
231+
}
232+
233+
$enumClass = $enumClassNode->toString();
234+
235+
return 'EnumType::from(' . $enumClass . '::class)';
202236
}
203237

204-
// Handle simple string types
205238
if ($node instanceof Node\Scalar\String_) {
206239
return '"' . $node->value . '"';
207240
}

0 commit comments

Comments
 (0)