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 FrontMatterObject 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 FrontMatterObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class FrontMatterObject |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 16 | * |
||
| 17 | * $dataDependencies['collections'] = array() |
||
| 18 | * $dataDependencies['data'] = array() |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $dataDependencies; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 26 | * precedence over values in $frontMatter |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $writableFrontMatter; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 34 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 35 | * instead. |
||
| 36 | * |
||
| 37 | * @var string[] |
||
| 38 | */ |
||
| 39 | protected $frontMatterBlacklist; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $frontMatterEvaluated; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var FrontMatterParser |
||
| 50 | */ |
||
| 51 | protected $frontMatterParser; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * An array containing the Yaml of the file |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $frontMatter; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $bodyContentEvaluated; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Only the body of the file, i.e. the content |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $bodyContent; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The extension of the file |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $extension; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The original file path to the ContentItem |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $filePath; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The permalink for this object |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $permalink; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * A filesystem object |
||
| 97 | * |
||
| 98 | * @var Filesystem |
||
| 99 | */ |
||
| 100 | protected $fs; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * A list URLs that will redirect to this object |
||
| 104 | * |
||
| 105 | * @var string[] |
||
| 106 | */ |
||
| 107 | private $redirects; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * ContentItem constructor. |
||
| 111 | * |
||
| 112 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 113 | * |
||
| 114 | * @throws FileNotFoundException The given file path does not exist |
||
| 115 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 116 | * no body |
||
| 117 | */ |
||
| 118 | 30 | public function __construct ($filePath) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 138 | * in a simple fashion |
||
| 139 | * |
||
| 140 | * @param string $name The key in the front matter |
||
| 141 | * |
||
| 142 | * @return mixed|null |
||
| 143 | */ |
||
| 144 | public function __get ($name) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 156 | * function |
||
| 157 | * |
||
| 158 | * @param string $name The name of the Front Matter value being looked for |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function __isset ($name) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return the body of the Content Item |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | abstract public function getContent (); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value |
||
| 179 | */ |
||
| 180 | final public function evaluateFrontMatter ($variables = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the Front Matter of a ContentItem as an array |
||
| 192 | * |
||
| 193 | * @param bool $evaluateYaml When set to true, the YAML will be evaluated for variables |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the permalink of this Content Item |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | * @throws \Exception |
||
| 217 | */ |
||
| 218 | final public function getPermalink () |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get an array of URLs that will redirect to |
||
| 252 | * |
||
| 253 | * @return string[] |
||
|
|
|||
| 254 | */ |
||
| 255 | final public function getRedirects () |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | final public function getTargetFile () |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the name of the item, which is just the file name without the extension |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | final public function getName () |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the original file path |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | final public function getFilePath () |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | final public function getRelativeFilePath () |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns true when the evaluated Front Matter has expanded values embeded |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | final public function hasExpandedFrontMatter () |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Read the file, and parse its contents |
||
| 326 | */ |
||
| 327 | final public function refreshFileContent () |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Check whether this object has a reference to a collection or data item |
||
| 362 | * |
||
| 363 | * @param string $namespace 'collections' or 'data' |
||
| 364 | * @param string $needle |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | final public function hasTwigDependency ($namespace, $needle) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Append a custom FrontMatter value |
||
| 375 | * |
||
| 376 | * @param array $frontMatter |
||
| 377 | */ |
||
| 378 | final public function appendFrontMatter (array $frontMatter) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Delete a custom FrontMatter value |
||
| 388 | * |
||
| 389 | * This will not delete a FrontMatter value parsed from the file |
||
| 390 | * |
||
| 391 | * @param string $key |
||
| 392 | */ |
||
| 393 | final public function deleteFrontMatter ($key) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set custom FrontMatter values |
||
| 402 | * |
||
| 403 | * These custom values are temporary and will take precedence over Front Matter evaluated from the file but is only |
||
| 404 | * available to Twig templates |
||
| 405 | * |
||
| 406 | * @param array $frontMatter |
||
| 407 | */ |
||
| 408 | final public function setFrontMatter (array $frontMatter) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 420 | * |
||
| 421 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 422 | * |
||
| 423 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 424 | */ |
||
| 425 | final protected function evaluateYaml (&$yaml) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Handle special front matter values that need special treatment or have special meaning to a Content Item |
||
| 432 | */ |
||
| 433 | private function handleSpecialFrontMatter () |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 459 | * |
||
| 460 | * @param string $filter 'collections' or 'data' |
||
| 461 | */ |
||
| 462 | private function findTwigDataDependencies ($filter) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 474 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | private function getPathPermalink () |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 505 | * |
||
| 506 | * @param string $permalink A permalink |
||
| 507 | * |
||
| 508 | * @return string $permalink The sanitized permalink |
||
| 509 | */ |
||
| 510 | private function sanitizePermalink ($permalink) |
||
| 540 | } |
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.