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", onDelete="cascade") |
||
| 45 | */ |
||
| 46 | protected $view; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Widget |
||
| 50 | * |
||
| 51 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget", mappedBy="widgetMap", orphanRemoval=true, cascade={"persist", "remove"}) |
||
| 52 | */ |
||
| 53 | protected $widgets; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @deprecated |
||
| 57 | * @var Widget |
||
| 58 | * |
||
| 59 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget") |
||
| 60 | * @ORM\JoinColumn(name="widget_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 61 | */ |
||
| 62 | protected $widget; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="substitutes") |
||
| 66 | * @ORM\JoinColumn(name="replaced_id", referencedColumnName="id") |
||
| 67 | */ |
||
| 68 | protected $replaced; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ArrayCollection |
||
| 72 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="replaced") |
||
| 73 | */ |
||
| 74 | protected $substitutes; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | * |
||
| 79 | * @ORM\Column(name="asynchronous", type="boolean") |
||
| 80 | */ |
||
| 81 | protected $asynchronous = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="children") |
||
| 85 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 86 | */ |
||
| 87 | protected $parent; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | * |
||
| 92 | * @ORM\Column(name="position", type="string", nullable=true) |
||
| 93 | */ |
||
| 94 | protected $position; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Collection |
||
| 98 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="parent") |
||
| 99 | */ |
||
| 100 | protected $children; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | * |
||
| 105 | * @ORM\Column(name="slot", type="string", length=255, nullable=true) |
||
| 106 | */ |
||
| 107 | protected $slot; |
||
| 108 | |||
| 109 | public function __construct() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | public function getId() |
||
| 122 | |||
| 123 | public function setId($id) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function isAsynchronous() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param bool|string $asynchronous |
||
| 138 | */ |
||
| 139 | public function setAsynchronous($asynchronous) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set the action. |
||
| 146 | * |
||
| 147 | * @param string $action |
||
| 148 | * |
||
| 149 | * @throws \Exception The action is not valid |
||
| 150 | */ |
||
| 151 | public function setAction($action) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the action. |
||
| 163 | * |
||
| 164 | * @return string The action |
||
| 165 | */ |
||
| 166 | public function getAction() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return [Widget] |
||
| 173 | */ |
||
| 174 | public function getWidgets() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param Widget $widget |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function addWidget(Widget $widget) |
||
| 190 | /** |
||
| 191 | * @param [Widget] $widgets |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function setWidgets($widgets) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return View |
||
| 204 | */ |
||
| 205 | public function getView() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param View $view |
||
| 212 | */ |
||
| 213 | public function setView(View $view) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | public function getReplaced() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param mixed $replaced |
||
| 228 | */ |
||
| 229 | public function setReplaced($replaced) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getSlot() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $slot |
||
| 247 | */ |
||
| 248 | public function setSlot($slot) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | public function getChildren(View $view = null) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return Collection |
||
| 315 | */ |
||
| 316 | public function getChildrenRaw() |
||
| 320 | |||
| 321 | public function hasChild($position, View $view = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return WidgetMap|null |
||
| 334 | */ |
||
| 335 | public function getChild($position) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return [WidgetMap] |
||
| 349 | */ |
||
| 350 | public function getChilds($position) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param mixed $children |
||
| 364 | */ |
||
| 365 | public function setChildren($children) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function removeChildren() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param WidgetMap $child |
||
| 382 | */ |
||
| 383 | public function addChild($child) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param WidgetMap $child |
||
| 390 | */ |
||
| 391 | public function removeChild($child) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return WidgetMap|null |
||
| 398 | */ |
||
| 399 | public function getParent() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param null|WidgetMap $parent |
||
| 406 | */ |
||
| 407 | public function setParent(WidgetMap $parent = null) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getPosition() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $position |
||
| 428 | */ |
||
| 429 | public function setPosition($position) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return ArrayCollection |
||
| 436 | */ |
||
| 437 | public function getSubstitutes() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return mixed |
||
| 444 | */ |
||
| 445 | public function getSubstituteForView(View $view) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param WidgetMap $substitute |
||
| 456 | */ |
||
| 457 | public function addSubstitute(WidgetMap $substitute) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param [WidgetMap] $substitutes |
||
| 464 | */ |
||
| 465 | public function setSubstitutes($substitutes) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @deprecated |
||
| 472 | * |
||
| 473 | * @return Widget |
||
| 474 | */ |
||
| 475 | public function getWidget() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @deprecated |
||
| 482 | * |
||
| 483 | * @param Widget $widget |
||
| 484 | * |
||
| 485 | * @return WidgetMap |
||
| 486 | */ |
||
| 487 | public function setWidget(Widget $widget = null) |
||
| 493 | } |
||
| 494 |
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.