Complex classes like FrontMatterParser 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 FrontMatterParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class FrontMatterParser |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * The RegEx used to identify Front Matter variables. |
||
| 63 | */ |
||
| 64 | const VARIABLE_DEF = '/(?<!\\\\)%([a-zA-Z]+)/'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The RegEx used to identify special variables. |
||
| 68 | */ |
||
| 69 | const ARRAY_DEF = '/(?<!\\\\)%{([a-zA-Z\.]+)}/'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * A list of special fields in the Front Matter that will support expansion. |
||
| 73 | * |
||
| 74 | * @var string[] |
||
| 75 | */ |
||
| 76 | private static $expandableFields = ['permalink']; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Whether or not an field was expanded into several values. |
||
| 80 | * |
||
| 81 | * Only fields specified in $expandableFields will cause this value to be set to true |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | private $expansionUsed; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The current depth of the recursion for evaluating nested arrays in the Front Matter. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | private $nestingLevel; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Special FrontMatter keys that are defined manually. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $specialKeys; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The current hierarchy of the keys that are being evaluated. |
||
| 103 | * |
||
| 104 | * Since arrays can be nested, we'll keep track of the keys up until the current depth. This information is used for |
||
| 105 | * error reporting |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private $yamlKeys; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The entire Front Matter block; evaluation will happen in place. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | private $frontMatter; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * YAML data that is being imported from external sources. |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | private $complexVariables; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param array $rawFrontMatter The array representation of a document's Front Matter |
||
| 127 | * @param array $specialKeys Front Matter variables defined manually, which will override any values defined |
||
| 128 | * through Front Matter. |
||
| 129 | */ |
||
| 130 | 51 | public function __construct(array &$rawFrontMatter, array $specialKeys = array()) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Make complex variables available to the parser. |
||
| 143 | * |
||
| 144 | * @param array $yaml |
||
| 145 | */ |
||
| 146 | 2 | public function addComplexVariables(array $yaml) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Trigger the parsing functionality. The given array will be evaluated in place. |
||
| 153 | */ |
||
| 154 | 51 | public function parse() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * True if any fields were expanded in the FrontMatter block. |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 24 | public function hasExpansion() |
|
| 169 | |||
| 170 | // |
||
| 171 | // Special FrontMatter fields |
||
| 172 | // |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Special treatment for some FrontMatter variables. |
||
| 176 | */ |
||
| 177 | 51 | private function handleSpecialFrontMatter() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Merge in the special keys with the existing FrontMatter. |
||
| 185 | */ |
||
| 186 | 51 | private function handleSpecialKeys() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Special treatment for the `date` field in FrontMatter that creates three new variables: year, month, day. |
||
| 193 | */ |
||
| 194 | 51 | private function handleDateField() |
|
| 212 | |||
| 213 | // |
||
| 214 | // Evaluation |
||
| 215 | // |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Evaluate an array as Front Matter. |
||
| 219 | * |
||
| 220 | * @param array $yaml |
||
| 221 | */ |
||
| 222 | 51 | private function evaluateBlock(&$yaml) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Evaluate an expandable field. |
||
| 255 | * |
||
| 256 | * @param string $key |
||
| 257 | * @param string $fmStatement |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 27 | private function evaluateExpandableField($key, $fmStatement) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Convert a string or an array into an array of ExpandedValue objects created through "value expansion". |
||
| 290 | * |
||
| 291 | * @param string $frontMatterKey The current hierarchy of the Front Matter keys being used |
||
| 292 | * @param string $expandableValue The Front Matter value that will be expanded |
||
| 293 | * @param string[] $arrayVariableNames The Front Matter variable names that reference arrays |
||
| 294 | * |
||
| 295 | * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 7 | private function evaluateArrayType($frontMatterKey, $expandableValue, $arrayVariableNames) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values. |
||
| 342 | * |
||
| 343 | * @param string $key The key of the Front Matter value |
||
| 344 | * @param string $string The string that will be evaluated |
||
| 345 | * @param bool $ignoreArrays When set to true, an exception won't be thrown when an array is found with the |
||
| 346 | * interpolation |
||
| 347 | * |
||
| 348 | * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string |
||
| 349 | * |
||
| 350 | * @return string The final string with variables evaluated |
||
| 351 | */ |
||
| 352 | 51 | private function evaluateBasicType($key, $string, $ignoreArrays = false) |
|
| 377 | |||
| 378 | // |
||
| 379 | // Variable management |
||
| 380 | // |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get an array of FrontMatter variables in the specified string that need to be interpolated. |
||
| 384 | * |
||
| 385 | * @param string $string |
||
| 386 | * |
||
| 387 | * @return string[] |
||
| 388 | */ |
||
| 389 | 51 | private function findFrontMatterVariables($string) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get the value of a FM variable. |
||
| 404 | * |
||
| 405 | * @param string $key The FM key that is being currently evaluated (used solely for a helpful error message) |
||
| 406 | * @param string $varName The variable name we're searching for without the `%` |
||
| 407 | * |
||
| 408 | * @throws YamlVariableUndefinedException When variable is not defined. |
||
| 409 | * |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | 31 | private function getVariableValue($key, $varName) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get the variable template that needs to be replaced. |
||
| 436 | * |
||
| 437 | * The syntax for primitive variables differ from complex variables, so this method will return the appropriate |
||
| 438 | * template that will be used to replace the value. |
||
| 439 | * |
||
| 440 | * @param string $variableName The variable name |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 25 | private function getVariableTemplate($variableName) |
|
| 450 | |||
| 451 | // |
||
| 452 | // Utility functions |
||
| 453 | // |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $epochTime |
||
| 457 | * |
||
| 458 | * @return bool|\DateTime |
||
| 459 | */ |
||
| 460 | 6 | private function castDateTimeTimezone($epochTime) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * @param $guess |
||
| 471 | * |
||
| 472 | * @return bool|\DateTime |
||
| 473 | */ |
||
| 474 | 7 | private function guessDateTime($guess) |
|
| 494 | } |
||
| 495 |