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 |
||
| 42 | abstract class AbstractEditHandler |
||
| 43 | { |
||
| 44 | use TranslatorTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Name of treated object type. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $objectType; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Name of treated object type starting with upper case. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $objectTypeCapital; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Lower case version. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $objectTypeLower; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Permission component based on object type. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $permissionComponent; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Reference to treated entity instance. |
||
| 76 | * |
||
| 77 | * @var EntityAccess |
||
| 78 | */ |
||
| 79 | protected $entityRef = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * List of identifier names. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $idFields = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * List of identifiers of treated entity. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $idValues = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Code defining the redirect goal after command handling. |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $returnTo = null; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Whether a create action is going to be repeated or not. |
||
| 104 | * |
||
| 105 | * @var boolean |
||
| 106 | */ |
||
| 107 | protected $repeatCreateAction = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Url of current form with all parameters for multiple creations. |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $repeatReturnUrl = null; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Whether an existing item is used as template for a new one. |
||
| 118 | * |
||
| 119 | * @var boolean |
||
| 120 | */ |
||
| 121 | protected $hasTemplateId = false; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Whether the PageLock extension is used for this entity type or not. |
||
| 125 | * |
||
| 126 | * @var boolean |
||
| 127 | */ |
||
| 128 | protected $hasPageLockSupport = false; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var KernelInterface |
||
| 132 | */ |
||
| 133 | protected $kernel; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var FormFactoryInterface |
||
| 137 | */ |
||
| 138 | protected $formFactory; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The current request. |
||
| 142 | * |
||
| 143 | * @var Request |
||
| 144 | */ |
||
| 145 | protected $request; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The router. |
||
| 149 | * |
||
| 150 | * @var RouterInterface |
||
| 151 | */ |
||
| 152 | protected $router; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var LoggerInterface |
||
| 156 | */ |
||
| 157 | protected $logger; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var PermissionApi |
||
| 161 | */ |
||
| 162 | protected $permissionApi; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var CurrentUserApi |
||
| 166 | */ |
||
| 167 | protected $currentUserApi; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var RoutesFactory |
||
| 171 | */ |
||
| 172 | protected $entityFactory; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var ControllerHelper |
||
| 176 | */ |
||
| 177 | protected $controllerHelper; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var ModelHelper |
||
| 181 | */ |
||
| 182 | protected $modelHelper; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var SelectionHelper |
||
| 186 | */ |
||
| 187 | protected $selectionHelper; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var WorkflowHelper |
||
| 191 | */ |
||
| 192 | protected $workflowHelper; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Reference to optional locking api. |
||
| 196 | * |
||
| 197 | * @var LockingApi |
||
| 198 | */ |
||
| 199 | protected $lockingApi = null; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The handled form type. |
||
| 203 | * |
||
| 204 | * @var AbstractType |
||
| 205 | */ |
||
| 206 | protected $form; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Template parameters. |
||
| 210 | * |
||
| 211 | * @var array |
||
| 212 | */ |
||
| 213 | protected $templateParameters = []; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * EditHandler constructor. |
||
| 217 | * |
||
| 218 | * @param KernelInterface $kernel Kernel service instance |
||
| 219 | * @param TranslatorInterface $translator Translator service instance |
||
| 220 | * @param FormFactoryInterface $formFactory FormFactory service instance |
||
| 221 | * @param RequestStack $requestStack RequestStack service instance |
||
| 222 | * @param RouterInterface $router Router service instance |
||
| 223 | * @param LoggerInterface $logger Logger service instance |
||
| 224 | * @param PermissionApi $permissionApi PermissionApi service instance |
||
| 225 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 226 | * @param RoutesFactory $entityFactory RoutesFactory service instance |
||
| 227 | * @param ControllerHelper $controllerHelper ControllerHelper service instance |
||
| 228 | * @param ModelHelper $modelHelper ModelHelper service instance |
||
| 229 | * @param SelectionHelper $selectionHelper SelectionHelper service instance |
||
| 230 | * @param WorkflowHelper $workflowHelper WorkflowHelper service instance |
||
| 231 | */ |
||
| 232 | public function __construct( |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets the translator. |
||
| 264 | * |
||
| 265 | * @param TranslatorInterface $translator Translator service instance |
||
| 266 | */ |
||
| 267 | public function setTranslator(/*TranslatorInterface */$translator) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Initialise form handler. |
||
| 274 | * |
||
| 275 | * This method takes care of all necessary initialisation of our data and form states. |
||
| 276 | * |
||
| 277 | * @param array $templateParameters List of preassigned template variables |
||
| 278 | * |
||
| 279 | * @return boolean False in case of initialisation errors, otherwise true |
||
| 280 | * |
||
| 281 | * @throws NotFoundHttpException Thrown if item to be edited isn't found |
||
| 282 | * @throws RuntimeException Thrown if the workflow actions can not be determined |
||
| 283 | */ |
||
| 284 | public function processForm(array $templateParameters) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Creates the form type. |
||
| 382 | */ |
||
| 383 | protected function createForm() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the template parameters. |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getTemplateParameters() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Create concatenated identifier string (for composite keys). |
||
| 401 | * |
||
| 402 | * @return String concatenated identifiers |
||
| 403 | */ |
||
| 404 | protected function createCompositeIdentifier() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Initialise existing entity for editing. |
||
| 419 | * |
||
| 420 | * @return EntityAccess|null Desired entity instance or null |
||
| 421 | * |
||
| 422 | * @throws NotFoundHttpException Thrown if item to be edited isn't found |
||
| 423 | */ |
||
| 424 | protected function initEntityForEditing() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Initialise new entity for creation. |
||
| 438 | * |
||
| 439 | * @return EntityAccess|null Desired entity instance or null |
||
| 440 | * |
||
| 441 | * @throws NotFoundHttpException Thrown if item to be cloned isn't found |
||
| 442 | */ |
||
| 443 | protected function initEntityForCreation() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get list of allowed redirect codes. |
||
| 479 | * |
||
| 480 | * @return array list of possible redirect codes |
||
| 481 | */ |
||
| 482 | protected function getRedirectCodes() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Command event handler. |
||
| 493 | * |
||
| 494 | * @param array $args List of arguments |
||
| 495 | * |
||
| 496 | * @return mixed Redirect or false on errors |
||
| 497 | */ |
||
| 498 | public function handleCommand($args = []) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get success or error message for default operations. |
||
| 538 | * |
||
| 539 | * @param array $args arguments from handleCommand method |
||
| 540 | * @param Boolean $success true if this is a success, false for default error |
||
| 541 | * |
||
| 542 | * @return String desired status or error message |
||
| 543 | */ |
||
| 544 | protected function getDefaultMessage($args, $success = false) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Add success or error message to session. |
||
| 576 | * |
||
| 577 | * @param array $args arguments from handleCommand method |
||
| 578 | * @param Boolean $success true if this is a success, false for default error |
||
| 579 | * |
||
| 580 | * @throws RuntimeException Thrown if executing the workflow action fails |
||
| 581 | */ |
||
| 582 | protected function addDefaultMessage($args, $success = false) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Input data processing called by handleCommand method. |
||
| 601 | * |
||
| 602 | * @param array $args Additional arguments |
||
| 603 | */ |
||
| 604 | public function fetchInputData($args) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * This method executes a certain workflow action. |
||
| 623 | * |
||
| 624 | * @param array $args Arguments from handleCommand method |
||
| 625 | * |
||
| 626 | * @return bool Whether everything worked well or not |
||
| 627 | */ |
||
| 628 | public function applyAction(array $args = []) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Sets optional locking api reference. |
||
| 636 | * |
||
| 637 | * @param LockingApi $lockingApi |
||
| 638 | */ |
||
| 639 | public function setLockingApi(LockingApi $lockingApi) |
||
| 643 | } |
||
| 644 |
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.