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 Widget 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 Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Widget extends BaseWidget implements VictoireQueryInterface |
||
| 25 | { |
||
| 26 | use StyleTrait; |
||
| 27 | use QueryTrait; |
||
| 28 | use TimestampableEntity; |
||
| 29 | |||
| 30 | public function __construct() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | * |
||
| 39 | * @ORM\Column(name="id", type="integer") |
||
| 40 | * @ORM\Id |
||
| 41 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 42 | */ |
||
| 43 | protected $id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | * |
||
| 48 | * @ORM\Column(name="slot", type="string", length=255, nullable=true) |
||
| 49 | */ |
||
| 50 | protected $slot; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | * |
||
| 55 | * @ORM\Column(name="childrenSlot", type="string", length=100, nullable=true) |
||
| 56 | */ |
||
| 57 | protected $childrenSlot; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | * |
||
| 62 | * @ORM\Column(name="theme", type="string", length=255, nullable=true) |
||
| 63 | */ |
||
| 64 | protected $theme; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | * |
||
| 69 | * @ORM\Column(name="asynchronous", type="boolean", nullable=true) |
||
| 70 | */ |
||
| 71 | protected $asynchronous; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | * |
||
| 76 | * @ORM\Column(name="fields", type="text") |
||
| 77 | */ |
||
| 78 | protected $fields = 'a:0:{}'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | * |
||
| 83 | * @ORM\Column(name="mode", type="string", length=255, nullable=false) |
||
| 84 | */ |
||
| 85 | protected $mode = self::MODE_STATIC; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Auto simple mode: joined entity. |
||
| 89 | * |
||
| 90 | * @var EntityProxy |
||
| 91 | * |
||
| 92 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\EntityProxy", inversedBy="widgets", cascade={"persist"}) |
||
| 93 | * @ORM\JoinColumn(name="entityProxy_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 94 | */ |
||
| 95 | protected $entityProxy; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The entity linked to the widget. |
||
| 99 | * |
||
| 100 | * @var unknown |
||
| 101 | */ |
||
| 102 | protected $entity; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @deprecated Remove Doctrine mapping and property |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | * |
||
| 109 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", inversedBy="widgets", cascade={"persist"}) |
||
| 110 | * @ORM\JoinColumn(name="view_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 111 | */ |
||
| 112 | protected $view; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var WidgetMap |
||
| 116 | * |
||
| 117 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="widgets") |
||
| 118 | * @ORM\JoinColumn(name="widget_map_id", referencedColumnName="id", onDelete="SET NULL")) |
||
| 119 | */ |
||
| 120 | protected $widgetMap; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var [Criteria] |
||
| 124 | * |
||
| 125 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\CriteriaBundle\Entity\Criteria", mappedBy="widget", cascade={"persist", "remove"}, orphanRemoval=true) |
||
| 126 | */ |
||
| 127 | protected $criterias; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | * |
||
| 132 | * @ORM\Column(name="quantum", type="string", length=255, nullable=true) |
||
| 133 | */ |
||
| 134 | protected $quantum; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function isAsynchronous() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $asynchronous |
||
| 146 | */ |
||
| 147 | public function setAsynchronous($asynchronous) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the entity proxy. |
||
| 154 | * |
||
| 155 | * @param EntityProxy $entityProxy |
||
| 156 | */ |
||
| 157 | public function setEntityProxy(EntityProxy $entityProxy) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the entity proxy. |
||
| 164 | * |
||
| 165 | * @return EntityProxy |
||
| 166 | */ |
||
| 167 | public function getEntityProxy() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get businessEntityName. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getBusinessEntityName() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * to string. |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function __toString() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get id. |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | public function getId() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set fields. |
||
| 208 | * |
||
| 209 | * @param string $fields |
||
| 210 | * |
||
| 211 | * @return Widget |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function setFields($fields) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get fields. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getFields() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set The Id. |
||
| 237 | * |
||
| 238 | * @param int $id The id |
||
| 239 | */ |
||
| 240 | public function setId($id) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set slot. |
||
| 247 | * |
||
| 248 | * @param string $slot |
||
| 249 | * |
||
| 250 | * @return Widget |
||
| 251 | */ |
||
| 252 | public function setSlot($slot) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get slot. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function getSlot() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set theme. |
||
| 271 | * |
||
| 272 | * @param string $theme |
||
| 273 | * |
||
| 274 | * @return Widget |
||
| 275 | */ |
||
| 276 | public function setTheme($theme) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get theme. |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getTheme() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get the type of the object. |
||
| 295 | * |
||
| 296 | * @return string The type |
||
| 297 | */ |
||
| 298 | public function getType() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Guess the type of this by exploding and getting the last item. |
||
| 305 | * |
||
| 306 | * @return string The guessed type |
||
| 307 | */ |
||
| 308 | protected function guessType() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the mode. |
||
| 317 | * |
||
| 318 | * @param string $mode |
||
| 319 | */ |
||
| 320 | public function setMode($mode) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the mode. |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getMode() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the view id. |
||
| 337 | * |
||
| 338 | * @return int The view id |
||
| 339 | */ |
||
| 340 | public function getViewId() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function getChildrenSlot() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $childrenSlot |
||
| 363 | */ |
||
| 364 | public function setChildrenSlot($childrenSlot) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set widgets. |
||
| 371 | * |
||
| 372 | * @param [WidgetMap] $widgetMap |
||
| 373 | * |
||
| 374 | * @return Widget |
||
| 375 | */ |
||
| 376 | public function setWidgetMap(WidgetMap $widgetMap = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get widgets. |
||
| 388 | * |
||
| 389 | * @return [WidgetMap] |
||
| 390 | */ |
||
| 391 | public function getWidgetMap() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set the entity. |
||
| 398 | * |
||
| 399 | * @param object $entity |
||
| 400 | */ |
||
| 401 | public function setEntity($entity) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the entity. |
||
| 408 | * |
||
| 409 | * @return number |
||
| 410 | */ |
||
| 411 | public function getEntity() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Clone a widget. |
||
| 429 | */ |
||
| 430 | public function __clone() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @deprecated |
||
| 449 | * Get view |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getView() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return [Criteria] |
||
| 460 | */ |
||
| 461 | public function getCriterias() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param [Criteria] $criterias |
||
| 468 | */ |
||
| 469 | public function setCriterias($criterias) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param Criteria $criteria |
||
| 476 | */ |
||
| 477 | public function addCriteria($criteria) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param Criteria $criteria |
||
| 485 | */ |
||
| 486 | public function removeCriteria(Criteria $criteria) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param Criteria $criteria |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | public function hasCriteria(Criteria $criteria) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $criteriaAlias |
||
| 504 | * |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | public function hasCriteriaNamed($criteriaAlias) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getQuantum() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $quantum |
||
| 524 | */ |
||
| 525 | public function setQuantum($quantum) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Generate the CacheId, insert params that can invalid the cache. |
||
| 532 | * |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function generateCacheId() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * This method parse the widget criterias to discover in which locale this widget is used. |
||
| 552 | * |
||
| 553 | * @param $defaultLocale |
||
| 554 | * |
||
| 555 | * @return string|null |
||
| 556 | */ |
||
| 557 | public function guessLocale() |
||
| 567 | } |
||
| 568 |