Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 11 | abstract class FrontMatterObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 15 | * |
||
| 16 | * $dataDependencies['collections'] = array() |
||
| 17 | * $dataDependencies['data'] = array() |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $dataDependencies; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 25 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 26 | * instead. |
||
| 27 | * |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | protected $frontMatterBlacklist; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $frontMatterEvaluated; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An array containing the Yaml of the file |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $frontMatter; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $bodyContentEvaluated; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Only the body of the file, i.e. the content |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $bodyContent; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The extension of the file |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $extension; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The original file path to the ContentItem |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $filePath; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * A filesystem object |
||
| 76 | * |
||
| 77 | * @var Filesystem |
||
| 78 | */ |
||
| 79 | protected $fs; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The permalink for this object |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $permalink; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * A list URLs that will redirect to this object |
||
| 90 | * |
||
| 91 | * @var string[] |
||
| 92 | */ |
||
| 93 | private $redirects; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * ContentItem constructor. |
||
| 97 | * |
||
| 98 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 99 | * |
||
| 100 | * @throws FileNotFoundException The given file path does not exist |
||
| 101 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 102 | * no body |
||
| 103 | */ |
||
| 104 | 29 | public function __construct ($filePath) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * The magic getter returns values from the front matter in order to make these values accessible to Twig templates |
||
| 123 | * in a simple fashion |
||
| 124 | * |
||
| 125 | * @param string $name The key in the front matter |
||
| 126 | * |
||
| 127 | * @return mixed|null |
||
| 128 | */ |
||
| 129 | public function __get ($name) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The magic getter returns true if the value exists in the Front Matter. This is used in conjunction with the __get |
||
| 136 | * function |
||
| 137 | * |
||
| 138 | * @param string $name The name of the Front Matter value being looked for |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function __isset ($name) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the body of the Content Item |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | abstract public function getContent (); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param array|null $variables An array of YAML variables to use in evaluating the `$permalink` value |
||
| 156 | */ |
||
| 157 | final public function evaluateFrontMatter ($variables = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the Front Matter of a ContentItem as an array |
||
| 169 | * |
||
| 170 | * @param bool $evaluateYaml When set to true, the YAML will be evaluated for variables |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the permalink of this Content Item |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | final public function getPermalink () |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get an array of URLs that will redirect to |
||
| 223 | * |
||
| 224 | * @return string[] |
||
|
|
|||
| 225 | */ |
||
| 226 | final public function getRedirects () |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | final public function getTargetFile () |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the name of the item, which is just the file name without the extension |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | final public function getName () |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the original file path |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | final public function getFilePath () |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | final public function getRelativeFilePath () |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Read the file, and parse its contents |
||
| 286 | */ |
||
| 287 | final public function refreshFileContent () |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Check whether this object has a reference to a collection or data item |
||
| 322 | * |
||
| 323 | * @param string $namespace 'collections' or 'data' |
||
| 324 | * @param string $needle |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | final public function hasTwigDependency ($namespace, $needle) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 335 | * |
||
| 336 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 337 | * |
||
| 338 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 339 | */ |
||
| 340 | final protected function evaluateYaml (&$yaml) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values |
||
| 357 | * |
||
| 358 | * @param string $string The string that will be evaluated |
||
| 359 | * @param array $yaml The existing front matter from which the variable values will be pulled from |
||
| 360 | * |
||
| 361 | * @return string The final string with variables evaluated |
||
| 362 | * |
||
| 363 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 364 | */ |
||
| 365 | private function evaluateYamlVar ($string, $yaml) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Handle special front matter values that need special treatment or have special meaning to a Content Item |
||
| 392 | */ |
||
| 393 | private function handleSpecialFrontMatter () |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 419 | * |
||
| 420 | * @param string $filter 'collections' or 'data' |
||
| 421 | */ |
||
| 422 | private function findTwigDataDependencies ($filter) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 434 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | private function getPathPermalink () |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 459 | * |
||
| 460 | * @param string $permalink A permalink |
||
| 461 | * |
||
| 462 | * @return string $permalink The sanitized permalink |
||
| 463 | */ |
||
| 464 | private function sanitizePermalink ($permalink) |
||
| 494 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.