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 | 84 | public function __construct ($filePath) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Return the body of the Content Item |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | abstract public function getContent (); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the extension of the current file |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | final public function getExtension () |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the original file path |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | final public function getFilePath () |
||
| 177 | |||
| 178 | /** |
||
| 179 | * The number of lines that are taken up by FrontMatter and white space |
||
| 180 | * |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | final public function getLineOffset () |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the name of the item, which is just the file name without the extension |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | final public function getName () |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | final public function getRelativeFilePath () |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | final public function getTargetFile () |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check whether this object has a reference to a collection or data item |
||
| 235 | * |
||
| 236 | * @param string $namespace 'collections' or 'data' |
||
| 237 | * @param string $needle |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | final public function hasTwigDependency ($namespace, $needle) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Read the file, and parse its contents |
||
| 248 | */ |
||
| 249 | final public function refreshFileContent () |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 299 | * |
||
| 300 | * @param string $filter 'collections' or 'data' |
||
| 301 | */ |
||
| 302 | private function findTwigDataDependencies ($filter) |
||
| 311 | |||
| 312 | // |
||
| 313 | // Permalink and redirect functionality |
||
| 314 | // |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the permalink of this Content Item |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | * @throws \Exception |
||
| 321 | */ |
||
| 322 | final public function getPermalink () |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get an array of URLs that will redirect to |
||
| 358 | * |
||
| 359 | * @return string[] |
||
| 360 | */ |
||
| 361 | final public function getRedirects () |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 373 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | private function getPathPermalink () |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 404 | * |
||
| 405 | * @param string $permalink A permalink |
||
| 406 | * |
||
| 407 | * @return string $permalink The sanitized permalink |
||
| 408 | */ |
||
| 409 | private function sanitizePermalink ($permalink) |
||
| 436 | |||
| 437 | // |
||
| 438 | // WritableFrontMatter Implementation |
||
| 439 | // |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | final public function evaluateFrontMatter ($variables = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | final public function hasExpandedFrontMatter () |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc |
||
| 480 | */ |
||
| 481 | final public function appendFrontMatter (array $frontMatter) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc |
||
| 491 | */ |
||
| 492 | final public function deleteFrontMatter ($key) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc |
||
| 501 | */ |
||
| 502 | final public function setFrontMatter (array $frontMatter) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 514 | * |
||
| 515 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 516 | * |
||
| 517 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 518 | */ |
||
| 519 | private function evaluateYaml (&$yaml) |
||
| 524 | |||
| 525 | // |
||
| 526 | // ArrayAccess Implementation |
||
| 527 | // |
||
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritdoc} |
||
| 531 | */ |
||
| 532 | public function offsetSet ($offset, $value) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * {@inheritdoc} |
||
| 544 | */ |
||
| 545 | public function offsetExists ($offset) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritdoc} |
||
| 558 | */ |
||
| 559 | public function offsetUnset ($offset) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * {@inheritdoc} |
||
| 566 | */ |
||
| 567 | public function offsetGet ($offset) |
||
| 588 | } |
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..