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 |
||
| 14 | abstract class FrontMatterObject implements Jailable |
||
| 15 | { |
||
| 16 | protected static $whiteListFunctions = array( |
||
| 17 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent' |
||
| 18 | ); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 22 | * |
||
| 23 | * $dataDependencies['collections'] = array() |
||
| 24 | * $dataDependencies['data'] = array() |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $dataDependencies; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 32 | * precedence over values in $frontMatter |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $writableFrontMatter; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 40 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 41 | * instead. |
||
| 42 | * |
||
| 43 | * @var string[] |
||
| 44 | */ |
||
| 45 | protected $frontMatterBlacklist; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $frontMatterEvaluated; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var FrontMatterParser |
||
| 56 | */ |
||
| 57 | protected $frontMatterParser; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * An array containing the Yaml of the file |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $frontMatter; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $bodyContentEvaluated; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Only the body of the file, i.e. the content |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $bodyContent; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The extension of the file |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $extension; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The original file path to the ContentItem |
||
| 89 | * |
||
| 90 | * @var SplFileInfo |
||
| 91 | */ |
||
| 92 | protected $filePath; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The permalink for this object |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $permalink; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * A filesystem object |
||
| 103 | * |
||
| 104 | * @var Filesystem |
||
| 105 | */ |
||
| 106 | protected $fs; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * A list URLs that will redirect to this object |
||
| 110 | * |
||
| 111 | * @var string[] |
||
| 112 | */ |
||
| 113 | private $redirects; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The number of lines that Twig template errors should offset |
||
| 117 | * |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | private $lineOffset; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * ContentItem constructor. |
||
| 124 | * |
||
| 125 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 126 | * |
||
| 127 | * @throws FileNotFoundException The given file path does not exist |
||
| 128 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 129 | * no body |
||
| 130 | */ |
||
| 131 | 39 | public function __construct ($filePath) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 151 | * in a simple fashion |
||
| 152 | * |
||
| 153 | * @param string $name The key in the front matter |
||
| 154 | * |
||
| 155 | * @return mixed|null |
||
| 156 | */ |
||
| 157 | public function __get ($name) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 169 | * function |
||
| 170 | * |
||
| 171 | * @param string $name The name of the Front Matter value being looked for |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function __isset ($name) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check if a specific value is defined in the Front Matter |
||
| 182 | * |
||
| 183 | * @param string $name |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function isMagicGet ($name) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return the body of the Content Item |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | abstract public function getContent (); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value |
||
| 204 | */ |
||
| 205 | final public function evaluateFrontMatter ($variables = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the Front Matter of a ContentItem as an array |
||
| 217 | * |
||
| 218 | * @param bool $evaluateYaml When set to true, the YAML will be evaluated for variables |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the permalink of this Content Item |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | final public function getPermalink () |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get an array of URLs that will redirect to |
||
| 279 | * |
||
| 280 | * @return string[] |
||
| 281 | */ |
||
| 282 | final public function getRedirects () |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | final public function getTargetFile () |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the name of the item, which is just the file name without the extension |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | final public function getName () |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the original file path |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | final public function getFilePath () |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | final public function getRelativeFilePath () |
||
| 346 | |||
| 347 | /** |
||
| 348 | * The number of lines that are taken up by FrontMatter and white space |
||
| 349 | * |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | final public function getLineOffset () |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns true when the evaluated Front Matter has expanded values embeded |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | final public function hasExpandedFrontMatter () |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Read the file, and parse its contents |
||
| 369 | */ |
||
| 370 | final public function refreshFileContent () |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Check whether this object has a reference to a collection or data item |
||
| 409 | * |
||
| 410 | * @param string $namespace 'collections' or 'data' |
||
| 411 | * @param string $needle |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | final public function hasTwigDependency ($namespace, $needle) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Append a custom FrontMatter value |
||
| 422 | * |
||
| 423 | * @param array $frontMatter |
||
| 424 | */ |
||
| 425 | final public function appendFrontMatter (array $frontMatter) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Delete a custom FrontMatter value |
||
| 435 | * |
||
| 436 | * This will not delete a FrontMatter value parsed from the file |
||
| 437 | * |
||
| 438 | * @param string $key |
||
| 439 | */ |
||
| 440 | final public function deleteFrontMatter ($key) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set custom FrontMatter values |
||
| 449 | * |
||
| 450 | * These custom values are temporary and will take precedence over Front Matter evaluated from the file but is only |
||
| 451 | * available to Twig templates |
||
| 452 | * |
||
| 453 | * @param array $frontMatter |
||
| 454 | */ |
||
| 455 | final public function setFrontMatter (array $frontMatter) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 467 | * |
||
| 468 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 469 | * |
||
| 470 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 471 | */ |
||
| 472 | final protected function evaluateYaml (&$yaml) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Handle special front matter values that need special treatment or have special meaning to a Content Item |
||
| 479 | */ |
||
| 480 | private function handleSpecialFrontMatter () |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 511 | * |
||
| 512 | * @param string $filter 'collections' or 'data' |
||
| 513 | */ |
||
| 514 | private function findTwigDataDependencies ($filter) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 526 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | private function getPathPermalink () |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 557 | * |
||
| 558 | * @param string $permalink A permalink |
||
| 559 | * |
||
| 560 | * @return string $permalink The sanitized permalink |
||
| 561 | */ |
||
| 562 | private function sanitizePermalink ($permalink) |
||
| 589 | } |
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..