Complex classes like Document 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 Document, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class Document implements WritableDocumentInterface, JailedDocumentInterface, \ArrayAccess |
||
|
|||
22 | { |
||
23 | const TEMPLATE = "---\n%s\n---\n\n%s"; |
||
24 | |||
25 | /** |
||
26 | * The names of FrontMatter keys that are specially defined for all Documents |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | public static $specialFrontMatterKeys = array( |
||
31 | 'filename', 'basename' |
||
32 | ); |
||
33 | |||
34 | protected static $whiteListFunctions = array( |
||
35 | 'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
||
36 | 'getExtension', 'getFrontMatter' |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * An array to keep track of collection or data dependencies used inside of a Twig template. |
||
41 | * |
||
42 | * $dataDependencies['collections'] = array() |
||
43 | * $dataDependencies['data'] = array() |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $dataDependencies; |
||
48 | |||
49 | /** |
||
50 | * FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
||
51 | * precedence over values in $frontMatter. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $writableFrontMatter; |
||
56 | |||
57 | /** |
||
58 | * A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
||
59 | * here have dedicated functions that handle those Front Matter values and the respective functions should be called |
||
60 | * instead. |
||
61 | * |
||
62 | * @var string[] |
||
63 | */ |
||
64 | protected $frontMatterBlacklist; |
||
65 | |||
66 | /** |
||
67 | * Set to true if the front matter has already been evaluated with variable interpolation. |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $frontMatterEvaluated; |
||
72 | |||
73 | /** |
||
74 | * @var Parser |
||
75 | */ |
||
76 | protected $frontMatterParser; |
||
77 | |||
78 | /** |
||
79 | * An array containing the Yaml of the file. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $frontMatter; |
||
84 | |||
85 | /** |
||
86 | * Set to true if the body has already been parsed as markdown or any other format. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | protected $bodyContentEvaluated; |
||
91 | |||
92 | /** |
||
93 | * Only the body of the file, i.e. the content. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $bodyContent; |
||
98 | |||
99 | /** |
||
100 | * The permalink for this object. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $permalink; |
||
105 | |||
106 | /** |
||
107 | * A filesystem object. |
||
108 | * |
||
109 | * @var Filesystem |
||
110 | */ |
||
111 | protected $fs; |
||
112 | |||
113 | /** |
||
114 | * The extension of the file. |
||
115 | * |
||
116 | * @var string |
||
117 | */ |
||
118 | private $extension; |
||
119 | |||
120 | /** |
||
121 | * The number of lines that Twig template errors should offset. |
||
122 | * |
||
123 | * @var int |
||
124 | */ |
||
125 | private $lineOffset; |
||
126 | |||
127 | /** |
||
128 | * A list URLs that will redirect to this object. |
||
129 | * |
||
130 | * @var string[] |
||
131 | */ |
||
132 | private $redirects; |
||
133 | |||
134 | /** |
||
135 | * The original file path to the ContentItem. |
||
136 | * |
||
137 | * @var SplFileInfo |
||
138 | */ |
||
139 | private $filePath; |
||
140 | |||
141 | /** |
||
142 | * ContentItem constructor. |
||
143 | * |
||
144 | * @param string $filePath The path to the file that will be parsed into a ContentItem |
||
145 | * |
||
146 | * @throws FileNotFoundException The given file path does not exist |
||
147 | * @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
||
148 | * no body |
||
149 | */ |
||
150 | 116 | public function __construct($filePath) |
|
167 | |||
168 | /** |
||
169 | * Return the body of the Content Item. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | abstract public function getContent(); |
||
174 | |||
175 | /** |
||
176 | * Get the extension of the current file. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | final public function getExtension() |
||
184 | |||
185 | /** |
||
186 | * Get the original file path. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | final public function getFilePath() |
||
194 | |||
195 | /** |
||
196 | * The number of lines that are taken up by FrontMatter and white space. |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | final public function getLineOffset() |
||
204 | |||
205 | /** |
||
206 | * Get the name of the item, which is just the filename without the extension. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | final public function getName() |
||
214 | |||
215 | /** |
||
216 | * Get the filename of this document. |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | final public function getFileName() |
||
224 | |||
225 | /** |
||
226 | * Get the relative path to this file relative to the root of the Stakx website. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | final public function getRelativeFilePath() |
||
240 | |||
241 | /** |
||
242 | * Get the destination of where this Content Item would be written to when the website is compiled. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | final public function getTargetFile() |
||
259 | |||
260 | /** |
||
261 | * Check whether this object has a reference to a collection or data item. |
||
262 | * |
||
263 | * @param string $namespace 'collections' or 'data' |
||
264 | * @param string $needle |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | final public function hasTwigDependency($namespace, $needle) |
||
272 | |||
273 | /** |
||
274 | * Read the file, and parse its contents. |
||
275 | */ |
||
276 | final public function refreshFileContent() |
||
328 | |||
329 | /** |
||
330 | * Get all of the references to either DataItems or ContentItems inside of given string. |
||
331 | * |
||
332 | * @param string $filter 'collections' or 'data' |
||
333 | */ |
||
334 | private function findTwigDataDependencies($filter) |
||
343 | |||
344 | // |
||
345 | // Permalink and redirect functionality |
||
346 | // |
||
347 | |||
348 | /** |
||
349 | * Get the permalink of this Content Item. |
||
350 | * |
||
351 | * @throws \Exception |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | final public function getPermalink() |
||
388 | |||
389 | /** |
||
390 | * Get an array of URLs that will redirect to. |
||
391 | * |
||
392 | * @return string[] |
||
393 | */ |
||
394 | final public function getRedirects() |
||
403 | |||
404 | /** |
||
405 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
406 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | private function getPathPermalink() |
||
434 | |||
435 | /** |
||
436 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens. |
||
437 | * |
||
438 | * @param string $permalink A permalink |
||
439 | * |
||
440 | * @return string $permalink The sanitized permalink |
||
441 | */ |
||
442 | private function sanitizePermalink($permalink) |
||
469 | |||
470 | // |
||
471 | // WritableFrontMatter Implementation |
||
472 | // |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | final public function evaluateFrontMatter($variables = null) |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | final public function getFrontMatter($evaluateYaml = true) |
||
502 | |||
503 | /** |
||
504 | * {@inheritdoc} |
||
505 | */ |
||
506 | final public function hasExpandedFrontMatter() |
||
510 | |||
511 | /** |
||
512 | * {@inheritdoc. |
||
513 | */ |
||
514 | final public function appendFrontMatter(array $frontMatter) |
||
521 | |||
522 | /** |
||
523 | * {@inheritdoc. |
||
524 | */ |
||
525 | final public function deleteFrontMatter($key) |
||
534 | |||
535 | /** |
||
536 | * {@inheritdoc. |
||
537 | */ |
||
538 | final public function setFrontMatter(array $frontMatter) |
||
547 | |||
548 | /** |
||
549 | * Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
||
550 | * |
||
551 | * @param array $yaml An array of data containing FrontMatter variables |
||
552 | * |
||
553 | * @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
||
554 | */ |
||
555 | private function evaluateYaml(&$yaml) |
||
570 | |||
571 | // |
||
572 | // ArrayAccess Implementation |
||
573 | // |
||
574 | |||
575 | /** |
||
576 | * {@inheritdoc} |
||
577 | */ |
||
578 | public function offsetSet($offset, $value) |
||
587 | |||
588 | /** |
||
589 | * {@inheritdoc} |
||
590 | */ |
||
591 | public function offsetExists($offset) |
||
602 | |||
603 | /** |
||
604 | * {@inheritdoc} |
||
605 | */ |
||
606 | public function offsetUnset($offset) |
||
610 | |||
611 | /** |
||
612 | * {@inheritdoc} |
||
613 | */ |
||
614 | public function offsetGet($offset) |
||
635 | } |
||
636 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.