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 FrontMatterDocument 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 FrontMatterDocument, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class FrontMatterDocument extends PermalinkDocument implements |
||
| 22 | TrackableDocument, |
||
| 23 | TwigDocument, |
||
| 24 | WritableDocumentInterface |
||
| 25 | { |
||
| 26 | const TEMPLATE = "---\n%s\n---\n\n%s"; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The names of FrontMatter keys that are specially defined for all Documents |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public static $specialFrontMatterKeys = array( |
||
|
|
|||
| 34 | 'filename', 'basename' |
||
| 35 | ); |
||
| 36 | |||
| 37 | protected static $whiteListFunctions = array( |
||
| 38 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
| 39 | 'getExtension', 'getFrontMatter', 'getBasename' |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $importDependencies; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * An array to keep track of collection or data dependencies used inside of a Twig template. |
||
| 49 | * |
||
| 50 | * $dataDependencies['collections'] = array() |
||
| 51 | * $dataDependencies['data'] = array() |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $dataDependencies; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 59 | * precedence over values in $frontMatter. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $writableFrontMatter; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 67 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 68 | * instead. |
||
| 69 | * |
||
| 70 | * @var string[] |
||
| 71 | */ |
||
| 72 | protected $frontMatterBlacklist; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set to true if the front matter has already been evaluated with variable interpolation. |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $frontMatterEvaluated; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var Parser |
||
| 83 | */ |
||
| 84 | protected $frontMatterParser; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * An array containing the Yaml of the file. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $frontMatter; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set to true if the body has already been parsed as markdown or any other format. |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $bodyContentEvaluated; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Only the body of the file, i.e. the content. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $bodyContent; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The number of lines that Twig template errors should offset. |
||
| 109 | * |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | private $lineOffset; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * ContentItem constructor. |
||
| 116 | * |
||
| 117 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 118 | * |
||
| 119 | * @throws FileNotFoundException The given file path does not exist |
||
| 120 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 121 | * no body |
||
| 122 | */ |
||
| 123 | 141 | public function __construct($filePath) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Return the body of the Content Item. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | abstract public function getContent(); |
||
| 138 | |||
| 139 | final public function getImportDependencies() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * The number of lines that are taken up by FrontMatter and white space. |
||
| 146 | * |
||
| 147 | * @return int |
||
| 148 | */ |
||
| 149 | final public function getLineOffset() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the name of the item, which is just the filename without the extension. |
||
| 156 | * |
||
| 157 | * @deprecated use getObjectName() instead |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | final public function getName() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 47 | public function getObjectName() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Check whether this object has a reference to a collection or data item. |
||
| 175 | * |
||
| 176 | * @param string $namespace 'collections' or 'data' |
||
| 177 | * @param string $needle |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 21 | final public function hasTwigDependency($namespace, $needle) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Check whether this object has an "import" or "from" reference to a given path. |
||
| 190 | * |
||
| 191 | * @param string $filePath |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | final public function hasImportDependency($filePath) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Read the file, and parse its contents. |
||
| 202 | */ |
||
| 203 | 141 | final public function refreshFileContent() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get all of the references to either DataItems or ContentItems inside of given string. |
||
| 259 | * |
||
| 260 | * @param string $filter 'collections' or 'data' |
||
| 261 | */ |
||
| 262 | 129 | private function findTwigDataDependencies($filter) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get all of the "import" and "from" dependencies from a Twig body. |
||
| 277 | */ |
||
| 278 | 129 | private function findTwigImportDependencies() |
|
| 292 | |||
| 293 | 4 | public function isDraft() |
|
| 297 | |||
| 298 | 19 | public function getIterator() |
|
| 302 | |||
| 303 | // |
||
| 304 | // Permalink and redirect functionality |
||
| 305 | // |
||
| 306 | |||
| 307 | 42 | final public function buildPermalink($force = false) |
|
| 308 | { |
||
| 309 | 42 | if (!is_null($this->permalink) && !$force) |
|
| 310 | { |
||
| 311 | 8 | return; |
|
| 312 | } |
||
| 313 | |||
| 314 | 40 | if (!is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion()) |
|
| 315 | { |
||
| 316 | throw new \Exception('The permalink for this item has not been set'); |
||
| 317 | } |
||
| 318 | |||
| 319 | 40 | $permalink = (is_array($this->frontMatter) && isset($this->frontMatter['permalink'])) ? |
|
| 320 | 40 | $this->frontMatter['permalink'] : $this->getPathPermalink(); |
|
| 321 | |||
| 322 | 40 | View Code Duplication | if (is_array($permalink)) |
| 323 | { |
||
| 324 | 22 | $this->permalink = $permalink[0]; |
|
| 325 | 22 | array_shift($permalink); |
|
| 326 | 22 | $this->redirects = $permalink; |
|
| 327 | } |
||
| 328 | else |
||
| 329 | { |
||
| 330 | 24 | $this->permalink = $permalink; |
|
| 331 | 24 | $this->redirects = array(); |
|
| 332 | } |
||
| 333 | 40 | } |
|
| 334 | |||
| 335 | // |
||
| 336 | // WritableFrontMatter Implementation |
||
| 337 | // |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | 7 | final public function evaluateFrontMatter($variables = null) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | */ |
||
| 354 | 30 | final public function getFrontMatter($evaluateYaml = true) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | 2 | final public function hasExpandedFrontMatter() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc. |
||
| 378 | */ |
||
| 379 | final public function appendFrontMatter(array $frontMatter) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc. |
||
| 389 | */ |
||
| 390 | final public function deleteFrontMatter($key) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc. |
||
| 402 | */ |
||
| 403 | 2 | final public function setFrontMatter(array $frontMatter) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 415 | * |
||
| 416 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 417 | * |
||
| 418 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 419 | */ |
||
| 420 | 31 | private function evaluateYaml(&$yaml) |
|
| 421 | { |
||
| 422 | try |
||
| 423 | { |
||
| 424 | 31 | $this->frontMatterParser = new Parser($yaml, array( |
|
| 425 | 31 | 'filename' => $this->getFileName(), |
|
| 426 | 31 | 'basename' => $this->getObjectName(), |
|
| 427 | )); |
||
| 428 | 31 | $this->frontMatterParser->parse(); |
|
| 429 | 30 | $this->frontMatterEvaluated = true; |
|
| 430 | } |
||
| 431 | 1 | catch (\Exception $e) |
|
| 432 | { |
||
| 433 | 1 | throw FileAwareException::castException($e, $this->getRelativeFilePath()); |
|
| 434 | } |
||
| 435 | 30 | } |
|
| 436 | |||
| 437 | // |
||
| 438 | // ArrayAccess Implementation |
||
| 439 | // |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | public function offsetSet($offset, $value) |
||
| 445 | { |
||
| 446 | if (is_null($offset)) |
||
| 447 | { |
||
| 448 | throw new \InvalidArgumentException('$offset cannot be null'); |
||
| 449 | } |
||
| 450 | |||
| 451 | $this->writableFrontMatter[$offset] = $value; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | 34 | public function offsetExists($offset) |
|
| 458 | { |
||
| 459 | 34 | if (isset($this->writableFrontMatter[$offset]) || isset($this->frontMatter[$offset])) |
|
| 460 | { |
||
| 461 | 33 | return true; |
|
| 462 | } |
||
| 463 | |||
| 464 | 14 | $fxnCall = 'get' . ucfirst($offset); |
|
| 465 | |||
| 466 | 14 | return method_exists($this, $fxnCall) && in_array($fxnCall, static::$whiteListFunctions); |
|
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function offsetUnset($offset) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | 51 | public function offsetGet($offset) |
|
| 501 | } |
||
| 502 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.