Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Page extends Item |
||
| 22 | { |
||
| 23 | use VariableTrait; |
||
| 24 | |||
| 25 | const SLUGIFY_PATTERN = '/(^\/|[^a-z0-9\/]|-)+/'; |
||
| 26 | // https://regex101.com/r/tJWUrd/1 |
||
| 27 | const PREFIX_PATTERN = '^(.*?)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_|\.)(.*)$'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var SplFileInfo |
||
| 31 | */ |
||
| 32 | protected $file; |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $fileExtension; |
||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $filePath; |
||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $fileName; |
||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $fileId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $virtual = false; |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $nodeType; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $id; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $pathname; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $path; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $name; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $frontmatter; |
||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $body; |
||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $html; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Constructor. |
||
| 91 | * |
||
| 92 | * @param null|SplFileInfo $file |
||
| 93 | */ |
||
| 94 | public function __construct(SplFileInfo $file = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return matches array if prefix exist or false. |
||
| 159 | * |
||
| 160 | * @param $string |
||
| 161 | * |
||
| 162 | * @return string[]|false |
||
| 163 | */ |
||
| 164 | public static function asPrefix($string) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return prefix if prefix or false. |
||
| 175 | * |
||
| 176 | * @param $string |
||
| 177 | * |
||
| 178 | * @return string[]|false |
||
|
|
|||
| 179 | */ |
||
| 180 | View Code Duplication | public static function getPrefix($string) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Return string without prefix (if exist). |
||
| 191 | * |
||
| 192 | * @param $string |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | View Code Duplication | public static function subPrefix($string) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Format string into URL. |
||
| 207 | * |
||
| 208 | * @param $string |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public static function urlize($string) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Is current page is virtual? |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isVirtual() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set node type. |
||
| 231 | * |
||
| 232 | * @param string $nodeType |
||
| 233 | * |
||
| 234 | * @return self |
||
| 235 | */ |
||
| 236 | public function setNodeType($nodeType) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get node type. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getNodeType() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Parse file content. |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function parse() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set name. |
||
| 270 | * |
||
| 271 | * @param $name |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function setName($name) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get name. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getName() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set path. |
||
| 294 | * |
||
| 295 | * @param $path |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setPath($path) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get path. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getPath() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set path name. |
||
| 318 | * |
||
| 319 | * @param string $pathname |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function setPathname($pathname) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get path name. |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getPathname() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set section. |
||
| 342 | * |
||
| 343 | * @param $section |
||
| 344 | * |
||
| 345 | * @return $this |
||
| 346 | */ |
||
| 347 | public function setSection($section) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get section. |
||
| 356 | * |
||
| 357 | * @return mixed|false |
||
| 358 | */ |
||
| 359 | public function getSection() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set title. |
||
| 370 | * |
||
| 371 | * @param $title |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function setTitle($title) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get title. |
||
| 384 | * |
||
| 385 | * @return mixed|false |
||
| 386 | */ |
||
| 387 | public function getTitle() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set date. |
||
| 394 | * |
||
| 395 | * @param $date |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function setDate($date) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get Date. |
||
| 408 | * |
||
| 409 | * @return \DateTime|false |
||
| 410 | */ |
||
| 411 | public function getDate() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set weight. |
||
| 418 | * |
||
| 419 | * @param $int |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function setWeight($int) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get weight. |
||
| 432 | * |
||
| 433 | * @return int |
||
| 434 | */ |
||
| 435 | public function getWeight() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set permalink. |
||
| 442 | * |
||
| 443 | * @param $permalink |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | public function setPermalink($permalink) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get permalink. |
||
| 456 | * |
||
| 457 | * @return mixed|false |
||
| 458 | */ |
||
| 459 | public function getPermalink() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get frontmatter. |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getFrontmatter() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get body. |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getBody() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Set HTML. |
||
| 490 | * |
||
| 491 | * @param string $html |
||
| 492 | * |
||
| 493 | * @return $this |
||
| 494 | */ |
||
| 495 | public function setHtml($html) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get HTML alias. |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getContent() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Set layout. |
||
| 514 | * |
||
| 515 | * @param $layout |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function setLayout($layout) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get layout. |
||
| 528 | * |
||
| 529 | * @return mixed|false |
||
| 530 | */ |
||
| 531 | public function getLayout() |
||
| 535 | } |
||
| 536 |
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.