|
34 | 34 | */ |
35 | 35 | class ColumnTypeExtractor extends NodeVisitorAbstract |
36 | 36 | { |
37 | | - /** |
38 | | - * @var \PhpParser\Parser |
39 | | - */ |
40 | 37 | protected Parser $parser; |
41 | 38 |
|
42 | 39 | /** |
43 | 40 | * @var array<string, string> |
44 | 41 | */ |
45 | 42 | protected array $columnTypes = []; |
46 | 43 |
|
47 | | - /** |
48 | | - * @var bool |
49 | | - */ |
50 | 44 | protected bool $inInitialize = false; |
51 | 45 |
|
52 | 46 | /** |
@@ -80,7 +74,7 @@ public function extract(string $code): array |
80 | 74 | $traverser = new NodeTraverser(); |
81 | 75 | $traverser->addVisitor($this); |
82 | 76 | $traverser->traverse($ast); |
83 | | - } catch (Exception $e) { |
| 77 | + } catch (Exception) { |
84 | 78 | // If parsing fails, return empty array |
85 | 79 | return []; |
86 | 80 | } |
@@ -136,37 +130,25 @@ public function leaveNode(Node $node) |
136 | 130 | protected function processMethodCall(MethodCall $methodCall): void |
137 | 131 | { |
138 | 132 | // Check if this is a setColumnType call |
139 | | - if ($methodCall->name instanceof Node\Identifier && $methodCall->name->name === 'setColumnType') { |
140 | | - // Check if it's called on getSchema() |
141 | | - if ( |
142 | | - $methodCall->var instanceof MethodCall && |
143 | | - $methodCall->var->name instanceof Node\Identifier && |
144 | | - $methodCall->var->name->name === 'getSchema' && |
145 | | - $methodCall->var->var instanceof Variable && |
146 | | - $methodCall->var->var->name === 'this' |
147 | | - ) { |
148 | | - // Extract the column name and type expression |
149 | | - if (count($methodCall->args) >= 2) { |
150 | | - $columnArgNode = $methodCall->args[0]; |
151 | | - $typeArgNode = $methodCall->args[1]; |
152 | | - if (!$columnArgNode instanceof Node\Arg || !$typeArgNode instanceof Node\Arg) { |
153 | | - return; |
154 | | - } |
155 | | - $columnArg = $columnArgNode->value; |
156 | | - $typeArg = $typeArgNode->value; |
157 | | - |
158 | | - // Get column name |
159 | | - $columnName = $this->getStringValue($columnArg); |
160 | | - if ($columnName === null) { |
161 | | - return; |
162 | | - } |
163 | | - |
164 | | - // Get the type expression as a string |
165 | | - $typeExpression = $this->getTypeExpression($typeArg); |
166 | | - if ($typeExpression !== null) { |
167 | | - $this->columnTypes[$columnName] = $typeExpression; |
168 | | - } |
169 | | - } |
| 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; |
170 | 152 | } |
171 | 153 | } |
172 | 154 | } |
@@ -204,25 +186,17 @@ protected function getTypeExpression(Node $node): ?string |
204 | 186 | $methodName = $node->name->name; |
205 | 187 |
|
206 | 188 | // Handle EnumType::from() calls |
207 | | - if ($className === 'EnumType' || str_ends_with($className, '\\EnumType')) { |
208 | | - if ($methodName === 'from' && count($node->args) > 0) { |
209 | | - // Extract the enum class name |
210 | | - $argNode = $node->args[0]; |
211 | | - if (!$argNode instanceof Node\Arg) { |
212 | | - return null; |
213 | | - } |
214 | | - $arg = $argNode->value; |
215 | | - if ($arg instanceof Node\Expr\ClassConstFetch) { |
216 | | - if ( |
217 | | - $arg->class instanceof Node\Name && |
218 | | - $arg->name instanceof Node\Identifier && |
219 | | - $arg->name->name === 'class' |
220 | | - ) { |
221 | | - $enumClass = $arg->class->toString(); |
222 | | - // Return the full EnumType::from() expression |
223 | | - return 'EnumType::from(' . $enumClass . '::class)'; |
224 | | - } |
225 | | - } |
| 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)'; |
226 | 200 | } |
227 | 201 | } |
228 | 202 | } |
|
0 commit comments