Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ClassGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ClassGenerator extends AbstractFileGenerator |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * [$currentClass description]. |
||
| 25 | * |
||
| 26 | * @var \Nette\PhpGenerator\ClassType |
||
| 27 | */ |
||
| 28 | private $currentClass; |
||
| 29 | |||
| 30 | public function __construct($namespace, $className, $document = 'Generated Class') |
||
| 31 | { |
||
| 32 | $this->currentFile = new PhpFile(); |
||
| 33 | $this->currentClass = $this->currentFile->addClass($namespace.'\\'.ucfirst($className)); |
||
| 34 | $this->currentClass->addComment($document); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Aggiungere il costruttore. |
||
| 39 | * |
||
| 40 | * @return Nette\PhpGenerator\Method |
||
| 41 | */ |
||
| 42 | private function addConstructor() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $comment |
||
| 56 | * @param boolean $createGetter |
||
| 57 | */ |
||
| 58 | private function addSingleton($comment, $createGetter) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param boolean $isConcrete |
||
| 85 | */ |
||
| 86 | private function addGetter($fieldName, $fieldClassFull, $isStatic, $isConcrete) |
||
| 109 | |||
| 110 | private function addTrait($trait, $typesReference) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param boolean $isConcrete |
||
| 127 | */ |
||
| 128 | private function addSetter($fieldName, $fieldClassFull, $isStatic, $isConcrete) |
||
| 154 | |||
| 155 | private function addParseString() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * [generateClassType description]. |
||
| 181 | * |
||
| 182 | * @param string $properties elementi possibili 'fields', 'extend', 'implements' |
||
| 183 | * @param array $typesReference [description] |
||
| 184 | * @param array $typesDescription [description] |
||
| 185 | * @param ClassConfig $config [description] |
||
| 186 | */ |
||
| 187 | public function generateClassType(array $properties, $typesReference, $typesDescription, ClassConfig $config) |
||
| 188 | { |
||
| 189 | $phpNamespace = $this->currentClass->getNamespace(); |
||
| 190 | if ($config->isInterface) { |
||
| 191 | $this->info('Passo a interfaccia', [$this->currentClass->getName()]); |
||
| 192 | $docs = $this->currentClass->getComment(); |
||
| 193 | $this->currentClass = $this->currentFile->addInterface($phpNamespace->getName().'\\'.ucfirst($this->currentClass->getName())); |
||
| 194 | $this->currentClass->setComment($docs); |
||
| 195 | $this->info('Check haveConstructor, in caso metto a false', [$config->haveConstructor]); |
||
| 196 | $config->haveConstructor = false; |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->info('Generate', ['class' => $this->currentClass->getName(), 'namespace' => $phpNamespace->getName(), 'comment' => $this->currentClass->getComment(), 'properties' => $properties]); |
||
| 200 | |||
| 201 | // extend class |
||
| 202 | View Code Duplication | if (array_key_exists('extend', $properties)) { |
|
| 203 | $extendClassName = $properties['extend']; |
||
| 204 | $this->info('Aggiungo extend', [ |
||
| 205 | 'class' => $this->currentClass->getName(), |
||
| 206 | 'extend' => $extendClassName, |
||
| 207 | ]); |
||
| 208 | $this->currentClass->setExtends($extendClassName); |
||
| 209 | $this->currentClass->getNamespace()->addUse($extendClassName); |
||
| 210 | } |
||
| 211 | |||
| 212 | // implements class |
||
| 213 | if (array_key_exists('implements', $properties)) { |
||
| 214 | $implementsList = []; |
||
| 215 | if (!is_array($properties['implements'])) { |
||
| 216 | $implementsList[] = $properties['implements']; |
||
| 217 | } else { |
||
| 218 | $implementsList = array_merge($implementsList, $properties['implements']); |
||
| 219 | } |
||
| 220 | $this->currentClass->setImplements($implementsList); |
||
| 221 | foreach ($implementsList as $implementUse) { |
||
| 222 | $this->info('Aggiungo implement', [ |
||
| 223 | 'class' => $this->currentClass->getName(), |
||
| 224 | 'implements' => $implementUse, |
||
| 225 | ]); |
||
| 226 | $this->currentClass->getNamespace()->addUse($implementUse); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | // traits |
||
| 231 | if (array_key_exists('traits', $properties)) { |
||
| 232 | if (is_array($properties['traits'])) { |
||
| 233 | foreach ($properties['traits'] as $trait) { |
||
| 234 | $this->addTrait($trait, $typesReference); |
||
| 235 | } |
||
| 236 | } else { |
||
| 237 | $traitObject = $properties['traits']; |
||
| 238 | $this->addTrait($traitObject, $typesReference); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($config->isFinalClass) { |
||
| 243 | $this->currentClass->setFinal(true); |
||
| 244 | } |
||
| 245 | |||
| 246 | $first = true; |
||
| 247 | if (array_key_exists('fields', $properties)) { |
||
| 248 | /** @var $methodConstructor \Nette\PhpGenerator\Method */ |
||
| 249 | $methodConstructor = null; |
||
| 250 | if ($config->haveConstructor) { |
||
| 251 | $methodConstructor = $this->addConstructor(); |
||
| 252 | } |
||
| 253 | |||
| 254 | $body = ''; |
||
| 255 | |||
| 256 | foreach ($properties['fields'] as $name => $fieldProperties) { |
||
| 257 | $isStatic = false; |
||
| 258 | $isAutoinizialize = false; |
||
| 259 | $defaultValue = null; |
||
| 260 | if (array_key_exists('static', $fieldProperties)) { |
||
| 261 | $isStatic = $fieldProperties['static']; |
||
| 262 | } |
||
| 263 | |||
| 264 | if (array_key_exists('autoinizialize', $fieldProperties)) { |
||
| 265 | $isAutoinizialize = boolval($fieldProperties['autoinizialize']); |
||
| 266 | } |
||
| 267 | |||
| 268 | if (array_key_exists('default', $fieldProperties)) { |
||
| 269 | $defaultValue = $fieldProperties['default']; |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!$isAutoinizialize) { |
||
| 273 | if (null != $defaultValue) { |
||
| 274 | //TODO: usare "primitive type per determinare il corretto IF" |
||
| 275 | //FARE UN TEST PER I BOOLEAN |
||
| 276 | //@see https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ |
||
| 277 | $body .= 'if ( empty($'.$name.') ) { '."\n"; |
||
| 278 | if ($isStatic) { |
||
| 279 | $body .= ' self::$'; |
||
| 280 | } else { |
||
| 281 | $body .= ' $this->'; |
||
| 282 | } |
||
| 283 | $body .= $name.' = '.$defaultValue.';'."\n"; |
||
| 284 | $body .= '} else {'; |
||
| 285 | if ($isStatic) { |
||
| 286 | $body .= ' self::$'; |
||
| 287 | } else { |
||
| 288 | $body .= ' $this->'; |
||
| 289 | } |
||
| 290 | $body .= $name.' = $'.$name.';'."\n"; |
||
| 291 | $body .= '}'."\n"; |
||
| 292 | } else { |
||
| 293 | if (!$isStatic) { |
||
| 294 | $body .= ' $this->'.$name.' = $'.$name.';'."\n"; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } else { |
||
| 298 | if (!empty($defaultValue) || is_int($defaultValue)) { |
||
| 299 | if (substr(rtrim($defaultValue), -1) == ';') { |
||
| 300 | $this->error('autoinizialize for '.$name.' on class '.$this->currentClass->getName().' have default with ";" please remove!'); |
||
| 301 | $defaultValue = substr($defaultValue, 0, strlen($defaultValue) - 1); |
||
| 302 | } |
||
| 303 | if (!$isStatic) { |
||
| 304 | if ($isAutoinizialize) { |
||
| 305 | $body .= '// autoinizialize'."\n"; |
||
| 306 | $body .= '$this->'.$name.' = '.$defaultValue.';'."\n"; |
||
| 307 | } else { |
||
| 308 | if ($defaultValue) { |
||
| 309 | $body .= 'if ( !is_null($'.$name.') ) {'."\n"; |
||
| 310 | $body .= ' $this->'.$name.' = $'.$name.';'."\n"; |
||
| 311 | $body .= '} else {'."\n"; |
||
| 312 | $body .= ' $this->'.$name.' = '.$defaultValue.';'."\n"; |
||
| 313 | $body .= '}'."\n"; |
||
| 314 | // $body .= '$this->'.$name.' = '.$defaultValue.';'."\n"; |
||
| 315 | } else { |
||
| 316 | $body .= 'if ( is_null($'.$name.') ) {'."\n"; |
||
| 317 | $body .= ' $this->'.$name.' = '.$defaultValue.';'."\n"; |
||
| 318 | $body .= '}'."\n"; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } else { |
||
| 323 | $this->error('autoinizialize for '.$name.' not defined on element '.$this->currentClass->getName()); |
||
| 324 | $this->errors[] = 'autoinizialize for '.$name.' not defined on element '.$this->currentClass->getName(); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $fieldClassFull = ''; |
||
| 329 | if (array_key_exists('class', $fieldProperties)) { |
||
| 330 | $fieldClassName = ucfirst($fieldProperties['class']); |
||
| 331 | |||
| 332 | if (array_key_exists($fieldClassName, $typesReference)) { |
||
| 333 | $fieldNamespace = $typesReference[$fieldClassName]; |
||
| 334 | $fieldClassFull = $fieldNamespace.'\\'.$fieldClassName; |
||
| 335 | $this->info('Trovato field namespace tra le reference', [ |
||
| 336 | 'class' => $this->currentClass->getName(), |
||
| 337 | 'field' => $fieldClassName, |
||
| 338 | 'className' => $fieldClassFull, |
||
| 339 | ]); |
||
| 340 | } else { |
||
| 341 | //FIXME: strpos is better |
||
| 342 | if ($fieldClassName[0] == '\\') { |
||
| 343 | //Class: \DateTime |
||
| 344 | $fieldClassFull = $fieldClassName; |
||
| 345 | } else { |
||
| 346 | $fieldClassFull = $phpNamespace->getName().'\\'.$fieldClassName; |
||
| 347 | $this->info('Uso class for field same namespace', [ |
||
| 348 | 'class' => $this->currentClass->getName(), |
||
| 349 | 'field' => $fieldClassName, |
||
| 350 | 'className' => $fieldClassFull, |
||
| 351 | ]); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | if ($config->haveConstructor && !$isStatic) { |
||
| 356 | $parameter = null; |
||
| 357 | if (!$isAutoinizialize) { |
||
| 358 | $this->info('Aggiungo parametro al costruttore', [ |
||
| 359 | 'class' => $this->currentClass->getName(), |
||
| 360 | 'parameter' => $name, |
||
| 361 | 'className' => $fieldClassFull, |
||
| 362 | 'default' => $defaultValue, |
||
| 363 | 'autoinizialize' => $isAutoinizialize, |
||
| 364 | ]); |
||
| 365 | if (!$first) { |
||
| 366 | $parameter = $methodConstructor->addParameter($name, null); //solo i primitivi hanno un default, gli altri null come object |
||
| 367 | $parameter->setTypeHint($fieldClassFull); |
||
| 368 | } else { |
||
| 369 | $parameter = $methodConstructor->addParameter($name); |
||
| 370 | $parameter->setTypeHint($fieldClassFull); |
||
| 371 | } |
||
| 372 | } else { |
||
| 373 | $this->info('Skip parametro al costruttore -> autoinizialize true', [ |
||
| 374 | 'class' => $this->currentClass->getName(), |
||
| 375 | 'parameter' => $name, |
||
| 376 | 'className' => $fieldClassFull, |
||
| 377 | 'default' => $defaultValue, |
||
| 378 | 'autoinizialize' => $isAutoinizialize, |
||
| 379 | ]); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | View Code Duplication | if (array_key_exists($fieldClassName, $typesReference)) { |
|
| 384 | $this->info('Add field type class with namespace', [ |
||
| 385 | 'class' => $this->currentClass->getName(), |
||
| 386 | 'field' => $fieldClassName, |
||
| 387 | 'className' => $fieldClassFull, |
||
| 388 | ]); |
||
| 389 | $this->currentClass->getNamespace()->addUse($fieldClassFull); |
||
| 390 | } |
||
| 391 | } else { |
||
| 392 | //tipo primitivo |
||
| 393 | $fieldClassName = $fieldProperties['primitive']; |
||
| 394 | $fieldNamespace = null; |
||
| 395 | $fieldClassFull = $fieldProperties['primitive']; |
||
| 396 | if ($config->haveConstructor && !$isStatic) { |
||
| 397 | //FIXME: se sono in php7 ho anche gli altri elementi primitivi |
||
| 398 | //@see: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration |
||
| 399 | |||
| 400 | $parameter = null; |
||
| 401 | |||
| 402 | if (!$isAutoinizialize) { |
||
| 403 | if (is_null($defaultValue)) { |
||
| 404 | $this->info('Aggiungo parametro al costruttore', [ |
||
| 405 | 'class' => $this->currentClass->getName(), |
||
| 406 | 'parameter' => $name, |
||
| 407 | 'className' => $fieldClassFull, |
||
| 408 | 'default' => $defaultValue, |
||
| 409 | 'autoinizialize' => $isAutoinizialize, |
||
| 410 | ]); |
||
| 411 | |||
| 412 | //PHP7 ONLY |
||
| 413 | // if ($fieldClassFull == 'int') { |
||
| 414 | // $parameter->setTypeHint('int'); |
||
| 415 | // } |
||
| 416 | |||
| 417 | if (!$first) { |
||
| 418 | $parameter = $methodConstructor->addParameter($name, null); |
||
| 419 | } else { |
||
| 420 | $parameter = $methodConstructor->addParameter($name); |
||
| 421 | } |
||
| 422 | |||
| 423 | if ($fieldClassFull == 'array') { |
||
| 424 | $parameter->setTypeHint('array'); |
||
| 425 | } else { |
||
| 426 | if ($defaultValue != null) { |
||
| 427 | /* @var $parameter \Nette\PhpGenerator\Parameter */ |
||
| 428 | $parameter->setDefaultValue(''.$defaultValue); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | $this->info('Check autoinizialize field', [ |
||
| 437 | 'class' => $this->currentClass->getName(), |
||
| 438 | 'field' => $name, |
||
| 439 | 'autoinizialize' => $isAutoinizialize, |
||
| 440 | 'default' => $defaultValue, |
||
| 441 | ]); |
||
| 442 | |||
| 443 | $comment = 'no description available'; |
||
| 444 | if (array_key_exists('description', $fieldProperties)) { |
||
| 445 | $comment = $fieldProperties['description']; |
||
| 446 | } else { |
||
| 447 | if (!is_null($typesDescription) && array_key_exists($fieldClassName, $typesDescription)) { |
||
| 448 | $comment = $typesDescription[$fieldClassName]; |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | if (!$config->isInterface) { |
||
| 453 | /** $field @var \Nette\PhpGenerator\Property */ |
||
| 454 | $field = $this->currentClass->addProperty($name); |
||
| 455 | $field->setStatic($isStatic); |
||
| 456 | if ($config->isEnum) { |
||
| 457 | $field->setVisibility('protected'); |
||
| 458 | } else { |
||
| 459 | $field->setVisibility('private'); |
||
| 460 | } |
||
| 461 | $field->addComment($comment)->addComment('@var '.$fieldClassFull); |
||
| 462 | } |
||
| 463 | |||
| 464 | $createSetter = $config->haveSetter; |
||
| 465 | if (array_key_exists('setter', $fieldProperties)) { |
||
| 466 | $createSetter = $fieldProperties['setter']; |
||
| 467 | } |
||
| 468 | |||
| 469 | $createGetter = $config->haveGetter; |
||
| 470 | if (array_key_exists('getter', $fieldProperties)) { |
||
| 471 | $createGetter = $fieldProperties['getter']; |
||
| 472 | } |
||
| 473 | |||
| 474 | if ($config->isInterface) { |
||
| 475 | if ($createGetter) { |
||
| 476 | $this->addGetter($name, $fieldClassFull, $isStatic, false); |
||
| 477 | } |
||
| 478 | |||
| 479 | if ($createSetter) { |
||
| 480 | $this->addSetter($name, $fieldClassFull, $isStatic, false); |
||
| 481 | } |
||
| 482 | } else { |
||
| 483 | if ($createGetter) { |
||
| 484 | $this->addGetter($name, $fieldClassFull, $isStatic, true); |
||
| 485 | } |
||
| 486 | |||
| 487 | if ($createSetter) { |
||
| 488 | $this->addSetter($name, $fieldClassFull, $isStatic, true); |
||
| 489 | } |
||
| 490 | } |
||
| 491 | if (!$isAutoinizialize) { |
||
| 492 | $first = false; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | if ($config->haveConstructor) { |
||
| 496 | $methodConstructor->setBody($body, []); |
||
| 497 | } |
||
| 498 | } //end fields |
||
| 499 | |||
| 500 | if (array_key_exists('methods', $properties)) { |
||
| 501 | $body = ''; |
||
| 502 | |||
| 503 | foreach ($properties['methods'] as $methodName => $methodsProperties) { |
||
| 504 | $this->info('Aggiungo method', [ |
||
| 505 | 'class' => $this->currentClass->getName(), |
||
| 506 | 'methodName' => $methodName, |
||
| 507 | 'methodProp' => $methodsProperties, |
||
| 508 | ]); |
||
| 509 | |||
| 510 | /** $newMethodCall @var \Nette\PhpGenerator\Method */ |
||
| 511 | $newMethodCall = $this->currentClass->addMethod($methodName); |
||
| 512 | $newMethodCall->setFinal(true); |
||
| 513 | |||
| 514 | $newMethodCall->setStatic(false); |
||
| 515 | if (array_key_exists('static', $methodsProperties)) { |
||
| 516 | $newMethodCall->setStatic($methodsProperties['static']); |
||
| 517 | } |
||
| 518 | |||
| 519 | if (array_key_exists('description', $methodsProperties)) { |
||
| 520 | $newMethodCall->setVisibility($methodsProperties['visibility']); |
||
| 521 | } else { |
||
| 522 | $newMethodCall->setVisibility('public'); |
||
| 523 | } |
||
| 524 | |||
| 525 | if (array_key_exists('description', $methodsProperties)) { |
||
| 526 | $newMethodCall->addComment($methodsProperties['description']); |
||
| 527 | } else { |
||
| 528 | $returnType = 'void'; |
||
| 529 | if (array_key_exists('@return', $methodsProperties)) { |
||
| 530 | $returnType = $methodsProperties['@return']; |
||
| 531 | //TODO: .'|null' va messo in quale condizione? |
||
| 532 | $newMethodCall->addComment('@return '.$returnType); |
||
| 533 | } else { |
||
| 534 | //NOPE |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | if (array_key_exists('params', $methodsProperties)) { |
||
| 539 | foreach ($methodsProperties['params'] as $paramName => $paramProp) { |
||
| 540 | if (array_key_exists('class', $paramProp)) { |
||
| 541 | $newMethodCall->addParameter($paramName)->setTypeHint($paramProp['class']); |
||
| 542 | } |
||
| 543 | if (array_key_exists('primitive', $paramProp)) { |
||
| 544 | $newMethodCall->addParameter($paramName); |
||
| 545 | } |
||
| 546 | } |
||
| 547 | } |
||
| 548 | $body = ' // FIMXE: da implementare '; |
||
| 549 | if (array_key_exists('body', $methodsProperties)) { |
||
| 550 | $body = $methodsProperties['body']; |
||
| 551 | } |
||
| 552 | $newMethodCall->setBody($body); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | |||
| 556 | if ($config->isEnum) { |
||
| 557 | $this->currentClass->setAbstract(true); |
||
| 558 | $this->addSingleton('Singleton instance for enum', false); |
||
| 559 | $this->addParseString(); |
||
| 560 | } |
||
| 561 | |||
| 562 | if ($config->isSingleton) { |
||
| 563 | $this->addSingleton('Singleton instance', true); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | public function createFileOnDir(Local $adapter) |
||
| 572 | } |
||
| 573 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.