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 implements Jailable |
||
| 13 | { |
||
| 14 | protected static $whiteListFunctions = array( |
||
| 15 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent' |
||
| 16 | ); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 20 | * |
||
| 21 | * $dataDependencies['collections'] = array() |
||
| 22 | * $dataDependencies['data'] = array() |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $dataDependencies; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 30 | * precedence over values in $frontMatter |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $writableFrontMatter; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 38 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 39 | * instead. |
||
| 40 | * |
||
| 41 | * @var string[] |
||
| 42 | */ |
||
| 43 | protected $frontMatterBlacklist; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $frontMatterEvaluated; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var FrontMatterParser |
||
| 54 | */ |
||
| 55 | protected $frontMatterParser; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * An array containing the Yaml of the file |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $frontMatter; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 66 | * |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | protected $bodyContentEvaluated; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Only the body of the file, i.e. the content |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $bodyContent; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The extension of the file |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $extension; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The original file path to the ContentItem |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $filePath; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The permalink for this object |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $permalink; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * A filesystem object |
||
| 101 | * |
||
| 102 | * @var Filesystem |
||
| 103 | */ |
||
| 104 | protected $fs; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * A list URLs that will redirect to this object |
||
| 108 | * |
||
| 109 | * @var string[] |
||
| 110 | */ |
||
| 111 | private $redirects; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * ContentItem constructor. |
||
| 115 | * |
||
| 116 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 117 | * |
||
| 118 | * @throws FileNotFoundException The given file path does not exist |
||
| 119 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 120 | * no body |
||
| 121 | */ |
||
| 122 | 30 | public function __construct ($filePath) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 142 | * in a simple fashion |
||
| 143 | * |
||
| 144 | * @param string $name The key in the front matter |
||
| 145 | * |
||
| 146 | * @return mixed|null |
||
| 147 | */ |
||
| 148 | public function __get ($name) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 160 | * function |
||
| 161 | * |
||
| 162 | * @param string $name The name of the Front Matter value being looked for |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function __isset ($name) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Check if a specific value is defined in the Front Matter |
||
| 173 | * |
||
| 174 | * @param string $name |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function isMagicGet ($name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return the body of the Content Item |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | abstract public function getContent (); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value |
||
| 195 | */ |
||
| 196 | final public function evaluateFrontMatter ($variables = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the Front Matter of a ContentItem as an array |
||
| 208 | * |
||
| 209 | * @param bool $evaluateYaml When set to true, the YAML will be evaluated for variables |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the permalink of this Content Item |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | final public function getPermalink () |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get an array of URLs that will redirect to |
||
| 268 | * |
||
| 269 | * @return string[] |
||
|
|
|||
| 270 | */ |
||
| 271 | final public function getRedirects () |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | final public function getTargetFile () |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the name of the item, which is just the file name without the extension |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | final public function getName () |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get the original file path |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | final public function getFilePath () |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | final public function getRelativeFilePath () |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns true when the evaluated Front Matter has expanded values embeded |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | final public function hasExpandedFrontMatter () |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Read the file, and parse its contents |
||
| 342 | */ |
||
| 343 | final public function refreshFileContent () |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Check whether this object has a reference to a collection or data item |
||
| 378 | * |
||
| 379 | * @param string $namespace 'collections' or 'data' |
||
| 380 | * @param string $needle |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | final public function hasTwigDependency ($namespace, $needle) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Append a custom FrontMatter value |
||
| 391 | * |
||
| 392 | * @param array $frontMatter |
||
| 393 | */ |
||
| 394 | final public function appendFrontMatter (array $frontMatter) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Delete a custom FrontMatter value |
||
| 404 | * |
||
| 405 | * This will not delete a FrontMatter value parsed from the file |
||
| 406 | * |
||
| 407 | * @param string $key |
||
| 408 | */ |
||
| 409 | final public function deleteFrontMatter ($key) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set custom FrontMatter values |
||
| 418 | * |
||
| 419 | * These custom values are temporary and will take precedence over Front Matter evaluated from the file but is only |
||
| 420 | * available to Twig templates |
||
| 421 | * |
||
| 422 | * @param array $frontMatter |
||
| 423 | */ |
||
| 424 | final public function setFrontMatter (array $frontMatter) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 436 | * |
||
| 437 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 438 | * |
||
| 439 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 440 | */ |
||
| 441 | final protected function evaluateYaml (&$yaml) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Handle special front matter values that need special treatment or have special meaning to a Content Item |
||
| 448 | */ |
||
| 449 | private function handleSpecialFrontMatter () |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 475 | * |
||
| 476 | * @param string $filter 'collections' or 'data' |
||
| 477 | */ |
||
| 478 | private function findTwigDataDependencies ($filter) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 490 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | private function getPathPermalink () |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 521 | * |
||
| 522 | * @param string $permalink A permalink |
||
| 523 | * |
||
| 524 | * @return string $permalink The sanitized permalink |
||
| 525 | */ |
||
| 526 | private function sanitizePermalink ($permalink) |
||
| 556 | } |
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.