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:
| 1 | <?php |
||
| 18 | abstract class FrontMatterDocument extends ReadableDocument implements \IteratorAggregate, \ArrayAccess |
||
| 19 | { |
||
| 20 | const TEMPLATE = "---\n%s\n---\n\n%s"; |
||
| 21 | |||
| 22 | /** @var array Functions that are white listed and can be called from templates. */ |
||
| 23 | public static $whiteListedFunctions = [ |
||
| 24 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getContent', |
||
| 25 | 'getFilename', 'getBasename', 'getExtension', 'isDraft', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** @var array FrontMatter keys that will be defined internally and cannot be overridden by users. */ |
||
| 29 | protected $specialFrontMatter = [ |
||
| 30 | 'filePath' => null, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** @var bool Whether or not the body content has been evaluated yet. */ |
||
| 34 | protected $bodyContentEvaluated = false; |
||
| 35 | |||
| 36 | /** @var FrontMatterParser */ |
||
| 37 | protected $frontMatterParser; |
||
| 38 | |||
| 39 | /** @var array The raw FrontMatter that has not been evaluated. */ |
||
| 40 | protected $rawFrontMatter = []; |
||
| 41 | |||
| 42 | /** @var array|null FrontMatter that is read from user documents. */ |
||
| 43 | protected $frontMatter = null; |
||
| 44 | |||
| 45 | /** @var int The number of lines that Twig template errors should offset. */ |
||
| 46 | protected $lineOffset = 0; |
||
| 47 | |||
| 48 | /// |
||
| 49 | // Getter functions |
||
| 50 | /// |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function getIterator() |
||
| 56 | { |
||
| 57 | return new \ArrayIterator($this->frontMatter); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the number of lines that are taken up by FrontMatter and whitespace. |
||
| 62 | * |
||
| 63 | * @return int |
||
| 64 | */ |
||
| 65 | public function getLineOffset() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get whether or not this document is a draft. |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | 2 | public function isDraft() |
|
| 79 | |||
| 80 | /// |
||
| 81 | // FrontMatter functionality |
||
| 82 | /// |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 118 | protected function beforeReadContents() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 118 | protected function readContents($readNecessary) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 108 | protected function afterReadContents($fileStructure) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Get the FrontMatter without evaluating its variables or special functionality. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 18 | final public function getRawFrontMatter() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get the FrontMatter for this document. |
||
| 203 | * |
||
| 204 | * @param bool $evaluateYaml whether or not to evaluate any variables |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | 14 | final public function getFrontMatter($evaluateYaml = true) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | 92 | final public function evaluateFrontMatter(array $variables = [], array $complexVariables = []) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Returns true when the evaluated Front Matter has expanded values embedded. |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 14 | final public function hasExpandedFrontMatter() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 239 | * |
||
| 240 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 241 | * |
||
| 242 | * @see $specialFrontMatter |
||
| 243 | * |
||
| 244 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 245 | */ |
||
| 246 | 92 | private function evaluateYaml(array &$yaml, array $complexVariables = []) |
|
| 264 | |||
| 265 | /// |
||
| 266 | // ArrayAccess Implementation |
||
| 267 | /// |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function offsetSet($offset, $value) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | 47 | public function offsetExists($offset) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function offsetUnset($offset) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 48 | public function offsetGet($offset) |
|
| 324 | } |
||
| 325 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.