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 |
||
| 16 | abstract class FrontMatterObject implements FrontMatterable, Jailable, \ArrayAccess |
||
| 17 | { |
||
| 18 | protected static $whiteListFunctions = array( |
||
| 19 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
| 20 | 'getExtension', 'getFrontMatter' |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 25 | * |
||
| 26 | * $dataDependencies['collections'] = array() |
||
| 27 | * $dataDependencies['data'] = array() |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $dataDependencies; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 35 | * precedence over values in $frontMatter |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $writableFrontMatter; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 43 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 44 | * instead. |
||
| 45 | * |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $frontMatterBlacklist; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $frontMatterEvaluated; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var FrontMatterParser |
||
| 59 | */ |
||
| 60 | protected $frontMatterParser; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * An array containing the Yaml of the file |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $frontMatter; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 71 | * |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | protected $bodyContentEvaluated; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Only the body of the file, i.e. the content |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $bodyContent; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The permalink for this object |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $permalink; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * A filesystem object |
||
| 92 | * |
||
| 93 | * @var Filesystem |
||
| 94 | */ |
||
| 95 | protected $fs; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The extension of the file |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $extension; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The number of lines that Twig template errors should offset |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | private $lineOffset; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * A list URLs that will redirect to this object |
||
| 113 | * |
||
| 114 | * @var string[] |
||
| 115 | */ |
||
| 116 | private $redirects; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The original file path to the ContentItem |
||
| 120 | * |
||
| 121 | * @var SplFileInfo |
||
| 122 | */ |
||
| 123 | private $filePath; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * ContentItem constructor. |
||
| 127 | * |
||
| 128 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 129 | * |
||
| 130 | * @throws FileNotFoundException The given file path does not exist |
||
| 131 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 132 | * no body |
||
| 133 | */ |
||
| 134 | 105 | public function __construct ($filePath) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Return the body of the Content Item |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | abstract public function getContent (); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the extension of the current file |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | final public function getExtension () |
||
| 165 | { |
||
| 166 | return $this->extension; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the original file path |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | final public function getFilePath () |
||
| 178 | |||
| 179 | /** |
||
| 180 | * The number of lines that are taken up by FrontMatter and white space |
||
| 181 | * |
||
| 182 | * @return int |
||
| 183 | */ |
||
| 184 | final public function getLineOffset () |
||
| 185 | { |
||
| 186 | 5 | return $this->lineOffset; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the name of the item, which is just the file name without the extension |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | final public function getName () |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | final public function getRelativeFilePath () |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | final public function getTargetFile () |
||
| 221 | { |
||
| 222 | 11 | $permalink = $this->getPermalink(); |
|
| 223 | 11 | $missingFile = (substr($permalink, -1) == '/'); |
|
| 224 | 11 | $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink); |
|
| 225 | |||
| 226 | if ($missingFile) |
||
| 227 | 11 | { |
|
| 228 | 4 | $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html'; |
|
| 229 | 4 | } |
|
| 230 | |||
| 231 | 11 | return ltrim($permalink, DIRECTORY_SEPARATOR); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Check whether this object has a reference to a collection or data item |
||
| 236 | * |
||
| 237 | * @param string $namespace 'collections' or 'data' |
||
| 238 | * @param string $needle |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | final public function hasTwigDependency ($namespace, $needle) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Read the file, and parse its contents |
||
| 249 | */ |
||
| 250 | final public function refreshFileContent () |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 300 | * |
||
| 301 | * @param string $filter 'collections' or 'data' |
||
| 302 | */ |
||
| 303 | private function findTwigDataDependencies ($filter) |
||
| 312 | |||
| 313 | // |
||
| 314 | // Permalink and redirect functionality |
||
| 315 | // |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the permalink of this Content Item |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | * @throws \Exception |
||
| 322 | */ |
||
| 323 | final public function getPermalink () |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get an array of URLs that will redirect to |
||
| 359 | * |
||
| 360 | * @return string[] |
||
| 361 | */ |
||
| 362 | final public function getRedirects () |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 374 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | private function getPathPermalink () |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 405 | * |
||
| 406 | * @param string $permalink A permalink |
||
| 407 | * |
||
| 408 | * @return string $permalink The sanitized permalink |
||
| 409 | */ |
||
| 410 | private function sanitizePermalink ($permalink) |
||
| 437 | |||
| 438 | // |
||
| 439 | // WritableFrontMatter Implementation |
||
| 440 | // |
||
| 441 | |||
| 442 | /** |
||
| 443 | * {@inheritdoc} |
||
| 444 | */ |
||
| 445 | final public function evaluateFrontMatter ($variables = null) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * {@inheritdoc} |
||
| 473 | */ |
||
| 474 | final public function hasExpandedFrontMatter () |
||
| 478 | |||
| 479 | /** |
||
| 480 | * {@inheritdoc |
||
| 481 | */ |
||
| 482 | final public function appendFrontMatter (array $frontMatter) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * {@inheritdoc |
||
| 492 | */ |
||
| 493 | final public function deleteFrontMatter ($key) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * {@inheritdoc |
||
| 502 | */ |
||
| 503 | final public function setFrontMatter (array $frontMatter) |
||
| 504 | { |
||
| 505 | if (!is_array($frontMatter)) |
||
| 506 | { |
||
| 507 | throw new \InvalidArgumentException('An array is required for setting the writable FrontMatter'); |
||
| 508 | } |
||
| 509 | |||
| 510 | $this->writableFrontMatter = $frontMatter; |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 515 | * |
||
| 516 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 517 | * |
||
| 518 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 519 | */ |
||
| 520 | private function evaluateYaml (&$yaml) |
||
| 532 | |||
| 533 | // |
||
| 534 | // ArrayAccess Implementation |
||
| 535 | // |
||
| 536 | |||
| 537 | /** |
||
| 538 | * {@inheritdoc} |
||
| 539 | */ |
||
| 540 | public function offsetSet ($offset, $value) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * {@inheritdoc} |
||
| 552 | */ |
||
| 553 | public function offsetExists ($offset) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * {@inheritdoc} |
||
| 566 | */ |
||
| 567 | public function offsetUnset ($offset) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * {@inheritdoc} |
||
| 574 | */ |
||
| 575 | public function offsetGet ($offset) |
||
| 596 | } |
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..