Complex classes like ArticleDocument 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 ArticleDocument, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class ArticleDocument implements |
||
| 40 | UuidBehavior, |
||
| 41 | NodeNameBehavior, |
||
| 42 | AutoNameBehavior, |
||
| 43 | PathBehavior, |
||
| 44 | LocalizedTitleBehavior, |
||
| 45 | StructureBehavior, |
||
| 46 | LocalizedStructureBehavior, |
||
| 47 | LocalizedAuditableBehavior, |
||
| 48 | DateShardingBehavior, |
||
| 49 | RoutableBehavior, |
||
| 50 | ExtensionBehavior, |
||
| 51 | WorkflowStageBehavior, |
||
| 52 | VersionBehavior, |
||
| 53 | AuthorBehavior, |
||
| 54 | ChildrenBehavior, |
||
| 55 | ArticleInterface |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $uuid; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $nodeName; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $path; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var object |
||
| 74 | */ |
||
| 75 | private $parent; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $title; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $pageTitle; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $pages; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var RouteInterface |
||
| 94 | */ |
||
| 95 | private $route; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | private $routePath; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $locale; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | private $originalLocale; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | private $structureType; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var StructureInterface |
||
| 119 | */ |
||
| 120 | private $structure; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var int |
||
| 124 | */ |
||
| 125 | private $creator; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var int |
||
| 129 | */ |
||
| 130 | private $changer; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var \DateTime |
||
| 134 | */ |
||
| 135 | private $created; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var \DateTime |
||
| 139 | */ |
||
| 140 | private $changed; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var int |
||
| 144 | */ |
||
| 145 | private $author; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var \DateTime |
||
| 149 | */ |
||
| 150 | private $authored; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Document's extensions ie seo, ... |
||
| 154 | * |
||
| 155 | * @var ExtensionContainer |
||
| 156 | */ |
||
| 157 | protected $extensions; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Workflow Stage currently Test or Published. |
||
| 161 | * |
||
| 162 | * @var int |
||
| 163 | */ |
||
| 164 | protected $workflowStage; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Is Document is published. |
||
| 168 | * |
||
| 169 | * @var bool |
||
| 170 | */ |
||
| 171 | protected $published; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * List of versions. |
||
| 175 | * |
||
| 176 | * @var Version[] |
||
| 177 | */ |
||
| 178 | protected $versions = []; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var ChildrenCollection |
||
| 182 | */ |
||
| 183 | protected $children; |
||
| 184 | |||
| 185 | 44 | public function __construct() |
|
| 186 | { |
||
| 187 | 44 | $this->structure = new Structure(); |
|
| 188 | 44 | $this->extensions = new ExtensionContainer(); |
|
| 189 | 44 | $this->children = new \ArrayIterator(); |
|
|
|
|||
| 190 | 44 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | 44 | public function getUuid() |
|
| 196 | { |
||
| 197 | 44 | return $this->uuid; |
|
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | 44 | public function setUuid($uuid) |
|
| 204 | { |
||
| 205 | 44 | $this->uuid = $uuid; |
|
| 206 | 44 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function getNodeName() |
||
| 212 | { |
||
| 213 | return $this->nodeName; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | 29 | public function getPath() |
|
| 220 | { |
||
| 221 | 29 | return $this->path; |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 44 | public function getParent() |
|
| 228 | { |
||
| 229 | 44 | return $this->parent; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | 44 | public function setParent($document) |
|
| 236 | { |
||
| 237 | 44 | $this->parent = $document; |
|
| 238 | 44 | } |
|
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | 44 | public function getTitle() |
|
| 244 | { |
||
| 245 | 44 | return $this->title; |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | 44 | public function setTitle($title) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns route. |
||
| 258 | * |
||
| 259 | * @return RouteInterface |
||
| 260 | */ |
||
| 261 | 11 | public function getRoute() |
|
| 262 | { |
||
| 263 | 11 | return $this->route; |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | 11 | public function setRoute(RouteInterface $route) |
|
| 270 | { |
||
| 271 | 11 | $this->route = $route; |
|
| 272 | 11 | } |
|
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function removeRoute() |
||
| 278 | { |
||
| 279 | $this->route = null; |
||
| 280 | $this->routePath = null; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | 44 | public function getRoutePath() |
|
| 287 | { |
||
| 288 | 44 | return $this->routePath; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | 44 | public function setRoutePath($routePath) |
|
| 295 | { |
||
| 296 | 44 | $this->routePath = $routePath; |
|
| 297 | 44 | } |
|
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | 44 | public function getClass() |
|
| 303 | { |
||
| 304 | 44 | return self::class; |
|
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | 44 | public function getLocale() |
|
| 311 | { |
||
| 312 | 44 | return $this->locale; |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | 44 | public function setLocale($locale) |
|
| 319 | { |
||
| 320 | 44 | $this->locale = $locale; |
|
| 321 | 44 | } |
|
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | 44 | public function getOriginalLocale() |
|
| 327 | { |
||
| 328 | 44 | return $this->originalLocale; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | 44 | public function setOriginalLocale($locale) |
|
| 335 | { |
||
| 336 | 44 | $this->originalLocale = $locale; |
|
| 337 | 44 | } |
|
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | 44 | public function getStructureType() |
|
| 343 | { |
||
| 344 | 44 | return $this->structureType; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | 44 | public function setStructureType($structureType) |
|
| 351 | { |
||
| 352 | 44 | $this->structureType = $structureType; |
|
| 353 | 44 | } |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | 44 | public function getStructure() |
|
| 359 | { |
||
| 360 | 44 | return $this->structure; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | 44 | public function getCreator() |
|
| 367 | { |
||
| 368 | 44 | return $this->creator; |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * {@inheritdoc} |
||
| 373 | */ |
||
| 374 | 44 | public function getChanger() |
|
| 375 | { |
||
| 376 | 44 | return $this->changer; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | 44 | public function getCreated() |
|
| 383 | { |
||
| 384 | 44 | return $this->created; |
|
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc} |
||
| 389 | */ |
||
| 390 | 44 | public function getChanged() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Returns identifier. |
||
| 397 | * |
||
| 398 | * @return mixed |
||
| 399 | */ |
||
| 400 | 44 | public function getId() |
|
| 401 | { |
||
| 402 | 44 | return $this->getUuid(); |
|
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritdoc} |
||
| 407 | */ |
||
| 408 | 44 | public function getExtensionsData() |
|
| 409 | { |
||
| 410 | 44 | return $this->extensions; |
|
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritdoc} |
||
| 415 | */ |
||
| 416 | 44 | public function setExtensionsData($extensions) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function setExtension($name, $data) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritdoc} |
||
| 431 | */ |
||
| 432 | 46 | public function getWorkflowStage() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | 44 | public function setWorkflowStage($workflowStage) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | 44 | public function getPublished() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | 44 | public function getAuthored() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | 44 | public function setAuthored($authored) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | 44 | public function getAuthor() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | 44 | public function setAuthor($author) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * {@inheritdoc} |
||
| 487 | */ |
||
| 488 | public function getVersions() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * {@inheritdoc} |
||
| 495 | */ |
||
| 496 | public function setVersions($versions) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * {@inheritdoc} |
||
| 503 | */ |
||
| 504 | 44 | public function getChildren() |
|
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc} |
||
| 511 | */ |
||
| 512 | public function getArticleUuid() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * {@inheritdoc} |
||
| 519 | */ |
||
| 520 | public function getPageUuid() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritdoc} |
||
| 527 | */ |
||
| 528 | 44 | public function getPageNumber() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * {@inheritdoc} |
||
| 535 | */ |
||
| 536 | 44 | public function getPageTitle() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Set pageTitle. |
||
| 543 | * |
||
| 544 | * @param string $pageTitle |
||
| 545 | * |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | 44 | public function setPageTitle($pageTitle) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Returns pages. |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | public function getPages() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Set pages. |
||
| 567 | * |
||
| 568 | * @param array $pages |
||
| 569 | * |
||
| 570 | * @return $this |
||
| 571 | */ |
||
| 572 | 44 | public function setPages($pages) |
|
| 578 | } |
||
| 579 |
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..