Complex classes like NodeTranslation 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 NodeTranslation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class NodeTranslation extends AbstractEntity |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Node |
||
| 27 | * |
||
| 28 | * @ORM\ManyToOne(targetEntity="Node", inversedBy="nodeTranslations") |
||
| 29 | * @ORM\JoinColumn(name="node_id", referencedColumnName="id") |
||
| 30 | */ |
||
| 31 | protected $node; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | * |
||
| 36 | * @ORM\Column(type="string") |
||
| 37 | */ |
||
| 38 | protected $lang; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | * |
||
| 43 | * @ORM\Column(type="boolean") |
||
| 44 | */ |
||
| 45 | protected $online = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | * |
||
| 50 | * @ORM\Column(type="string") |
||
| 51 | */ |
||
| 52 | protected $title; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | * |
||
| 57 | * @ORM\Column(type="string", nullable=true) |
||
| 58 | * @Assert\Regex("/^[a-zA-Z0-9\-_\/]+$/") |
||
| 59 | */ |
||
| 60 | protected $slug; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | * |
||
| 65 | * @ORM\Column(type="string", nullable=true) |
||
| 66 | */ |
||
| 67 | protected $url; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var NodeVersion |
||
| 71 | * |
||
| 72 | * @ORM\ManyToOne(targetEntity="NodeVersion") |
||
| 73 | * @ORM\JoinColumn(name="public_node_version_id", referencedColumnName="id") |
||
| 74 | */ |
||
| 75 | protected $publicNodeVersion; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var ArrayCollection |
||
| 79 | * @Assert\Valid() |
||
| 80 | * @ORM\OneToMany(targetEntity="NodeVersion", mappedBy="nodeTranslation") |
||
| 81 | * @ORM\OrderBy({"created" = "ASC"}) |
||
| 82 | */ |
||
| 83 | protected $nodeVersions; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int |
||
| 87 | * |
||
| 88 | * @ORM\Column(type="smallint", nullable=true) |
||
| 89 | */ |
||
| 90 | protected $weight; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var \DateTime |
||
| 94 | * |
||
| 95 | * @ORM\Column(type="datetime", nullable=true) |
||
| 96 | */ |
||
| 97 | protected $created; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \DateTime |
||
| 101 | * |
||
| 102 | * @ORM\Column(type="datetime", nullable=true) |
||
| 103 | */ |
||
| 104 | protected $updated; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * contructor |
||
| 108 | */ |
||
| 109 | 59 | public function __construct() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Set node |
||
| 118 | * |
||
| 119 | * @param Node $node |
||
| 120 | * |
||
| 121 | * @return NodeTranslation |
||
| 122 | */ |
||
| 123 | 30 | public function setNode($node) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get Node |
||
| 132 | * |
||
| 133 | * @return Node |
||
| 134 | */ |
||
| 135 | 14 | public function getNode() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set lang |
||
| 142 | * |
||
| 143 | * @param string $lang |
||
| 144 | * |
||
| 145 | * @return NodeTranslation |
||
| 146 | */ |
||
| 147 | 17 | public function setLang($lang) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get lang |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 13 | public function getLang() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Is online |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | 8 | public function isOnline() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Set online |
||
| 176 | * |
||
| 177 | * @param bool $online |
||
| 178 | * |
||
| 179 | * @return NodeTranslation |
||
| 180 | */ |
||
| 181 | 13 | public function setOnline($online) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Set title |
||
| 190 | * |
||
| 191 | * @param string $title |
||
| 192 | * |
||
| 193 | * @return NodeTranslation |
||
| 194 | */ |
||
| 195 | 10 | public function setTitle($title) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get title |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 3 | public function getTitle() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Set slug |
||
| 214 | * |
||
| 215 | * @param string $slug |
||
| 216 | * |
||
| 217 | * @return NodeTranslation |
||
| 218 | */ |
||
| 219 | 3 | public function setSlug($slug) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get slug |
||
| 228 | * |
||
| 229 | * @return string |
||
|
|
|||
| 230 | */ |
||
| 231 | 2 | public function getFullSlug() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | 2 | public function getSlugPart() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get slug |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | 3 | public function getSlug() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param NodeVersion $publicNodeVersion |
||
| 276 | * |
||
| 277 | * @return NodeTranslation |
||
| 278 | */ |
||
| 279 | 6 | public function setPublicNodeVersion(NodeVersion $publicNodeVersion) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @return NodeVersion |
||
| 288 | */ |
||
| 289 | 11 | public function getPublicNodeVersion() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @return NodeVersion |
||
| 296 | */ |
||
| 297 | 1 | public function getDraftNodeVersion() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @return ArrayCollection |
||
| 304 | */ |
||
| 305 | 4 | public function getNodeVersions() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param ArrayCollection $nodeVersions |
||
| 312 | * |
||
| 313 | * @return NodeTranslation |
||
| 314 | */ |
||
| 315 | 2 | public function setNodeVersions(ArrayCollection $nodeVersions) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $type |
||
| 324 | * |
||
| 325 | * @return NodeVersion|null |
||
| 326 | */ |
||
| 327 | 4 | public function getNodeVersion($type) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Add nodeVersion |
||
| 350 | * |
||
| 351 | * @param NodeVersion $nodeVersion |
||
| 352 | * |
||
| 353 | * @return NodeTranslation |
||
| 354 | */ |
||
| 355 | 11 | public function addNodeVersion(NodeVersion $nodeVersion) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | 1 | public function getDefaultAdminType() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param EntityManagerInterface $em The entity manager |
||
| 377 | * @param string $type The type |
||
| 378 | * |
||
| 379 | * @return object|null |
||
| 380 | */ |
||
| 381 | 1 | public function getRef(EntityManagerInterface $em, $type = 'public') |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param EntityManagerInterface $em The entity manager |
||
| 394 | * @param NodeVersion $nodeVersion |
||
| 395 | * |
||
| 396 | 2 | * @return object|null |
|
| 397 | */ |
||
| 398 | 2 | public function getRefByNodeVersion(EntityManagerInterface $em, NodeVersion $nodeVersion) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $url |
||
| 405 | * |
||
| 406 | 7 | * @return NodeTranslation |
|
| 407 | */ |
||
| 408 | 7 | public function setUrl($url) |
|
| 414 | |||
| 415 | /** |
||
| 416 | 2 | * @return string |
|
| 417 | */ |
||
| 418 | 2 | public function getUrl() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $weight |
||
| 425 | * |
||
| 426 | 2 | * @return NodeTranslation |
|
| 427 | */ |
||
| 428 | 2 | public function setWeight($weight) |
|
| 434 | 1 | ||
| 435 | /** |
||
| 436 | 1 | * @return int |
|
| 437 | */ |
||
| 438 | public function getWeight() |
||
| 442 | |||
| 443 | /** |
||
| 444 | 59 | * @return \DateTime |
|
| 445 | */ |
||
| 446 | 59 | public function getCreated() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param \DateTime $created |
||
| 453 | * |
||
| 454 | 1 | * @return NodeTranslation |
|
| 455 | */ |
||
| 456 | 1 | public function setCreated($created) |
|
| 462 | |||
| 463 | /** |
||
| 464 | 59 | * @return \DateTime |
|
| 465 | */ |
||
| 466 | 59 | public function getUpdated() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param \DateTime $updated |
||
| 473 | * |
||
| 474 | * @return NodeTranslation |
||
| 475 | */ |
||
| 476 | public function setUpdated($updated) |
||
| 482 | } |
||
| 483 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.