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 |
||
| 15 | abstract class FrontMatterObject implements FrontMatterable, Jailable, \ArrayAccess |
||
| 16 | { |
||
| 17 | protected static $whiteListFunctions = array( |
||
| 18 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
| 19 | 'getExtension', 'getFrontMatter' |
||
| 20 | ); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * An array to keep track of collection or data dependencies used inside of a Twig template |
||
| 24 | * |
||
| 25 | * $dataDependencies['collections'] = array() |
||
| 26 | * $dataDependencies['data'] = array() |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $dataDependencies; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
| 34 | * precedence over values in $frontMatter |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $writableFrontMatter; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
| 42 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
| 43 | * instead. |
||
| 44 | * |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | protected $frontMatterBlacklist; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set to true if the front matter has already been evaluated with variable interpolation |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $frontMatterEvaluated; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var FrontMatterParser |
||
| 58 | */ |
||
| 59 | protected $frontMatterParser; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * An array containing the Yaml of the file |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $frontMatter; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set to true if the body has already been parsed as markdown or any other format |
||
| 70 | * |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $bodyContentEvaluated; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Only the body of the file, i.e. the content |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $bodyContent; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The permalink for this object |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $permalink; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * A filesystem object |
||
| 91 | * |
||
| 92 | * @var Filesystem |
||
| 93 | */ |
||
| 94 | protected $fs; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The extension of the file |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $extension; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The number of lines that Twig template errors should offset |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | private $lineOffset; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * A list URLs that will redirect to this object |
||
| 112 | * |
||
| 113 | * @var string[] |
||
| 114 | */ |
||
| 115 | private $redirects; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The original file path to the ContentItem |
||
| 119 | * |
||
| 120 | * @var SplFileInfo |
||
| 121 | */ |
||
| 122 | private $filePath; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * ContentItem constructor. |
||
| 126 | * |
||
| 127 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
| 128 | * |
||
| 129 | * @throws FileNotFoundException The given file path does not exist |
||
| 130 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
| 131 | * no body |
||
| 132 | */ |
||
| 133 | 84 | public function __construct ($filePath) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Return the body of the Content Item |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | abstract public function getContent (); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the extension of the current file |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | final public function getExtension () |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the original file path |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | final public function getFilePath () |
||
| 177 | |||
| 178 | /** |
||
| 179 | * The number of lines that are taken up by FrontMatter and white space |
||
| 180 | * |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | final public function getLineOffset () |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the name of the item, which is just the file name without the extension |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | final public function getName () |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the relative path to this file relative to the root of the Stakx website |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | final public function getRelativeFilePath () |
||
| 204 | { |
||
| 205 | 44 | if ($this->filePath instanceof SplFileInfo) |
|
| 206 | { |
||
| 207 | 31 | return $this->filePath->getRelativePathname(); |
|
| 208 | } |
||
| 209 | |||
| 210 | // TODO ensure that we always get SplFileInfo objects, even when handling VFS documents |
||
| 211 | 13 | return $this->fs->getRelativePath($this->filePath); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the destination of where this Content Item would be written to when the website is compiled |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | final public function getTargetFile () |
||
| 220 | { |
||
| 221 | 7 | $permalink = $this->getPermalink(); |
|
| 222 | 7 | $extension = $this->fs->getExtension($permalink); |
|
| 223 | 7 | $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink); |
|
| 224 | |||
| 225 | 7 | if (empty($extension)) |
|
| 226 | { |
||
| 227 | 3 | $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html'; |
|
| 228 | } |
||
| 229 | |||
| 230 | 7 | return ltrim($permalink, DIRECTORY_SEPARATOR); |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check whether this object has a reference to a collection or data item |
||
| 235 | * |
||
| 236 | * @param string $namespace 'collections' or 'data' |
||
| 237 | * @param string $needle |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | final public function hasTwigDependency ($namespace, $needle) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Read the file, and parse its contents |
||
| 248 | */ |
||
| 249 | final public function refreshFileContent () |
||
| 250 | { |
||
| 251 | // This function can be called after the initial object was created and the file may have been deleted since the |
||
| 252 | // creation of the object. |
||
| 253 | 83 | if (!$this->fs->exists($this->filePath)) |
|
| 254 | { |
||
| 255 | 1 | throw new FileNotFoundException(null, 0, null, $this->filePath); |
|
| 256 | } |
||
| 257 | |||
| 258 | 83 | $rawFileContents = file_get_contents($this->filePath); |
|
| 259 | 83 | $fileStructure = array(); |
|
| 260 | 83 | preg_match('/---\R(.*?\R)?---(\s+)(.*)/s', $rawFileContents, $fileStructure); |
|
| 261 | |||
| 262 | 83 | if (count($fileStructure) != 4) |
|
| 263 | { |
||
| 264 | 9 | throw new InvalidSyntaxException('Invalid FrontMatter file', 0, null, $this->getRelativeFilePath()); |
|
| 265 | } |
||
| 266 | |||
| 267 | 74 | if (empty(trim($fileStructure[3]))) |
|
| 268 | { |
||
| 269 | 1 | throw new InvalidSyntaxException('FrontMatter files must have a body to render', 0, null, $this->getRelativeFilePath()); |
|
| 270 | } |
||
| 271 | |||
| 272 | 73 | $this->lineOffset = substr_count($fileStructure[1], "\n") + substr_count($fileStructure[2], "\n"); |
|
| 273 | 73 | $this->bodyContent = $fileStructure[3]; |
|
| 274 | |||
| 275 | 73 | if (!empty(trim($fileStructure[1]))) |
|
| 276 | { |
||
| 277 | 60 | $this->frontMatter = Yaml::parse($fileStructure[1], Yaml::PARSE_DATETIME); |
|
| 278 | |||
| 279 | 60 | if (!empty($this->frontMatter) && !is_array($this->frontMatter)) |
|
| 280 | { |
||
| 281 | 60 | throw new ParseException('The evaluated FrontMatter should be an array'); |
|
| 282 | } |
||
| 283 | } |
||
| 284 | else |
||
| 285 | { |
||
| 286 | 13 | $this->frontMatter = array(); |
|
| 287 | } |
||
| 288 | |||
| 289 | 72 | $this->frontMatterEvaluated = false; |
|
| 290 | 72 | $this->bodyContentEvaluated = false; |
|
| 291 | 72 | $this->permalink = null; |
|
| 292 | |||
| 293 | 72 | $this->findTwigDataDependencies('collections'); |
|
| 294 | 72 | $this->findTwigDataDependencies('data'); |
|
| 295 | 72 | } |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get all of the references to either DataItems or ContentItems inside of given string |
||
| 299 | * |
||
| 300 | * @param string $filter 'collections' or 'data' |
||
| 301 | */ |
||
| 302 | private function findTwigDataDependencies ($filter) |
||
| 311 | |||
| 312 | // |
||
| 313 | // Permalink and redirect functionality |
||
| 314 | // |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the permalink of this Content Item |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | * @throws \Exception |
||
| 321 | */ |
||
| 322 | final public function getPermalink () |
||
| 323 | { |
||
| 324 | 18 | if (!is_null($this->permalink)) |
|
| 325 | { |
||
| 326 | 3 | return $this->permalink; |
|
| 327 | } |
||
| 328 | |||
| 329 | 18 | if (!is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion()) |
|
| 330 | { |
||
| 331 | throw new \Exception('The permalink for this item has not been set'); |
||
| 332 | } |
||
| 333 | |||
| 334 | 18 | $permalink = (is_array($this->frontMatter) && isset($this->frontMatter['permalink'])) ? |
|
| 335 | 18 | $this->frontMatter['permalink'] : $this->getPathPermalink(); |
|
| 336 | |||
| 337 | 18 | if (is_array($permalink)) |
|
| 338 | { |
||
| 339 | 6 | $this->permalink = $permalink[0]; |
|
| 340 | 6 | array_shift($permalink); |
|
| 341 | 6 | $this->redirects = $permalink; |
|
| 342 | } |
||
| 343 | else |
||
| 344 | { |
||
| 345 | 12 | $this->permalink = $permalink; |
|
| 346 | 12 | $this->redirects = array(); |
|
| 347 | } |
||
| 348 | |||
| 349 | 18 | $this->permalink = $this->sanitizePermalink($this->permalink); |
|
| 350 | 18 | $this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink); |
|
| 351 | 18 | $this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific |
|
| 352 | |||
| 353 | 18 | return $this->permalink; |
|
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get an array of URLs that will redirect to |
||
| 358 | * |
||
| 359 | * @return string[] |
||
| 360 | */ |
||
| 361 | final public function getRedirects () |
||
| 362 | { |
||
| 363 | 3 | if (is_null($this->redirects)) |
|
| 364 | { |
||
| 365 | 1 | $this->getPermalink(); |
|
| 366 | } |
||
| 367 | |||
| 368 | 3 | return $this->redirects; |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
| 373 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | private function getPathPermalink () |
||
| 378 | { |
||
| 379 | // Remove the protocol of the path, if there is one and prepend a '/' to the beginning |
||
| 380 | 3 | $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath()); |
|
| 381 | 3 | $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR); |
|
| 382 | |||
| 383 | // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator |
||
| 384 | 3 | if (DIRECTORY_SEPARATOR !== '/') |
|
| 385 | { |
||
| 386 | $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath); |
||
| 387 | } |
||
| 388 | |||
| 389 | // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx |
||
| 390 | 3 | $folders = explode(DIRECTORY_SEPARATOR, $cleanPath); |
|
| 391 | |||
| 392 | 3 | if (substr($folders[0], 0, 1) === '_') |
|
| 393 | { |
||
| 394 | 1 | array_shift($folders); |
|
| 395 | } |
||
| 396 | |||
| 397 | 3 | $cleanPath = implode(DIRECTORY_SEPARATOR, $folders); |
|
| 398 | |||
| 399 | 3 | return $cleanPath; |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens |
||
| 404 | * |
||
| 405 | * @param string $permalink A permalink |
||
| 406 | * |
||
| 407 | * @return string $permalink The sanitized permalink |
||
| 408 | */ |
||
| 409 | private function sanitizePermalink ($permalink) |
||
| 410 | { |
||
| 411 | // Remove multiple '/' together |
||
| 412 | 18 | $permalink = preg_replace('/\/+/', '/', $permalink); |
|
| 413 | |||
| 414 | // Replace all spaces with hyphens |
||
| 415 | 18 | $permalink = str_replace(' ', '-', $permalink); |
|
| 416 | |||
| 417 | // Remove all disallowed characters |
||
| 418 | 18 | $permalink = preg_replace('/[^0-9a-zA-Z-_\/\\\.]/', '', $permalink); |
|
| 419 | |||
| 420 | // Handle unnecessary extensions |
||
| 421 | 18 | $extensionsToStrip = array('twig'); |
|
| 422 | |||
| 423 | 18 | if (in_array($this->fs->getExtension($permalink), $extensionsToStrip)) |
|
| 424 | { |
||
| 425 | 4 | $permalink = $this->fs->removeExtension($permalink); |
|
| 426 | } |
||
| 427 | |||
| 428 | // Remove any special characters before a sane value |
||
| 429 | 18 | $permalink = preg_replace('/^[^0-9a-zA-Z-_]*/', '', $permalink); |
|
| 430 | |||
| 431 | // Convert permalinks to lower case |
||
| 432 | 18 | $permalink = mb_strtolower($permalink, 'UTF-8'); |
|
| 433 | |||
| 434 | 18 | return $permalink; |
|
| 435 | } |
||
| 436 | |||
| 437 | // |
||
| 438 | // WritableFrontMatter Implementation |
||
| 439 | // |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | final public function evaluateFrontMatter ($variables = null) |
||
| 445 | { |
||
| 446 | 2 | if (!is_null($variables)) |
|
| 447 | { |
||
| 448 | 2 | $this->frontMatter = array_merge($this->frontMatter, $variables); |
|
| 449 | 2 | $this->evaluateYaml($this->frontMatter); |
|
| 450 | } |
||
| 451 | 2 | } |
|
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | final public function getFrontMatter ($evaluateYaml = true) |
||
| 457 | { |
||
| 458 | 12 | if (is_null($this->frontMatter)) |
|
| 459 | { |
||
| 460 | $this->frontMatter = array(); |
||
| 461 | } |
||
| 462 | 12 | else if (!$this->frontMatterEvaluated && $evaluateYaml) |
|
| 463 | { |
||
| 464 | 12 | $this->evaluateYaml($this->frontMatter); |
|
| 465 | } |
||
| 466 | |||
| 467 | 11 | return $this->frontMatter; |
|
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | final public function hasExpandedFrontMatter () |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc |
||
| 480 | */ |
||
| 481 | final public function appendFrontMatter (array $frontMatter) |
||
| 482 | { |
||
| 483 | foreach ($frontMatter as $key => $value) |
||
| 484 | { |
||
| 485 | $this->writableFrontMatter[$key] = $value; |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc |
||
| 491 | */ |
||
| 492 | final public function deleteFrontMatter ($key) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc |
||
| 501 | */ |
||
| 502 | final public function setFrontMatter (array $frontMatter) |
||
| 503 | { |
||
| 504 | 1 | if (!is_array($frontMatter)) |
|
| 505 | { |
||
| 506 | throw new \InvalidArgumentException('An array is required for setting the writable FrontMatter'); |
||
| 507 | } |
||
| 508 | |||
| 509 | 1 | $this->writableFrontMatter = $frontMatter; |
|
| 510 | 1 | } |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
| 514 | * |
||
| 515 | * @param array $yaml An array of data containing FrontMatter variables |
||
| 516 | * |
||
| 517 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
| 518 | */ |
||
| 519 | private function evaluateYaml (&$yaml) |
||
| 524 | |||
| 525 | // |
||
| 526 | // ArrayAccess Implementation |
||
| 527 | // |
||
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritdoc} |
||
| 531 | */ |
||
| 532 | public function offsetSet ($offset, $value) |
||
| 533 | { |
||
| 534 | if (is_null($offset)) |
||
| 535 | { |
||
| 536 | throw new \InvalidArgumentException('$offset cannot be null'); |
||
| 537 | } |
||
| 538 | |||
| 539 | $this->writableFrontMatter[$offset] = $value; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * {@inheritdoc} |
||
| 544 | */ |
||
| 545 | public function offsetExists ($offset) |
||
| 546 | { |
||
| 547 | 26 | if (isset($this->writableFrontMatter[$offset]) || isset($this->frontMatter[$offset])) |
|
| 548 | { |
||
| 549 | 25 | return true; |
|
| 550 | } |
||
| 551 | |||
| 552 | 8 | $fxnCall = 'get' . ucfirst($offset); |
|
| 553 | 8 | return method_exists($this, $fxnCall) && in_array($fxnCall, static::$whiteListFunctions); |
|
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritdoc} |
||
| 558 | */ |
||
| 559 | public function offsetUnset ($offset) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * {@inheritdoc} |
||
| 566 | */ |
||
| 567 | public function offsetGet ($offset) |
||
| 568 | { |
||
| 569 | 36 | $fxnCall = 'get' . ucfirst($offset); |
|
| 570 | |||
| 571 | 36 | if (in_array($fxnCall, self::$whiteListFunctions) && method_exists($this, $fxnCall)) |
|
| 572 | { |
||
| 573 | 4 | return call_user_func_array(array($this, $fxnCall), array()); |
|
| 574 | } |
||
| 575 | |||
| 576 | 33 | if (isset($this->writableFrontMatter[$offset])) |
|
| 588 | } |
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..