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 AbstractEditHandler 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 AbstractEditHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractEditHandler |
||
| 36 | { |
||
| 37 | use TranslatorTrait; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Name of treated object type. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $objectType; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Name of treated object type starting with upper case. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $objectTypeCapital; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Lower case version. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $objectTypeLower; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Permission component based on object type. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $permissionComponent; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Reference to treated entity instance. |
||
| 69 | * |
||
| 70 | * @var EntityAccess |
||
| 71 | */ |
||
| 72 | protected $entityRef = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * List of identifier names. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $idFields = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * List of identifiers of treated entity. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $idValues = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Code defining the redirect goal after command handling. |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $returnTo = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Whether a create action is going to be repeated or not. |
||
| 97 | * |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | protected $repeatCreateAction = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Url of current form with all parameters for multiple creations. |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $repeatReturnUrl = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Whether an existing item is used as template for a new one. |
||
| 111 | * |
||
| 112 | * @var boolean |
||
| 113 | */ |
||
| 114 | protected $hasTemplateId = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Whether the PageLock extension is used for this entity type or not. |
||
| 118 | * |
||
| 119 | * @var boolean |
||
| 120 | */ |
||
| 121 | protected $hasPageLockSupport = false; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var ContainerBuilder |
||
| 125 | */ |
||
| 126 | protected $container; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The current request. |
||
| 130 | * |
||
| 131 | * @var Request |
||
| 132 | */ |
||
| 133 | protected $request = null; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The router. |
||
| 137 | * |
||
| 138 | * @var RouterInterface |
||
| 139 | */ |
||
| 140 | protected $router = null; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The handled form type. |
||
| 144 | * |
||
| 145 | * @var AbstractType |
||
| 146 | */ |
||
| 147 | protected $form = null; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Template parameters. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $templateParameters = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Constructor. |
||
| 158 | * |
||
| 159 | * @param ContainerBuilder $container ContainerBuilder service instance |
||
| 160 | * @param TranslatorInterface $translator Translator service instance |
||
| 161 | * @param RequestStack $requestStack RequestStack service instance |
||
| 162 | * @param RouterInterface $router Router service instance |
||
| 163 | */ |
||
| 164 | public function __construct(ContainerBuilder $container, TranslatorInterface $translator, RequestStack $requestStack, RouterInterface $router) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Sets the translator. |
||
| 174 | * |
||
| 175 | * @param TranslatorInterface $translator Translator service instance |
||
| 176 | */ |
||
| 177 | public function setTranslator(/*TranslatorInterface */$translator) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Initialise form handler. |
||
| 184 | * |
||
| 185 | * This method takes care of all necessary initialisation of our data and form states. |
||
| 186 | * |
||
| 187 | * @param array $templateParameters List of preassigned template variables |
||
| 188 | * |
||
| 189 | * @return boolean False in case of initialisation errors, otherwise true |
||
| 190 | * |
||
| 191 | * @throws NotFoundHttpException Thrown if item to be edited isn't found |
||
| 192 | * @throws RuntimeException Thrown if the workflow actions can not be determined |
||
| 193 | */ |
||
| 194 | public function processForm(array $templateParameters) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Creates the form type. |
||
| 301 | */ |
||
| 302 | protected function createForm() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the template parameters. |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getTemplateParameters() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create concatenated identifier string (for composite keys). |
||
| 320 | * |
||
| 321 | * @return String concatenated identifiers |
||
| 322 | */ |
||
| 323 | protected function createCompositeIdentifier() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Initialise existing entity for editing. |
||
| 338 | * |
||
| 339 | * @return EntityAccess|null Desired entity instance or null |
||
| 340 | * |
||
| 341 | * @throws NotFoundHttpException Thrown if item to be edited isn't found |
||
| 342 | */ |
||
| 343 | protected function initEntityForEditing() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Initialise new entity for creation. |
||
| 358 | * |
||
| 359 | * @return EntityAccess|null Desired entity instance or null |
||
| 360 | * |
||
| 361 | * @throws NotFoundHttpException Thrown if item to be cloned isn't found |
||
| 362 | */ |
||
| 363 | protected function initEntityForCreation() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get list of allowed redirect codes. |
||
| 401 | * |
||
| 402 | * @return array list of possible redirect codes |
||
| 403 | */ |
||
| 404 | protected function getRedirectCodes() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Command event handler. |
||
| 428 | * |
||
| 429 | * @param array $args List of arguments |
||
| 430 | * |
||
| 431 | * @return mixed Redirect or false on errors |
||
| 432 | */ |
||
| 433 | public function handleCommand($args = []) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get success or error message for default operations. |
||
| 471 | * |
||
| 472 | * @param array $args arguments from handleCommand method |
||
| 473 | * @param Boolean $success true if this is a success, false for default error |
||
| 474 | * |
||
| 475 | * @return String desired status or error message |
||
| 476 | */ |
||
| 477 | protected function getDefaultMessage($args, $success = false) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Add success or error message to session. |
||
| 509 | * |
||
| 510 | * @param array $args arguments from handleCommand method |
||
| 511 | * @param Boolean $success true if this is a success, false for default error |
||
| 512 | * |
||
| 513 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 514 | */ |
||
| 515 | protected function addDefaultMessage($args, $success = false) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Input data processing called by handleCommand method. |
||
| 535 | * |
||
| 536 | * @param array $args Additional arguments |
||
| 537 | */ |
||
| 538 | public function fetchInputData($args) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * This method executes a certain workflow action. |
||
| 557 | * |
||
| 558 | * @param array $args Arguments from handleCommand method |
||
| 559 | * |
||
| 560 | * @return bool Whether everything worked well or not |
||
| 561 | */ |
||
| 562 | public function applyAction(array $args = []) |
||
| 567 | } |
||
| 568 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.