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 |
||
21 | abstract class FrontMatterObject implements FrontMatterable, Jailable, \ArrayAccess |
||
22 | { |
||
23 | protected static $whiteListFunctions = array( |
||
24 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
25 | 'getExtension', 'getFrontMatter' |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * An array to keep track of collection or data dependencies used inside of a Twig template. |
||
30 | * |
||
31 | * $dataDependencies['collections'] = array() |
||
32 | * $dataDependencies['data'] = array() |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $dataDependencies; |
||
37 | |||
38 | /** |
||
39 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
40 | * precedence over values in $frontMatter. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $writableFrontMatter; |
||
45 | |||
46 | /** |
||
47 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
48 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
49 | * instead. |
||
50 | * |
||
51 | * @var string[] |
||
52 | */ |
||
53 | protected $frontMatterBlacklist; |
||
54 | |||
55 | /** |
||
56 | * Set to true if the front matter has already been evaluated with variable interpolation. |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $frontMatterEvaluated; |
||
61 | |||
62 | /** |
||
63 | * @var FrontMatterParser |
||
64 | */ |
||
65 | protected $frontMatterParser; |
||
66 | |||
67 | /** |
||
68 | * An array containing the Yaml of the file. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $frontMatter; |
||
73 | |||
74 | /** |
||
75 | * Set to true if the body has already been parsed as markdown or any other format. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $bodyContentEvaluated; |
||
80 | |||
81 | /** |
||
82 | * Only the body of the file, i.e. the content. |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $bodyContent; |
||
87 | |||
88 | /** |
||
89 | * The permalink for this object. |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $permalink; |
||
94 | |||
95 | /** |
||
96 | * A filesystem object. |
||
97 | * |
||
98 | * @var Filesystem |
||
99 | */ |
||
100 | protected $fs; |
||
101 | |||
102 | /** |
||
103 | * The extension of the file. |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | private $extension; |
||
108 | |||
109 | /** |
||
110 | * The number of lines that Twig template errors should offset. |
||
111 | * |
||
112 | * @var int |
||
113 | */ |
||
114 | private $lineOffset; |
||
115 | |||
116 | /** |
||
117 | * A list URLs that will redirect to this object. |
||
118 | * |
||
119 | * @var string[] |
||
120 | */ |
||
121 | private $redirects; |
||
122 | |||
123 | /** |
||
124 | * The original file path to the ContentItem. |
||
125 | * |
||
126 | * @var SplFileInfo |
||
127 | */ |
||
128 | private $filePath; |
||
129 | |||
130 | /** |
||
131 | * ContentItem constructor. |
||
132 | * |
||
133 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
134 | * |
||
135 | * @throws FileNotFoundException The given file path does not exist |
||
136 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
137 | * no body |
||
138 | */ |
||
139 | 105 | public function __construct($filePath) |
|
156 | |||
157 | /** |
||
158 | * Return the body of the Content Item. |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | abstract public function getContent(); |
||
163 | |||
164 | /** |
||
165 | * Get the extension of the current file. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | final public function getExtension() |
||
173 | |||
174 | /** |
||
175 | * Get the original file path. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | final public function getFilePath() |
||
183 | |||
184 | /** |
||
185 | * The number of lines that are taken up by FrontMatter and white space. |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | final public function getLineOffset() |
||
193 | |||
194 | /** |
||
195 | * Get the name of the item, which is just the file name without the extension. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | final public function getName() |
||
203 | |||
204 | /** |
||
205 | * Get the relative path to this file relative to the root of the Stakx website. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | final public function getRelativeFilePath() |
||
219 | |||
220 | /** |
||
221 | * Get the destination of where this Content Item would be written to when the website is compiled. |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | final public function getTargetFile() |
||
238 | |||
239 | /** |
||
240 | * Check whether this object has a reference to a collection or data item. |
||
241 | * |
||
242 | * @param string $namespace 'collections' or 'data' |
||
243 | * @param string $needle |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | final public function hasTwigDependency($namespace, $needle) |
||
251 | |||
252 | /** |
||
253 | * Read the file, and parse its contents. |
||
254 | */ |
||
255 | final public function refreshFileContent() |
||
302 | |||
303 | /** |
||
304 | * Get all of the references to either DataItems or ContentItems inside of given string. |
||
305 | * |
||
306 | * @param string $filter 'collections' or 'data' |
||
307 | */ |
||
308 | private function findTwigDataDependencies($filter) |
||
317 | |||
318 | // |
||
319 | // Permalink and redirect functionality |
||
320 | // |
||
321 | |||
322 | /** |
||
323 | * Get the permalink of this Content Item. |
||
324 | * |
||
325 | * @throws \Exception |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | final public function getPermalink() |
||
362 | |||
363 | /** |
||
364 | * Get an array of URLs that will redirect to. |
||
365 | * |
||
366 | * @return string[] |
||
367 | */ |
||
368 | final public function getRedirects() |
||
377 | |||
378 | /** |
||
379 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
380 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | private function getPathPermalink() |
||
408 | |||
409 | /** |
||
410 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens. |
||
411 | * |
||
412 | * @param string $permalink A permalink |
||
413 | * |
||
414 | * @return string $permalink The sanitized permalink |
||
415 | */ |
||
416 | private function sanitizePermalink($permalink) |
||
443 | |||
444 | // |
||
445 | // WritableFrontMatter Implementation |
||
446 | // |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | final public function evaluateFrontMatter($variables = null) |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | final public function getFrontMatter($evaluateYaml = true) |
||
476 | |||
477 | /** |
||
478 | * {@inheritdoc} |
||
479 | */ |
||
480 | final public function hasExpandedFrontMatter() |
||
484 | |||
485 | /** |
||
486 | * {@inheritdoc. |
||
487 | */ |
||
488 | final public function appendFrontMatter(array $frontMatter) |
||
495 | |||
496 | /** |
||
497 | * {@inheritdoc. |
||
498 | */ |
||
499 | final public function deleteFrontMatter($key) |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc. |
||
511 | */ |
||
512 | final public function setFrontMatter(array $frontMatter) |
||
521 | |||
522 | /** |
||
523 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
524 | * |
||
525 | * @param array $yaml An array of data containing FrontMatter variables |
||
526 | * |
||
527 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
528 | */ |
||
529 | private function evaluateYaml(&$yaml) |
||
541 | |||
542 | // |
||
543 | // ArrayAccess Implementation |
||
544 | // |
||
545 | |||
546 | /** |
||
547 | * {@inheritdoc} |
||
548 | */ |
||
549 | public function offsetSet($offset, $value) |
||
558 | |||
559 | /** |
||
560 | * {@inheritdoc} |
||
561 | */ |
||
562 | public function offsetExists($offset) |
||
573 | |||
574 | /** |
||
575 | * {@inheritdoc} |
||
576 | */ |
||
577 | public function offsetUnset($offset) |
||
581 | |||
582 | /** |
||
583 | * {@inheritdoc} |
||
584 | */ |
||
585 | public function offsetGet($offset) |
||
606 | } |
||
607 |
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..