Skip to content

Commit 2074e96

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

File tree

1 file changed

+73
-41
lines changed

1 file changed

+73
-41
lines changed

src/CodeGen/ColumnTypeExtractor.php

Lines changed: 73 additions & 41 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+
$isSchemaMethodCall = $methodCall->var instanceof MethodCall;
135+
$hasEnoughArguments = count($methodCall->args) >= 2;
136+
137+
if (!$isSetColumnTypeCall || !$isSchemaMethodCall || !$hasEnoughArguments) {
138+
return;
139+
}
140+
141+
$schemaCall = $methodCall->var;
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,49 @@ 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-
) {
194+
$isStaticCall = $node instanceof Node\Expr\StaticCall;
195+
if ($isStaticCall) {
196+
$hasNamedClass = $node->class instanceof Node\Name;
197+
$hasIdentifierMethod = $node->name instanceof Node\Identifier;
198+
199+
if (!$hasNamedClass || !$hasIdentifierMethod) {
200+
return null;
201+
}
202+
185203
$className = $node->class->toString();
186204
$methodName = $node->name->name;
205+
$isEnumTypeClass = $className === 'EnumType' || str_ends_with($className, '\\EnumType');
206+
$isFromMethod = $methodName === 'from';
207+
$hasArguments = $node->args !== [];
187208

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-
}
209+
if (!$isEnumTypeClass || !$isFromMethod || !$hasArguments) {
210+
return null;
201211
}
212+
213+
$argNode = $node->args[0];
214+
if (!$argNode instanceof Node\Arg) {
215+
return null;
216+
}
217+
218+
$arg = $argNode->value;
219+
$isClassConstFetch = $arg instanceof Node\Expr\ClassConstFetch;
220+
if (!$isClassConstFetch) {
221+
return null;
222+
}
223+
224+
$hasNamedEnumClass = $arg->class instanceof Node\Name;
225+
$isClassConstant = $arg->name instanceof Node\Identifier
226+
&& $arg->name->name === 'class';
227+
228+
if (!$hasNamedEnumClass || !$isClassConstant) {
229+
return null;
230+
}
231+
232+
$enumClass = $arg->class->toString();
233+
234+
return 'EnumType::from(' . $enumClass . '::class)';
202235
}
203236

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

0 commit comments

Comments
 (0)