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 FrontMatterable, Jailable, \ArrayAccess |
||
| 15 | { |
||
| 16 | protected static $whiteListFunctions = array( |
||
| 17 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
| 18 | 'getExtension', 'getFrontMatter' |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 23 | * |
||
| 24 | * $dataDependencies['collections'] = array() |
||
| 25 | * $dataDependencies['data'] = array() |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $dataDependencies; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 33 | * precedence over values in $frontMatter |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $writableFrontMatter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 41 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 42 | * instead. |
||
| 43 | * |
||
| 44 | * @var string[] |
||
| 45 | */ |
||
| 46 | protected $frontMatterBlacklist; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $frontMatterEvaluated; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var FrontMatterParser |
||
| 57 | */ |
||
| 58 | protected $frontMatterParser; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * An array containing the Yaml of the file |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $frontMatter; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 69 | * |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $bodyContentEvaluated; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Only the body of the file, i.e. the content |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $bodyContent; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The permalink for this object |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $permalink; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * A filesystem object |
||
| 90 | * |
||
| 91 | * @var Filesystem |
||
| 92 | */ |
||
| 93 | protected $fs; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The extension of the file |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | private $extension; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The number of lines that Twig template errors should offset |
||
| 104 | * |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | private $lineOffset; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * A list URLs that will redirect to this object |
||
| 111 | * |
||
| 112 | * @var string[] |
||
| 113 | */ |
||
| 114 | private $redirects; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The original file path to the ContentItem |
||
| 118 | * |
||
| 119 | * @var SplFileInfo |
||
| 120 | */ |
||
| 121 | private $filePath; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * ContentItem constructor. |
||
| 125 | * |
||
| 126 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 127 | * |
||
| 128 | * @throws FileNotFoundException The given file path does not exist |
||
| 129 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 130 | * no body |
||
| 131 | */ |
||
| 132 | 42 | public function __construct ($filePath) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 152 | * in a simple fashion |
||
| 153 | * |
||
| 154 | * @param string $name The key in the front matter |
||
| 155 | * |
||
| 156 | * @return mixed|null |
||
| 157 | */ |
||
| 158 | public function __get ($name) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 170 | * function |
||
| 171 | * |
||
| 172 | * @param string $name The name of the Front Matter value being looked for |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function __isset ($name) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return the body of the Content Item |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | abstract public function getContent (); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the extension of the current file |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | final public function getExtension () |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the original file path |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | final public function getFilePath () |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The number of lines that are taken up by FrontMatter and white space |
||
| 210 | * |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | final public function getLineOffset () |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the name of the item, which is just the file name without the extension |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | final public function getName () |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | final public function getRelativeFilePath () |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | final public function getTargetFile () |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Check whether this object has a reference to a collection or data item |
||
| 265 | * |
||
| 266 | * @param string $namespace 'collections' or 'data' |
||
| 267 | * @param string $needle |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | final public function hasTwigDependency ($namespace, $needle) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Read the file, and parse its contents |
||
| 278 | */ |
||
| 279 | final public function refreshFileContent () |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 316 | * |
||
| 317 | * @param string $filter 'collections' or 'data' |
||
| 318 | */ |
||
| 319 | private function findTwigDataDependencies ($filter) |
||
| 328 | |||
| 329 | // |
||
| 330 | // Permalink and redirect functionality |
||
| 331 | // |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the permalink of this Content Item |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | * @throws \Exception |
||
| 338 | */ |
||
| 339 | final public function getPermalink () |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get an array of URLs that will redirect to |
||
| 375 | * |
||
| 376 | * @return string[] |
||
| 377 | */ |
||
| 378 | final public function getRedirects () |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 390 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | private function getPathPermalink () |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 421 | * |
||
| 422 | * @param string $permalink A permalink |
||
| 423 | * |
||
| 424 | * @return string $permalink The sanitized permalink |
||
| 425 | */ |
||
| 426 | private function sanitizePermalink ($permalink) |
||
| 453 | |||
| 454 | // |
||
| 455 | // WritableFrontMatter Implementation |
||
| 456 | // |
||
| 457 | |||
| 458 | /** |
||
| 459 | * {@inheritdoc} |
||
| 460 | */ |
||
| 461 | final public function evaluateFrontMatter ($variables = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * {@inheritdoc} |
||
| 489 | */ |
||
| 490 | final public function hasExpandedFrontMatter () |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc |
||
| 497 | */ |
||
| 498 | final public function appendFrontMatter (array $frontMatter) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * {@inheritdoc |
||
| 508 | */ |
||
| 509 | final public function deleteFrontMatter ($key) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * {@inheritdoc |
||
| 518 | */ |
||
| 519 | final public function setFrontMatter (array $frontMatter) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 531 | * |
||
| 532 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 533 | * |
||
| 534 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 535 | */ |
||
| 536 | private function evaluateYaml (&$yaml) |
||
| 541 | |||
| 542 | // |
||
| 543 | // Jailable Implementation |
||
| 544 | // |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Check if a specific value is defined in the Front Matter |
||
| 548 | * |
||
| 549 | * @param string $name |
||
| 550 | * |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function isMagicGet ($name) |
||
| 558 | 4 | ||
| 559 | // |
||
| 560 | 1 | // ArrayAccess Implementation |
|
| 561 | // |
||
| 562 | |||
| 563 | /** |
||
| 564 | * {@inheritdoc} |
||
| 565 | */ |
||
| 566 | public function offsetSet ($offset, $value) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * {@inheritdoc} |
||
| 578 | */ |
||
| 579 | public function offsetExists ($offset) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * {@inheritdoc} |
||
| 586 | */ |
||
| 587 | public function offsetUnset ($offset) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * {@inheritdoc} |
||
| 594 | */ |
||
| 595 | public function offsetGet ($offset) |
||
| 616 | } |
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..