Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class View |
||
| 31 | { |
||
| 32 | use \Gedmo\Timestampable\Traits\TimestampableEntity; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | * |
||
| 37 | * @ORM\Column(name="id", type="integer") |
||
| 38 | * @ORM\Id |
||
| 39 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 40 | */ |
||
| 41 | protected $id; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | * |
||
| 46 | * @Assert\NotBlank() |
||
| 47 | * @ORM\Column(name="name", type="string", length=255) |
||
| 48 | * @Serializer\Groups({"search"}) |
||
| 49 | * @Gedmo\Translatable |
||
| 50 | */ |
||
| 51 | protected $name; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | * |
||
| 56 | * @ORM\Column(name="bodyId", type="string", length=255, nullable=true) |
||
| 57 | */ |
||
| 58 | protected $bodyId; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | * |
||
| 63 | * @ORM\Column(name="bodyClass", type="string", length=255, nullable=true) |
||
| 64 | */ |
||
| 65 | protected $bodyClass; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | * |
||
| 70 | * @Gedmo\Slug(handlers={ |
||
| 71 | * @Gedmo\SlugHandler(class="Victoire\Bundle\BusinessEntityBundle\Handler\TwigSlugHandler" |
||
| 72 | * )},fields={"name"}, updatable=false, unique=false) |
||
| 73 | * @Gedmo\Translatable |
||
| 74 | * @ORM\Column(name="slug", type="string", length=255) |
||
| 75 | */ |
||
| 76 | protected $slug; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var [WidgetMap] |
||
| 80 | * |
||
| 81 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", mappedBy="view", orphanRemoval=true, cascade={"persist", "remove"}) |
||
| 82 | */ |
||
| 83 | protected $widgetMaps = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @Gedmo\TreeParent |
||
| 87 | * @ORM\ManyToOne(targetEntity="View", inversedBy="children", cascade={"persist"}) |
||
| 88 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") |
||
| 89 | */ |
||
| 90 | protected $parent; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | * |
||
| 95 | * @ORM\Column(name="position", type="integer", nullable=false) |
||
| 96 | */ |
||
| 97 | protected $position = 0; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @Gedmo\TreeLeft |
||
| 101 | * @ORM\Column(name="lft", type="integer") |
||
| 102 | */ |
||
| 103 | protected $lft; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @Gedmo\TreeLevel |
||
| 107 | * @ORM\Column(name="lvl", type="integer") |
||
| 108 | */ |
||
| 109 | protected $lvl; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @Gedmo\TreeRight |
||
| 113 | * @ORM\Column(name="rgt", type="integer") |
||
| 114 | */ |
||
| 115 | protected $rgt; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @Gedmo\TreeRoot |
||
| 119 | * @ORM\Column(name="root", type="integer", nullable=true) |
||
| 120 | */ |
||
| 121 | protected $root; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @ORM\OneToMany(targetEntity="View", mappedBy="parent", cascade={"remove"}) |
||
| 125 | * @ORM\OrderBy({"lft" = "ASC"}) |
||
| 126 | */ |
||
| 127 | protected $children = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * This relation is dynamicly added by PageSubscriber. |
||
| 131 | */ |
||
| 132 | protected $author; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string |
||
| 136 | * |
||
| 137 | * @ORM\Column(name="undeletable", type="boolean") |
||
| 138 | */ |
||
| 139 | protected $undeletable = false; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var ViewReference[] |
||
| 143 | * The reference is related to viewsReferences.xml file which list all app views. |
||
| 144 | * This is used to speed up the routing system and identify virtual pages (BusinessPage). |
||
| 145 | */ |
||
| 146 | protected $references; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string |
||
| 150 | * |
||
| 151 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\TemplateBundle\Entity\Template", inversedBy="inheritors", cascade={"persist"}) |
||
| 152 | * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 153 | */ |
||
| 154 | protected $template; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var string |
||
| 158 | * |
||
| 159 | * @ORM\Column(name="cssHash", type="string", length=40 ,nullable=true) |
||
| 160 | */ |
||
| 161 | protected $cssHash; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @deprecated |
||
| 165 | * @ORM\Column(name="widget_map", type="array") |
||
| 166 | */ |
||
| 167 | protected $widgetMap = []; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var string |
||
| 171 | * |
||
| 172 | * @ORM\OneToMany(targetEntity="\Victoire\Bundle\WidgetBundle\Entity\Widget", mappedBy="view", cascade={"persist", "remove"}) |
||
| 173 | * @ORM\OrderBy({"id" = "ASC"}) |
||
| 174 | */ |
||
| 175 | protected $widgets; |
||
| 176 | /** |
||
| 177 | * @var bool |
||
| 178 | * |
||
| 179 | * @ORM\Column(name="cssUpToDate", type="boolean") |
||
| 180 | */ |
||
| 181 | protected $cssUpToDate = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @Gedmo\Locale |
||
| 185 | * Used locale to override Translation listener`s locale |
||
| 186 | * this is not a mapped field of entity metadata, just a simple property |
||
| 187 | * and it is not necessary because globally locale can be set in listener |
||
| 188 | */ |
||
| 189 | protected $locale; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @ORM\OneToMany( |
||
| 193 | * targetEntity="Victoire\Bundle\I18nBundle\Entity\ViewTranslation", |
||
| 194 | * mappedBy="object", |
||
| 195 | * cascade={"persist", "remove"} |
||
| 196 | * ) |
||
| 197 | */ |
||
| 198 | private $translations; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Construct. |
||
| 202 | **/ |
||
| 203 | public function __construct() |
||
| 204 | { |
||
| 205 | $this->children = new ArrayCollection(); |
||
|
|
|||
| 206 | $this->widgetMaps = new ArrayCollection(); |
||
| 207 | $this->translations = new ArrayCollection(); |
||
| 208 | $this->references = []; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * to string. |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | **/ |
||
| 216 | public function __toString() |
||
| 217 | { |
||
| 218 | return $this->getName(); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get id. |
||
| 223 | * |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getId() |
||
| 227 | { |
||
| 228 | return $this->id; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set id. |
||
| 233 | * |
||
| 234 | * @param id $id |
||
| 235 | */ |
||
| 236 | public function setId($id) |
||
| 237 | { |
||
| 238 | $this->id = $id; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get name. |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getName() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set name. |
||
| 253 | * |
||
| 254 | * @param string $name |
||
| 255 | * |
||
| 256 | * @return View |
||
| 257 | */ |
||
| 258 | public function setName($name) |
||
| 259 | { |
||
| 260 | $this->name = $name; |
||
| 261 | |||
| 262 | return $this; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set slug. |
||
| 267 | * |
||
| 268 | * @param string $slug |
||
| 269 | * |
||
| 270 | * @return View |
||
| 271 | */ |
||
| 272 | public function setSlug($slug) |
||
| 273 | { |
||
| 274 | $this->slug = $slug; |
||
| 275 | |||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get slug. |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getSlug() |
||
| 285 | { |
||
| 286 | return $this->slug; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set template. |
||
| 291 | * |
||
| 292 | * @param View $template |
||
| 293 | * |
||
| 294 | * @return View |
||
| 295 | */ |
||
| 296 | public function setTemplate($template) |
||
| 297 | { |
||
| 298 | $this->template = $template; |
||
| 299 | |||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get template. |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getTemplate() |
||
| 309 | { |
||
| 310 | return $this->template; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set parent. |
||
| 315 | * |
||
| 316 | * @param View $parent |
||
| 317 | */ |
||
| 318 | public function setParent(View $parent = null) |
||
| 319 | { |
||
| 320 | $this->parent = $parent; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get parent. |
||
| 325 | * |
||
| 326 | * @return View parent |
||
| 327 | */ |
||
| 328 | public function getParent() |
||
| 329 | { |
||
| 330 | return $this->parent; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set children. |
||
| 335 | * |
||
| 336 | * @param View[] $children |
||
| 337 | * |
||
| 338 | * @return View |
||
| 339 | */ |
||
| 340 | public function setChildren($children) |
||
| 341 | { |
||
| 342 | $this->children = $children; |
||
| 343 | if ($children !== null) { |
||
| 344 | foreach ($children as $child) { |
||
| 345 | $child->setParent($this); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | return $this; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get children. |
||
| 354 | * |
||
| 355 | * @return View[] |
||
| 356 | */ |
||
| 357 | public function getChildren() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Has children. |
||
| 364 | * |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | public function hasChildren() |
||
| 368 | { |
||
| 369 | return count($this->children); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get WebView children. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getWebViewChildren() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Add child. |
||
| 391 | * |
||
| 392 | * @param View $child |
||
| 393 | */ |
||
| 394 | public function addChild(View $child) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Remove child. |
||
| 401 | * |
||
| 402 | * @param View $child |
||
| 403 | */ |
||
| 404 | public function removeChild(View $child) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the left value. |
||
| 411 | * |
||
| 412 | * @return int |
||
| 413 | */ |
||
| 414 | public function getLft() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the left value. |
||
| 421 | * |
||
| 422 | * @param int $lft |
||
| 423 | */ |
||
| 424 | public function setLft($lft) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get the right value. |
||
| 431 | * |
||
| 432 | * @return int |
||
| 433 | */ |
||
| 434 | public function getRgt() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set the right value. |
||
| 441 | * |
||
| 442 | * @param int $rgt |
||
| 443 | */ |
||
| 444 | public function setRgt($rgt) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get the level value. |
||
| 451 | * |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function getLvl() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set the level value. |
||
| 461 | * |
||
| 462 | * @param int $lvl |
||
| 463 | */ |
||
| 464 | public function setLvl($lvl) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get the root value. |
||
| 471 | * |
||
| 472 | * @return int |
||
| 473 | */ |
||
| 474 | public function getRoot() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set the root value. |
||
| 481 | * |
||
| 482 | * @param int $root |
||
| 483 | */ |
||
| 484 | public function setRoot($root) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Set undeletable. |
||
| 491 | * |
||
| 492 | * @param bool $undeletable |
||
| 493 | * |
||
| 494 | * @return View The current instance |
||
| 495 | */ |
||
| 496 | public function setUndeletable($undeletable) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Is the widget is undeletable. |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function isUndeletable() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get author. |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getAuthor() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set author. |
||
| 525 | * |
||
| 526 | * @param string $author |
||
| 527 | * |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function setAuthor($author) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get bodyId. |
||
| 539 | * |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | public function getBodyId() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set bodyId. |
||
| 549 | * |
||
| 550 | * @param string $bodyId |
||
| 551 | * |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | public function setBodyId($bodyId) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get bodyClass. |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function getBodyClass() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Set bodyClass. |
||
| 573 | * |
||
| 574 | * @param string $bodyClass |
||
| 575 | * |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | public function setBodyClass($bodyClass) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Set widgets. |
||
| 587 | * |
||
| 588 | * @param [WidgetMap] $widgetMaps |
||
| 589 | * |
||
| 590 | * @return View |
||
| 591 | */ |
||
| 592 | public function setWidgetMaps($widgetMaps) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get widgets. |
||
| 601 | * |
||
| 602 | * @return Collection[WidgetMap] |
||
| 603 | */ |
||
| 604 | public function getWidgetMaps() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Add widget. |
||
| 611 | * |
||
| 612 | * @param Widget $widgetMap |
||
| 613 | */ |
||
| 614 | public function addWidgetMap(WidgetMap $widgetMap) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Remove a widgetMap. |
||
| 624 | * |
||
| 625 | * @param WidgetMap $widgetMap |
||
| 626 | */ |
||
| 627 | public function removeWidgetMap(WidgetMap $widgetMap) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Get widgets ids as array. |
||
| 634 | * |
||
| 635 | * @return array |
||
| 636 | */ |
||
| 637 | public function getWidgetsIds() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Get builtWidgetMap. |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | public function getBuiltWidgetMap() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Set builtWidgetMap. |
||
| 661 | * |
||
| 662 | * @param string $builtWidgetMap |
||
| 663 | * |
||
| 664 | * @return $this |
||
| 665 | */ |
||
| 666 | public function setBuiltWidgetMap($builtWidgetMap) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Get discriminator type. |
||
| 675 | * |
||
| 676 | * @return int |
||
| 677 | */ |
||
| 678 | public function getType() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Set position. |
||
| 687 | * |
||
| 688 | * @param int $position |
||
| 689 | */ |
||
| 690 | public function setPosition($position) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get position. |
||
| 697 | * |
||
| 698 | * @return int |
||
| 699 | */ |
||
| 700 | public function getPosition() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Get reference according to the current locale. |
||
| 707 | * |
||
| 708 | * @param string $locale |
||
| 709 | * |
||
| 710 | * @return null|ViewReference |
||
| 711 | */ |
||
| 712 | public function getReference($locale = null) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Get references. |
||
| 722 | * |
||
| 723 | * @return ViewReference[] |
||
| 724 | */ |
||
| 725 | public function getReferences() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Set references. |
||
| 732 | * |
||
| 733 | * @param ViewReference[] $references |
||
| 734 | * |
||
| 735 | * @return $this |
||
| 736 | */ |
||
| 737 | public function setReferences($references) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Set reference. |
||
| 746 | * |
||
| 747 | * @param ViewReference $reference |
||
| 748 | * @param string $locale |
||
| 749 | * |
||
| 750 | * @return $this |
||
| 751 | */ |
||
| 752 | public function setReference(ViewReference $reference, $locale = null) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Get CSS hash. |
||
| 762 | * |
||
| 763 | * @return string |
||
| 764 | */ |
||
| 765 | public function getCssHash() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Set CSS hash. |
||
| 772 | * |
||
| 773 | * @param string $cssHash |
||
| 774 | * |
||
| 775 | * @return $this |
||
| 776 | */ |
||
| 777 | public function setCssHash($cssHash) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Change cssHash. |
||
| 786 | */ |
||
| 787 | public function changeCssHash() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @deprecated |
||
| 794 | * Get widgetMap. |
||
| 795 | * |
||
| 796 | * @return widgetMap |
||
| 797 | */ |
||
| 798 | public function getWidgetMap() |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @deprecated |
||
| 805 | * Get widgets. |
||
| 806 | * |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | public function getWidgets() |
||
| 813 | |||
| 814 | public function isTemplateOf(View $view) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get cssUpToDate. |
||
| 828 | * |
||
| 829 | * @return bool |
||
| 830 | */ |
||
| 831 | public function isCssUpToDate() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Set CssUpToDate. |
||
| 838 | * |
||
| 839 | * @param bool $cssUpToDate |
||
| 840 | * |
||
| 841 | * @return $this |
||
| 842 | */ |
||
| 843 | public function setCssUpToDate($cssUpToDate) |
||
| 849 | |||
| 850 | public function setTranslatableLocale($locale) |
||
| 854 | |||
| 855 | public function setLocale($locale) |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @return string |
||
| 862 | */ |
||
| 863 | public function getLocale() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @return ViewTranslation[] |
||
| 870 | */ |
||
| 871 | public function getTranslations() |
||
| 875 | |||
| 876 | /** |
||
| 877 | * @return ViewTranslation[] |
||
| 878 | */ |
||
| 879 | public function getTranslationClass() |
||
| 883 | |||
| 884 | /** |
||
| 885 | * @param ViewTranslation $t |
||
| 886 | */ |
||
| 887 | public function addTranslation(ViewTranslation $t) |
||
| 894 | } |
||
| 895 |
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..