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() |
||
| 31 | { |
||
| 32 | $this->childrenSlot = uniqid(); |
||
| 33 | $this->criterias = new ArrayCollection(); |
||
| 34 | } |
||
| 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 |
||
| 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 | * @var WidgetMap |
||
| 115 | * |
||
| 116 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="widgets") |
||
| 117 | * @ORM\JoinColumn(name="widget_map_id", referencedColumnName="id", onDelete="SET NULL")) |
||
| 118 | */ |
||
| 119 | protected $widgetMap; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var [Criteria] |
||
| 123 | * |
||
| 124 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\CriteriaBundle\Entity\Criteria", mappedBy="widget", cascade={"persist", "remove"}, orphanRemoval=true) |
||
| 125 | */ |
||
| 126 | protected $criterias; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | * |
||
| 131 | * @ORM\Column(name="quantum", type="string", length=255, nullable=true) |
||
| 132 | */ |
||
| 133 | protected $quantum; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function isAsynchronous() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $asynchronous |
||
| 145 | */ |
||
| 146 | public function setAsynchronous($asynchronous) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set the entity proxy. |
||
| 153 | * |
||
| 154 | * @param BaseEntityProxy $entityProxy |
||
| 155 | */ |
||
| 156 | public function setEntityProxy(BaseEntityProxy $entityProxy) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the entity proxy. |
||
| 163 | * |
||
| 164 | * @return EntityProxy |
||
| 165 | */ |
||
| 166 | public function getEntityProxy() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * to string. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function __toString() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get id. |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | public function getId() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set fields. |
||
| 193 | * |
||
| 194 | * @param string $fields |
||
| 195 | * |
||
| 196 | * @return Widget |
||
| 197 | */ |
||
| 198 | public function setFields($fields) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get fields. |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getFields() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set The Id. |
||
| 222 | * |
||
| 223 | * @param int $id The id |
||
| 224 | */ |
||
| 225 | public function setId($id) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set slot. |
||
| 232 | * |
||
| 233 | * @param string $slot |
||
| 234 | * |
||
| 235 | * @return Widget |
||
| 236 | */ |
||
| 237 | public function setSlot($slot) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get slot. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getSlot() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set theme. |
||
| 256 | * |
||
| 257 | * @param string $theme |
||
| 258 | * |
||
| 259 | * @return Widget |
||
| 260 | */ |
||
| 261 | public function setTheme($theme) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get theme. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getTheme() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the content. |
||
| 280 | * |
||
| 281 | * @return unknown |
||
| 282 | */ |
||
| 283 | public function getValue() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the type of the object. |
||
| 290 | * |
||
| 291 | * @return string The type |
||
| 292 | */ |
||
| 293 | public function getType() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Guess the type of this by exploding and getting the last item. |
||
| 300 | * |
||
| 301 | * @return string The guessed type |
||
| 302 | */ |
||
| 303 | protected function guessType() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set the mode. |
||
| 312 | * |
||
| 313 | * @param string $mode |
||
| 314 | */ |
||
| 315 | public function setMode($mode) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the mode. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getMode() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the view id. |
||
| 332 | * |
||
| 333 | * @return int The view id |
||
| 334 | */ |
||
| 335 | public function getViewId() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getChildrenSlot() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $childrenSlot |
||
| 358 | */ |
||
| 359 | public function setChildrenSlot($childrenSlot) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set widgets. |
||
| 366 | * |
||
| 367 | * @param [WidgetMap] $widgetMaps |
||
| 368 | * |
||
| 369 | * @return Widget |
||
| 370 | */ |
||
| 371 | public function setWidgetMap(WidgetMap $widgetMap) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get widgets. |
||
| 381 | * |
||
| 382 | * @return [WidgetMap] |
||
| 383 | */ |
||
| 384 | public function getWidgetMap() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set the entity. |
||
| 391 | * |
||
| 392 | * @param object $entity |
||
| 393 | */ |
||
| 394 | public function setEntity($entity) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the entity. |
||
| 401 | * |
||
| 402 | * @return number |
||
| 403 | */ |
||
| 404 | public function getEntity() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Clone a widget. |
||
| 423 | */ |
||
| 424 | public function __clone() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @deprecated |
||
| 443 | * Get view. |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getView() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return [Criteria] |
||
| 454 | */ |
||
| 455 | public function getCriterias() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param [Criteria] $criterias |
||
| 462 | */ |
||
| 463 | public function setCriterias($criterias) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param Criteria $criteria |
||
| 470 | */ |
||
| 471 | public function addCriteria($criteria) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param Criteria $criteria |
||
| 479 | */ |
||
| 480 | public function removeCriteria(Criteria $criteria) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param Criteria $criteria |
||
| 488 | * |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function hasCriteria(Criteria $criteria) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param $criteriaAlias |
||
| 498 | * |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | public function hasCriteriaNamed($criteriaAlias) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getQuantum() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $quantum |
||
| 518 | */ |
||
| 519 | public function setQuantum($quantum) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Generate the CacheId, insert params that can invalid the cache. |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function generateCacheId() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param $defaultLocale |
||
| 552 | * |
||
| 553 | * @return mixed |
||
| 554 | */ |
||
| 555 | public function getLocale($defaultLocale) |
||
| 567 | } |
||
| 568 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..