| Conditions | 20 |
| Paths | 756 |
| Total Lines | 176 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 229 | public function generateClass(ClassDefinition $classDef): PhpFile |
||
| 230 | { |
||
| 231 | $phpFile = new PhpFile(); |
||
| 232 | $phpFile->addComment('This phpFile is auto-generated.'); |
||
| 233 | $phpFile->setStrictTypes(); // adds declare(strict_types=1) |
||
| 234 | |||
| 235 | $phpNamespace = $phpFile->addNamespace($this->baseNamespace); |
||
| 236 | |||
| 237 | $class = $phpNamespace->addClass($classDef->className); |
||
| 238 | |||
| 239 | $parentClass = $classDef->parentClass; |
||
| 240 | if ('Object' === $parentClass) { |
||
| 241 | $parentClass = static::OBJECT_CLASS; |
||
| 242 | } elseif ('Function' === $parentClass) { |
||
| 243 | $parentClass = static::FUNCTION_CLASS; |
||
| 244 | } |
||
| 245 | |||
| 246 | $class->addExtend($parentClass) |
||
| 247 | ->addComment($classDef->classDocs); |
||
| 248 | |||
| 249 | $class->addConstant('TYPE_NAME', $classDef->typeName) |
||
| 250 | ->setPublic(); |
||
| 251 | |||
| 252 | $constructor = $class->addMethod('__construct') |
||
| 253 | ->setPublic(); |
||
| 254 | |||
| 255 | if (!in_array($parentClass, [static::OBJECT_CLASS, static::FUNCTION_CLASS])) { |
||
| 256 | $constructor->addBody('parent::__construct();') |
||
| 257 | ->addBody(''); |
||
| 258 | } |
||
| 259 | |||
| 260 | $fromArray = $class->addMethod('fromArray') |
||
| 261 | ->setPublic() |
||
| 262 | ->setStatic() |
||
| 263 | ->setReturnType($classDef->className); |
||
| 264 | |||
| 265 | $serialize = $class->addMethod('typeSerialize') |
||
| 266 | ->setReturnType('array'); |
||
| 267 | |||
| 268 | $fromArray->addParameter('array') |
||
| 269 | ->setType('array'); |
||
| 270 | |||
| 271 | if (count($classDef->fields) > 0) { |
||
| 272 | $fromArray->addBody('return new static('); |
||
| 273 | |||
| 274 | $serialize->addBody('return ['); |
||
| 275 | $serialize->addBody(' \'@type\' => static::TYPE_NAME,'); |
||
| 276 | } else { |
||
| 277 | $fromArray->addBody('return new static();'); |
||
| 278 | $serialize->addBody('return [\'@type\' => static::TYPE_NAME];'); |
||
| 279 | } |
||
| 280 | |||
| 281 | foreach ($classDef->fields as $fieldDef) { |
||
| 282 | $typeStyle = $fieldDef->type; |
||
| 283 | $type = $fieldDef->type; |
||
| 284 | |||
| 285 | $arrayNestLevels = substr_count($type, '[]'); |
||
| 286 | if (1 === $arrayNestLevels) { |
||
| 287 | $type = 'array'; |
||
| 288 | $typeStyle = 'array'; |
||
| 289 | } elseif (2 === $arrayNestLevels) { |
||
| 290 | $type = 'array'; |
||
| 291 | $typeStyle = 'array_array'; |
||
| 292 | } elseif ($arrayNestLevels > 2) { |
||
| 293 | throw new InvalidArgumentException('Vector of higher than 2 lvl deep'); |
||
| 294 | } |
||
| 295 | |||
| 296 | $class->addProperty($fieldDef->name) |
||
| 297 | ->setProtected() |
||
| 298 | ->setNullable($fieldDef->mayBeNull) |
||
| 299 | ->setType($type) |
||
| 300 | ->addComment($fieldDef->doc) |
||
| 301 | ->addComment('') |
||
| 302 | ->addComment('@var ' . $fieldDef->type . ($fieldDef->mayBeNull ? '|null' : '')); |
||
| 303 | |||
| 304 | $constructor->addParameter($fieldDef->name) |
||
| 305 | ->setType($type) |
||
| 306 | ->setNullable($fieldDef->mayBeNull); |
||
| 307 | |||
| 308 | $constructor->addBody('$this->' . $fieldDef->name . ' = $' . $fieldDef->name . ';'); |
||
| 309 | |||
| 310 | [$rawType] = explode('[', $fieldDef->type); |
||
| 311 | |||
| 312 | switch ($rawType) { |
||
| 313 | case 'string': |
||
| 314 | case 'int': |
||
| 315 | case 'bool': |
||
| 316 | case 'float': |
||
| 317 | $fromArray->addBody(' $array[\'' . $fieldDef->rawName . '\'],'); |
||
| 318 | $serialize->addBody(' \'' . $fieldDef->rawName . '\' => $this->' . $fieldDef->name . ','); |
||
| 319 | break; |
||
| 320 | |||
| 321 | default: |
||
| 322 | if ($fieldDef->mayBeNull) { |
||
| 323 | if ('array' === $typeStyle) { |
||
| 324 | $fromArray->addBody( |
||
| 325 | ' (isset($array[\'' . $fieldDef->name . |
||
| 326 | '\']) ? array_map(fn($x) => ' . 'TdSchemaRegistry::fromArray($x), $array[\'' . |
||
| 327 | $fieldDef->name . '\']) : null),' |
||
| 328 | ); |
||
| 329 | |||
| 330 | $serialize->addBody( |
||
| 331 | ' (isset($this->' . $fieldDef->name . |
||
| 332 | ') ? array_map(fn($x) => $x->typeSerialize(), $this->' . $fieldDef->name . ') : null),' |
||
| 333 | ); |
||
| 334 | } elseif ('array_array' === $typeStyle) { |
||
| 335 | $fromArray->addBody( |
||
| 336 | ' (isset($array[\'' . $fieldDef->name . |
||
| 337 | '\']) ? array_map(fn($x) => ' . |
||
| 338 | 'array_map(fn($y) => TdSchemaRegistry::fromArray($y), $x), $array[\'' . |
||
| 339 | $fieldDef->name . '\']) : null),' |
||
| 340 | ); |
||
| 341 | |||
| 342 | $serialize->addBody( |
||
| 343 | ' (isset($this->' . $fieldDef->name . |
||
| 344 | ') ? array_map(fn($x) => array_map(fn($y) => $y->typeSerialize(), $x), $this->' . |
||
| 345 | $fieldDef->name . ') : null),' |
||
| 346 | ); |
||
| 347 | } else { |
||
| 348 | $fromArray->addBody( |
||
| 349 | ' (isset($array[\'' . $fieldDef->rawName . '\']) ? ' . |
||
| 350 | 'TdSchemaRegistry::fromArray($array[\'' . $fieldDef->rawName . '\']) : null),' |
||
| 351 | ); |
||
| 352 | |||
| 353 | $serialize->addBody( |
||
| 354 | ' \'' . $fieldDef->rawName . '\' => (isset($this->' . |
||
| 355 | $fieldDef->name . ') ? $this->' . $fieldDef->name . ' : null),' |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | } else { |
||
| 359 | if ('array' === $typeStyle) { |
||
| 360 | $fromArray->addBody( |
||
| 361 | ' array_map(fn($x) => TdSchemaRegistry::fromArray($x), $array[\'' . |
||
| 362 | $fieldDef->name . '\']),' |
||
| 363 | ); |
||
| 364 | |||
| 365 | $serialize->addBody( |
||
| 366 | ' array_map(fn($x) => $x->typeSerialize(), $this->' . $fieldDef->name . '),' |
||
| 367 | ); |
||
| 368 | } elseif ('array_array' === $typeStyle) { |
||
| 369 | $fromArray->addBody( |
||
| 370 | ' array_map(fn($x) => array_map(fn($y) => TdSchemaRegistry::fromArray($y), $x)' . |
||
| 371 | ', $array[\'' . $fieldDef->name . '\']),' |
||
| 372 | ); |
||
| 373 | |||
| 374 | $serialize->addBody( |
||
| 375 | ' array_map(fn($x) => array_map(fn($y) => $y->typeSerialize(), $x), $this->' . |
||
| 376 | $fieldDef->name . '),' |
||
| 377 | ); |
||
| 378 | } else { |
||
| 379 | $fromArray->addBody( |
||
| 380 | ' ' . 'TdSchemaRegistry::fromArray($array[\'' . $fieldDef->rawName . '\']),' |
||
| 381 | ); |
||
| 382 | |||
| 383 | $serialize->addBody( |
||
| 384 | ' \'' . $fieldDef->rawName . '\' => $this->' . $fieldDef->name . '->typeSerialize(),' |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | $getter = $class->addMethod('get' . ucfirst($fieldDef->name)) |
||
| 391 | ->setPublic() |
||
| 392 | ->setReturnType($type) |
||
| 393 | ->setReturnNullable($fieldDef->mayBeNull); |
||
| 394 | |||
| 395 | $getter->addBody('return $this->' . $fieldDef->name . ';'); |
||
| 396 | } |
||
| 397 | |||
| 398 | if (count($classDef->fields) > 0) { |
||
| 399 | $fromArray->addBody(');'); |
||
| 400 | |||
| 401 | $serialize->addBody('];'); |
||
| 402 | } |
||
| 403 | |||
| 404 | return $phpFile; |
||
| 405 | } |
||
| 407 |