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 WorkflowTemplate 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 WorkflowTemplate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class WorkflowTemplate { |
||
|
|
|||
| 61 | protected $name; |
||
| 62 | protected $description; |
||
| 63 | protected $version; |
||
| 64 | protected $remindDays; |
||
| 65 | protected $sort; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * An array representation of the structure of this workflow template |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $structure; |
||
| 73 | |||
| 74 | public function __construct($name, $description = '', $version = '0.0', $remindDays = 0, $sort = 0) { |
||
| 75 | $this->name = $name; |
||
| 76 | $this->description = $description; |
||
| 77 | $this->version = $version; |
||
| 78 | $this->remindDays = $remindDays; |
||
| 79 | $this->sort = $sort; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getName() { |
||
| 85 | |||
| 86 | public function getVersion() { |
||
| 89 | |||
| 90 | public function getDescription() { |
||
| 93 | |||
| 94 | public function getRemindDays() { |
||
| 97 | |||
| 98 | public function getSort() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set the structure for this template |
||
| 104 | * |
||
| 105 | * @param array $structure |
||
| 106 | */ |
||
| 107 | public function setStructure($structure) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Creates the relevant data objects for this structure, returning an array |
||
| 113 | * of actions in the order they've been created |
||
| 114 | * |
||
| 115 | * @param WorkflowDefinition $definitino |
||
| 116 | * An optional workflow definition to bind the actions into |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function createRelations($definition = null) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Create a workflow action based on a template |
||
| 165 | * |
||
| 166 | * @param string $name |
||
| 167 | * @param array $template |
||
| 168 | * @param WorkflowDefinition $definition |
||
| 169 | * @return WorkflowAction |
||
| 170 | */ |
||
| 171 | protected function createAction($name, $actionTemplate, WorkflowDefinition $definition = null) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Create a WorkflowDefinition->Users relation based on template data. But only if the related groups from the |
||
| 201 | * export, can be foud in the target environment's DB. |
||
| 202 | * |
||
| 203 | * Note: The template gives us a Member Email to work with rather than an ID as it's arguably |
||
| 204 | * more likely that Member Emails will be the same between environments than their IDs. |
||
| 205 | * |
||
| 206 | * @param array $users |
||
| 207 | * @param WorkflowDefinition $definition |
||
| 208 | * @param boolean $clear |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | protected function createUsers($users, WorkflowDefinition $definition, $clear = false) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Create a WorkflowDefinition->Groups relation based on template data, But only if the related groups from the |
||
| 219 | * export, can be foud in the target environment's DB. |
||
| 220 | * |
||
| 221 | * Note: The template gives us a Group Title to work with rther than an ID as it's arguably |
||
| 222 | * more likely that Group titles will be the same between environments than their IDs. |
||
| 223 | * |
||
| 224 | * @param array $groups |
||
| 225 | * @param WorkflowDefinition $definition |
||
| 226 | * @param boolean $clear |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function createGroups($groups, WorkflowDefinition $definition, $clear = false) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Update the transitions for a given action |
||
| 237 | * |
||
| 238 | * @param array $actionTemplate |
||
| 239 | * @param WorkflowAction $action |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | protected function updateActionTransitions($actionTemplate, $action) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Update a workflow definition |
||
| 284 | * |
||
| 285 | * @param WorkflowDefinition $definition |
||
| 286 | * The definition to update |
||
| 287 | */ |
||
| 288 | public function updateDefinition(WorkflowDefinition $definition) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Given an object, first check it has a ManyMany relation on it and add() Member and Group relations as required. |
||
| 360 | * |
||
| 361 | * @param Object $object (e.g. WorkflowDefinition, WorkflowAction, WorkflowTransition) |
||
| 362 | * @param array $source Usually data taken from a YAML template |
||
| 363 | * @param boolean $clear Lose/keep Group/Member relations on $object (useful for reloading/refreshing definition) |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | protected function addManyManyToObject($object, $source, $clear = false) { |
||
| 418 | } |
||
| 419 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.