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 |
||
| 41 | abstract class AbstractEditHandler |
||
| 42 | { |
||
| 43 | use TranslatorTrait; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Name of treated object type. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $objectType; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Name of treated object type starting with upper case. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $objectTypeCapital; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Lower case version. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $objectTypeLower; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Permission component based on object type. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $permissionComponent; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Reference to treated entity instance. |
||
| 75 | * |
||
| 76 | * @var EntityAccess |
||
| 77 | */ |
||
| 78 | protected $entityRef = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * List of identifier names. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $idFields = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * List of identifiers of treated entity. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $idValues = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Code defining the redirect goal after command handling. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $returnTo = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Whether a create action is going to be repeated or not. |
||
| 103 | * |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $repeatCreateAction = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Url of current form with all parameters for multiple creations. |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | protected $repeatReturnUrl = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Whether an existing item is used as template for a new one. |
||
| 117 | * |
||
| 118 | * @var boolean |
||
| 119 | */ |
||
| 120 | protected $hasTemplateId = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Whether the PageLock extension is used for this entity type or not. |
||
| 124 | * |
||
| 125 | * @var boolean |
||
| 126 | */ |
||
| 127 | protected $hasPageLockSupport = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var ZikulaHttpKernelInterface |
||
| 131 | */ |
||
| 132 | protected $kernel; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var FormFactoryInterface |
||
| 136 | */ |
||
| 137 | protected $formFactory; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The current request. |
||
| 141 | * |
||
| 142 | * @var Request |
||
| 143 | */ |
||
| 144 | protected $request; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * The router. |
||
| 148 | * |
||
| 149 | * @var RouterInterface |
||
| 150 | */ |
||
| 151 | protected $router; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var LoggerInterface |
||
| 155 | */ |
||
| 156 | protected $logger; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var PermissionApiInterface |
||
| 160 | */ |
||
| 161 | protected $permissionApi; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var CurrentUserApi |
||
| 165 | */ |
||
| 166 | protected $currentUserApi; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var RoutesFactory |
||
| 170 | */ |
||
| 171 | protected $entityFactory; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var ControllerHelper |
||
| 175 | */ |
||
| 176 | protected $controllerHelper; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var ModelHelper |
||
| 180 | */ |
||
| 181 | protected $modelHelper; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var SelectionHelper |
||
| 185 | */ |
||
| 186 | protected $selectionHelper; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var WorkflowHelper |
||
| 190 | */ |
||
| 191 | protected $workflowHelper; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Reference to optional locking api. |
||
| 195 | * |
||
| 196 | * @var LockingApi |
||
| 197 | */ |
||
| 198 | protected $lockingApi = null; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The handled form type. |
||
| 202 | * |
||
| 203 | * @var AbstractType |
||
| 204 | */ |
||
| 205 | protected $form; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Template parameters. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | protected $templateParameters = []; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * EditHandler constructor. |
||
| 216 | * |
||
| 217 | * @param ZikulaHttpKernelInterface $kernel Kernel service instance |
||
| 218 | * @param TranslatorInterface $translator Translator service instance |
||
| 219 | * @param FormFactoryInterface $formFactory FormFactory service instance |
||
| 220 | * @param RequestStack $requestStack RequestStack service instance |
||
| 221 | * @param RouterInterface $router Router service instance |
||
| 222 | * @param LoggerInterface $logger Logger service instance |
||
| 223 | * @param PermissionApiInterface $permissionApi PermissionApi service instance |
||
| 224 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 225 | * @param RoutesFactory $entityFactory RoutesFactory service instance |
||
| 226 | * @param ControllerHelper $controllerHelper ControllerHelper service instance |
||
| 227 | * @param ModelHelper $modelHelper ModelHelper service instance |
||
| 228 | * @param SelectionHelper $selectionHelper SelectionHelper service instance |
||
| 229 | * @param WorkflowHelper $workflowHelper WorkflowHelper service instance |
||
| 230 | */ |
||
| 231 | public function __construct( |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Sets the translator. |
||
| 263 | * |
||
| 264 | * @param TranslatorInterface $translator Translator service instance |
||
| 265 | */ |
||
| 266 | public function setTranslator(/*TranslatorInterface */$translator) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Initialise form handler. |
||
| 273 | * |
||
| 274 | * This method takes care of all necessary initialisation of our data and form states. |
||
| 275 | * |
||
| 276 | * @param array $templateParameters List of preassigned template variables |
||
| 277 | * |
||
| 278 | * @return boolean False in case of initialisation errors, otherwise true |
||
| 279 | * |
||
| 280 | * @throws RuntimeException Thrown if the workflow actions can not be determined |
||
| 281 | */ |
||
| 282 | public function processForm(array $templateParameters) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Creates the form type. |
||
| 396 | */ |
||
| 397 | protected function createForm() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the template parameters. |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function getTemplateParameters() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Create concatenated identifier string (for composite keys). |
||
| 415 | * |
||
| 416 | * @return String concatenated identifiers |
||
| 417 | */ |
||
| 418 | protected function createCompositeIdentifier() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Initialise existing entity for editing. |
||
| 433 | * |
||
| 434 | * @return EntityAccess|null Desired entity instance or null |
||
| 435 | */ |
||
| 436 | protected function initEntityForEditing() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Initialise new entity for creation. |
||
| 450 | * |
||
| 451 | * @return EntityAccess|null Desired entity instance or null |
||
| 452 | */ |
||
| 453 | protected function initEntityForCreation() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get list of allowed redirect codes. |
||
| 489 | * |
||
| 490 | * @return array list of possible redirect codes |
||
| 491 | */ |
||
| 492 | protected function getRedirectCodes() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Command event handler. |
||
| 503 | * |
||
| 504 | * @param array $args List of arguments |
||
| 505 | * |
||
| 506 | * @return mixed Redirect or false on errors |
||
| 507 | */ |
||
| 508 | public function handleCommand($args = []) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Get success or error message for default operations. |
||
| 548 | * |
||
| 549 | * @param array $args arguments from handleCommand method |
||
| 550 | * @param Boolean $success true if this is a success, false for default error |
||
| 551 | * |
||
| 552 | * @return String desired status or error message |
||
| 553 | */ |
||
| 554 | protected function getDefaultMessage($args, $success = false) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Add success or error message to session. |
||
| 586 | * |
||
| 587 | * @param array $args arguments from handleCommand method |
||
| 588 | * @param Boolean $success true if this is a success, false for default error |
||
| 589 | * |
||
| 590 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 591 | */ |
||
| 592 | protected function addDefaultMessage($args, $success = false) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Input data processing called by handleCommand method. |
||
| 611 | * |
||
| 612 | * @param array $args Additional arguments |
||
| 613 | */ |
||
| 614 | public function fetchInputData($args) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * This method executes a certain workflow action. |
||
| 642 | * |
||
| 643 | * @param array $args Arguments from handleCommand method |
||
| 644 | * |
||
| 645 | * @return bool Whether everything worked well or not |
||
| 646 | */ |
||
| 647 | public function applyAction(array $args = []) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Sets optional locking api reference. |
||
| 655 | * |
||
| 656 | * @param LockingApi $lockingApi |
||
| 657 | */ |
||
| 658 | public function setLockingApi(LockingApi $lockingApi) |
||
| 662 | } |
||
| 663 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.