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 |
||
| 13 | abstract class FrontMatterObject implements Jailable |
||
| 14 | { |
||
| 15 | protected static $whiteListFunctions = array( |
||
| 16 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent' |
||
| 17 | ); |
||
| 18 | |||
| 19 | /** |
||
| 20 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 21 | * |
||
| 22 | * $dataDependencies['collections'] = array() |
||
| 23 | * $dataDependencies['data'] = array() |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $dataDependencies; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 31 | * precedence over values in $frontMatter |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $writableFrontMatter; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 39 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 40 | * instead. |
||
| 41 | * |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $frontMatterBlacklist; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $frontMatterEvaluated; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var FrontMatterParser |
||
| 55 | */ |
||
| 56 | protected $frontMatterParser; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * An array containing the Yaml of the file |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $frontMatter; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 67 | * |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $bodyContentEvaluated; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Only the body of the file, i.e. the content |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $bodyContent; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The extension of the file |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $extension; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The original file path to the ContentItem |
||
| 88 | * |
||
| 89 | * @var SplFileInfo |
||
| 90 | */ |
||
| 91 | protected $filePath; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The permalink for this object |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $permalink; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * A filesystem object |
||
| 102 | * |
||
| 103 | * @var Filesystem |
||
| 104 | */ |
||
| 105 | protected $fs; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * A list URLs that will redirect to this object |
||
| 109 | * |
||
| 110 | * @var string[] |
||
| 111 | */ |
||
| 112 | private $redirects; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The number of lines that Twig template errors should offset |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | private $lineOffset; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * ContentItem constructor. |
||
| 123 | * |
||
| 124 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 125 | * |
||
| 126 | * @throws FileNotFoundException The given file path does not exist |
||
| 127 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 128 | * no body |
||
| 129 | */ |
||
| 130 | 30 | public function __construct ($filePath) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 150 | * in a simple fashion |
||
| 151 | * |
||
| 152 | * @param string $name The key in the front matter |
||
| 153 | * |
||
| 154 | * @return mixed|null |
||
| 155 | */ |
||
| 156 | public function __get ($name) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 168 | * function |
||
| 169 | * |
||
| 170 | * @param string $name The name of the Front Matter value being looked for |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function __isset ($name) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if a specific value is defined in the Front Matter |
||
| 181 | * |
||
| 182 | * @param string $name |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isMagicGet ($name) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return the body of the Content Item |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | abstract public function getContent (); |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value |
||
| 203 | */ |
||
| 204 | final public function evaluateFrontMatter ($variables = null) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the Front Matter of a ContentItem as an array |
||
| 216 | * |
||
| 217 | * @param bool $evaluateYaml When set to true, the YAML will be evaluated for variables |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the permalink of this Content Item |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | * @throws \Exception |
||
| 241 | */ |
||
| 242 | final public function getPermalink () |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get an array of URLs that will redirect to |
||
| 276 | * |
||
| 277 | * @return string[] |
||
| 278 | */ |
||
| 279 | final public function getRedirects () |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | final public function getTargetFile () |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the name of the item, which is just the file name without the extension |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | final public function getName () |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the original file path |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | final public function getFilePath () |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | final public function getRelativeFilePath () |
||
| 343 | |||
| 344 | /** |
||
| 345 | * The number of lines that are taken up by FrontMatter and white space |
||
| 346 | * |
||
| 347 | * @return int |
||
| 348 | */ |
||
| 349 | final public function getLineOffset () |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns true when the evaluated Front Matter has expanded values embeded |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | final public function hasExpandedFrontMatter () |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Read the file, and parse its contents |
||
| 366 | */ |
||
| 367 | final public function refreshFileContent () |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Check whether this object has a reference to a collection or data item |
||
| 403 | * |
||
| 404 | * @param string $namespace 'collections' or 'data' |
||
| 405 | * @param string $needle |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | final public function hasTwigDependency ($namespace, $needle) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Append a custom FrontMatter value |
||
| 416 | * |
||
| 417 | * @param array $frontMatter |
||
| 418 | */ |
||
| 419 | final public function appendFrontMatter (array $frontMatter) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Delete a custom FrontMatter value |
||
| 429 | * |
||
| 430 | * This will not delete a FrontMatter value parsed from the file |
||
| 431 | * |
||
| 432 | * @param string $key |
||
| 433 | */ |
||
| 434 | final public function deleteFrontMatter ($key) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Set custom FrontMatter values |
||
| 443 | * |
||
| 444 | * These custom values are temporary and will take precedence over Front Matter evaluated from the file but is only |
||
| 445 | * available to Twig templates |
||
| 446 | * |
||
| 447 | * @param array $frontMatter |
||
| 448 | */ |
||
| 449 | final public function setFrontMatter (array $frontMatter) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 461 | * |
||
| 462 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 463 | * |
||
| 464 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 465 | */ |
||
| 466 | final protected function evaluateYaml (&$yaml) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Handle special front matter values that need special treatment or have special meaning to a Content Item |
||
| 473 | */ |
||
| 474 | private function handleSpecialFrontMatter () |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 500 | * |
||
| 501 | * @param string $filter 'collections' or 'data' |
||
| 502 | */ |
||
| 503 | private function findTwigDataDependencies ($filter) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 515 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 516 | * |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | private function getPathPermalink () |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 546 | * |
||
| 547 | * @param string $permalink A permalink |
||
| 548 | * |
||
| 549 | * @return string $permalink The sanitized permalink |
||
| 550 | */ |
||
| 551 | private function sanitizePermalink ($permalink) |
||
| 581 | } |
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..