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 |
||
| 15 | abstract class FrontMatterObject implements FrontMatterable, Jailable, \ArrayAccess |
||
| 16 | { |
||
| 17 | protected static $whiteListFunctions = array( |
||
| 18 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
| 19 | 'getExtension', 'getFrontMatter' |
||
| 20 | ); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 24 | * |
||
| 25 | * $dataDependencies['collections'] = array() |
||
| 26 | * $dataDependencies['data'] = array() |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $dataDependencies; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 34 | * precedence over values in $frontMatter |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $writableFrontMatter; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 42 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 43 | * instead. |
||
| 44 | * |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | protected $frontMatterBlacklist; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $frontMatterEvaluated; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var FrontMatterParser |
||
| 58 | */ |
||
| 59 | protected $frontMatterParser; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * An array containing the Yaml of the file |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $frontMatter; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 70 | * |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $bodyContentEvaluated; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Only the body of the file, i.e. the content |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $bodyContent; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The permalink for this object |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $permalink; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * A filesystem object |
||
| 91 | * |
||
| 92 | * @var Filesystem |
||
| 93 | */ |
||
| 94 | protected $fs; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The extension of the file |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $extension; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The number of lines that Twig template errors should offset |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | private $lineOffset; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * A list URLs that will redirect to this object |
||
| 112 | * |
||
| 113 | * @var string[] |
||
| 114 | */ |
||
| 115 | private $redirects; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The original file path to the ContentItem |
||
| 119 | * |
||
| 120 | * @var SplFileInfo |
||
| 121 | */ |
||
| 122 | private $filePath; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * ContentItem constructor. |
||
| 126 | * |
||
| 127 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 128 | * |
||
| 129 | * @throws FileNotFoundException The given file path does not exist |
||
| 130 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 131 | * no body |
||
| 132 | */ |
||
| 133 | 54 | public function __construct ($filePath) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 153 | * in a simple fashion |
||
| 154 | * |
||
| 155 | * @param string $name The key in the front matter |
||
| 156 | * |
||
| 157 | * @return mixed|null |
||
| 158 | */ |
||
| 159 | public function __get ($name) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 171 | * function |
||
| 172 | * |
||
| 173 | * @param string $name The name of the Front Matter value being looked for |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function __isset ($name) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return the body of the Content Item |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | abstract public function getContent (); |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the extension of the current file |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | final public function getExtension () |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the original file path |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | final public function getFilePath () |
||
| 208 | |||
| 209 | /** |
||
| 210 | * The number of lines that are taken up by FrontMatter and white space |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | final public function getLineOffset () |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the name of the item, which is just the file name without the extension |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | final public function getName () |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | final public function getRelativeFilePath () |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | final public function getTargetFile () |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Check whether this object has a reference to a collection or data item |
||
| 266 | * |
||
| 267 | * @param string $namespace 'collections' or 'data' |
||
| 268 | * @param string $needle |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | final public function hasTwigDependency ($namespace, $needle) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Read the file, and parse its contents |
||
| 279 | */ |
||
| 280 | final public function refreshFileContent () |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 330 | * |
||
| 331 | * @param string $filter 'collections' or 'data' |
||
| 332 | */ |
||
| 333 | private function findTwigDataDependencies ($filter) |
||
| 342 | |||
| 343 | // |
||
| 344 | // Permalink and redirect functionality |
||
| 345 | // |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the permalink of this Content Item |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | * @throws \Exception |
||
| 352 | */ |
||
| 353 | final public function getPermalink () |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get an array of URLs that will redirect to |
||
| 389 | * |
||
| 390 | * @return string[] |
||
| 391 | */ |
||
| 392 | final public function getRedirects () |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 404 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | private function getPathPermalink () |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 435 | * |
||
| 436 | * @param string $permalink A permalink |
||
| 437 | * |
||
| 438 | * @return string $permalink The sanitized permalink |
||
| 439 | */ |
||
| 440 | private function sanitizePermalink ($permalink) |
||
| 467 | |||
| 468 | // |
||
| 469 | // WritableFrontMatter Implementation |
||
| 470 | // |
||
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | */ |
||
| 475 | final public function evaluateFrontMatter ($variables = null) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * {@inheritdoc} |
||
| 503 | */ |
||
| 504 | final public function hasExpandedFrontMatter () |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc |
||
| 511 | */ |
||
| 512 | final public function appendFrontMatter (array $frontMatter) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc |
||
| 522 | */ |
||
| 523 | final public function deleteFrontMatter ($key) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * {@inheritdoc |
||
| 532 | */ |
||
| 533 | final public function setFrontMatter (array $frontMatter) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 545 | * |
||
| 546 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 547 | * |
||
| 548 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 549 | */ |
||
| 550 | private function evaluateYaml (&$yaml) |
||
| 555 | |||
| 556 | // |
||
| 557 | // Jailable Implementation |
||
| 558 | // |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Check if a specific value is defined in the Front Matter |
||
| 562 | * |
||
| 563 | * @param string $name |
||
| 564 | * |
||
| 565 | * @return bool |
||
| 566 | */ |
||
| 567 | public function isMagicGet ($name) |
||
| 572 | |||
| 573 | // |
||
| 574 | // ArrayAccess Implementation |
||
| 575 | // |
||
| 576 | |||
| 577 | /** |
||
| 578 | * {@inheritdoc} |
||
| 579 | */ |
||
| 580 | public function offsetSet ($offset, $value) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * {@inheritdoc} |
||
| 592 | */ |
||
| 593 | public function offsetExists ($offset) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * {@inheritdoc} |
||
| 600 | */ |
||
| 601 | public function offsetUnset ($offset) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * {@inheritdoc} |
||
| 608 | */ |
||
| 609 | public function offsetGet ($offset) |
||
| 630 | } |
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..