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="array") |
||
| 77 | */ |
||
| 78 | protected $fields = []; |
||
| 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) |
||
| 157 | { |
||
| 158 | $this->entityProxy = $entityProxy; |
||
|
|
|||
| 159 | } |
||
| 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) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get fields. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getFields() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set The Id. |
||
| 217 | * |
||
| 218 | * @param int $id The id |
||
| 219 | */ |
||
| 220 | public function setId($id) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set slot. |
||
| 227 | * |
||
| 228 | * @param string $slot |
||
| 229 | * |
||
| 230 | * @return Widget |
||
| 231 | */ |
||
| 232 | public function setSlot($slot) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get slot. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getSlot() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set theme. |
||
| 251 | * |
||
| 252 | * @param string $theme |
||
| 253 | * |
||
| 254 | * @return Widget |
||
| 255 | */ |
||
| 256 | public function setTheme($theme) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get theme. |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getTheme() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the content. |
||
| 275 | * |
||
| 276 | * @return unknown |
||
| 277 | */ |
||
| 278 | public function getValue() |
||
| 279 | { |
||
| 280 | //return $this->getContent(); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the type of the object. |
||
| 285 | * |
||
| 286 | * @return string The type |
||
| 287 | */ |
||
| 288 | public function getType() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Guess the type of this by exploding and getting the last item. |
||
| 295 | * |
||
| 296 | * @return string The guessed type |
||
| 297 | */ |
||
| 298 | protected function guessType() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Set the mode. |
||
| 307 | * |
||
| 308 | * @param string $mode |
||
| 309 | */ |
||
| 310 | public function setMode($mode) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the mode. |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function getMode() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the view id. |
||
| 327 | * |
||
| 328 | * @return int The view id |
||
| 329 | */ |
||
| 330 | public function getViewId() |
||
| 331 | { |
||
| 332 | $viewId = null; |
||
| 333 | |||
| 334 | $view = $this->getView(); |
||
| 335 | |||
| 336 | if ($view !== null) { |
||
| 337 | $viewId = $view->getId(); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $viewId; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getChildrenSlot() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $childrenSlot |
||
| 353 | */ |
||
| 354 | public function setChildrenSlot($childrenSlot) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set widgets. |
||
| 361 | * |
||
| 362 | * @param [WidgetMap] $widgetMaps |
||
| 363 | * |
||
| 364 | * @return Widget |
||
| 365 | */ |
||
| 366 | public function setWidgetMap(WidgetMap $widgetMap) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get widgets. |
||
| 376 | * |
||
| 377 | * @return [WidgetMap] |
||
| 378 | */ |
||
| 379 | public function getWidgetMap() |
||
| 380 | { |
||
| 381 | return $this->widgetMap; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set the entity. |
||
| 386 | * |
||
| 387 | * @param object $entity |
||
| 388 | */ |
||
| 389 | public function setEntity($entity) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the entity. |
||
| 396 | * |
||
| 397 | * @return number |
||
| 398 | */ |
||
| 399 | public function getEntity() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Clone a widget. |
||
| 418 | */ |
||
| 419 | public function __clone() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @deprecated |
||
| 438 | * Get view. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getView() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return [Criteria] |
||
| 449 | */ |
||
| 450 | public function getCriterias() |
||
| 451 | { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param [Criteria] $criterias |
||
| 457 | */ |
||
| 458 | public function setCriterias($criterias) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param Criteria $criteria |
||
| 465 | */ |
||
| 466 | public function addCriteria($criteria) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param Criteria $criteria |
||
| 474 | */ |
||
| 475 | public function removeCriteria(Criteria $criteria) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param Criteria $criteria |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function hasCriteria(Criteria $criteria) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param $criteriaAlias |
||
| 493 | * |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | public function hasCriteriaNamed($criteriaAlias) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function getQuantum() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $quantum |
||
| 513 | */ |
||
| 514 | public function setQuantum($quantum) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Generate the CacheId, insert params that can invalid the cache. |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public function generateCacheId() |
||
| 544 | } |
||
| 545 |
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..