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 WidgetMap 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 WidgetMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WidgetMap |
||
| 16 | { |
||
| 17 | const ACTION_CREATE = 'create'; |
||
| 18 | const ACTION_OVERWRITE = 'overwrite'; |
||
| 19 | const ACTION_DELETE = 'delete'; |
||
| 20 | |||
| 21 | const POSITION_BEFORE = 'before'; |
||
| 22 | const POSITION_AFTER = 'after'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | * |
||
| 27 | * @ORM\Column(name="id", type="integer") |
||
| 28 | * @ORM\Id |
||
| 29 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 30 | */ |
||
| 31 | protected $id; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | * |
||
| 36 | * @ORM\Column(name="action", type="string", length=255) |
||
| 37 | */ |
||
| 38 | protected $action = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var View |
||
| 42 | * |
||
| 43 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", inversedBy="widgetMaps") |
||
| 44 | * @ORM\JoinColumn(name="view_id", referencedColumnName="id") |
||
| 45 | */ |
||
| 46 | protected $view; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Widget |
||
| 50 | * |
||
| 51 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget", inversedBy="widgetMaps") |
||
| 52 | * @ORM\JoinColumn(name="widget_id", referencedColumnName="id") |
||
| 53 | */ |
||
| 54 | protected $widget; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="substitutes") |
||
| 58 | * @ORM\JoinColumn(name="replaced_id", referencedColumnName="id") |
||
| 59 | */ |
||
| 60 | protected $replaced; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ArrayCollection |
||
| 64 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="replaced") |
||
| 65 | */ |
||
| 66 | protected $substitutes; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | * |
||
| 71 | * @ORM\Column(name="asynchronous", type="boolean") |
||
| 72 | */ |
||
| 73 | protected $asynchronous = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="children") |
||
| 77 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 78 | */ |
||
| 79 | protected $parent; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | * |
||
| 84 | * @ORM\Column(name="position", type="string", nullable=true) |
||
| 85 | */ |
||
| 86 | protected $position; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var Collection |
||
| 90 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="parent") |
||
| 91 | */ |
||
| 92 | protected $children; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | * |
||
| 97 | * @ORM\Column(name="slot", type="string", length=255, nullable=true) |
||
| 98 | */ |
||
| 99 | protected $slot; |
||
| 100 | |||
| 101 | public function __construct() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function getId() |
||
| 114 | |||
| 115 | public function setId($id) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return null |
||
| 122 | */ |
||
| 123 | public function isAsynchronous() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param null $asynchronous |
||
| 130 | */ |
||
| 131 | public function setAsynchronous($asynchronous) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set the action. |
||
| 138 | * |
||
| 139 | * @param string $action |
||
| 140 | * |
||
| 141 | * @throws \Exception The action is not valid |
||
| 142 | */ |
||
| 143 | public function setAction($action) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the action. |
||
| 155 | * |
||
| 156 | * @return string The action |
||
| 157 | */ |
||
| 158 | public function getAction() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return Widget |
||
| 165 | */ |
||
| 166 | public function getWidget() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param Widget $widget |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function setWidget(Widget $widget) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return View |
||
| 185 | */ |
||
| 186 | public function getView() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param View $view |
||
| 193 | */ |
||
| 194 | public function setView(View $view) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function getReplaced() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param mixed $replaced |
||
| 209 | */ |
||
| 210 | public function setReplaced($replaced) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getSlot() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $slot |
||
| 228 | */ |
||
| 229 | public function setSlot($slot) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return mixed |
||
| 236 | */ |
||
| 237 | public function getChildren(View $view = null) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function getChildrenRaw() |
||
| 300 | |||
| 301 | public function hasChild($position, View $view = null) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return WidgetMap|null |
||
| 314 | */ |
||
| 315 | public function getChild($position) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return [WidgetMap] |
||
| 329 | */ |
||
| 330 | public function getChilds($position) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param mixed $children |
||
| 344 | */ |
||
| 345 | public function setChildren($children) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | public function removeChildren() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param WidgetMap $child |
||
| 362 | */ |
||
| 363 | public function addChild($child) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param WidgetMap $child |
||
| 370 | */ |
||
| 371 | public function removeChild($child) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return WidgetMap|null |
||
| 378 | */ |
||
| 379 | public function getParent() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param mixed $parent |
||
| 386 | */ |
||
| 387 | public function setParent(WidgetMap $parent = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function getPosition() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $position |
||
| 408 | */ |
||
| 409 | public function setPosition($position) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | public function getSubstitutes() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return mixed |
||
| 424 | */ |
||
| 425 | public function getSubstituteForView(View $view) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param WidgetMap $substitute |
||
| 438 | */ |
||
| 439 | public function addSubstitute(WidgetMap $substitute) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param [WidgetMap] $substitutes |
||
| 446 | */ |
||
| 447 | public function setSubstitutes($substitutes) |
||
| 451 | } |
||
| 452 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.