Complex classes like Frontmatter 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 Frontmatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Frontmatter implements FilterInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var array $config with options on how to parse text. |
||
14 | */ |
||
15 | protected $config; |
||
16 | |||
17 | |||
18 | |||
19 | /** |
||
20 | * @var array $lines text split into lines. |
||
21 | */ |
||
22 | protected $lines; |
||
23 | |||
24 | |||
25 | |||
26 | /** |
||
27 | * @var array $lineNumber current line number being parsed. |
||
28 | */ |
||
29 | protected $lineNumber; |
||
30 | |||
31 | |||
32 | |||
33 | /** |
||
34 | * @var integer $linesRemoved keep track on how many lines is removed. |
||
35 | */ |
||
36 | protected $linesRemoved; |
||
37 | |||
38 | |||
39 | |||
40 | /** |
||
41 | * @var array $frontmatter parsed as frontmatter, accumulated when parsing |
||
42 | * the text. |
||
43 | */ |
||
44 | protected $frontmatter; |
||
45 | |||
46 | |||
47 | |||
48 | /** |
||
49 | * @var array $blockTypes with details on block to detect. |
||
50 | */ |
||
51 | protected $blockTypes = [ |
||
52 | "#" => ["Include"], |
||
53 | "-" => ["YamlFrontmatter"], |
||
54 | "{" => ["JsonFrontmatter"], |
||
55 | ]; |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Parse the text through the filter and do what the filter does, |
||
61 | * return the resulting text and with some optional additional details, |
||
62 | * all wrapped in an key-value array. |
||
63 | * |
||
64 | * @param string $text to parse. |
||
65 | * @param array $frontmatter optional to use while parsing. |
||
66 | * @param array $options custom options to use while parsing. |
||
67 | * |
||
68 | * @return array with the resulting text and frontmatter. |
||
69 | */ |
||
70 | 36 | public function parse($text, array $frontmatter, array $options = []) |
|
97 | |||
98 | |||
99 | |||
100 | // /** |
||
101 | // * Debugging and visualising parsing by printing information on current row. |
||
102 | // * |
||
103 | // * @param string $msg additional message to print. |
||
104 | // */ |
||
105 | // private function log($msg) |
||
106 | // { |
||
107 | // $lineNumber = $this->lineNumber + $this->linesRemoved; |
||
108 | // $line = $this->currentLine(); |
||
109 | // |
||
110 | // echo <<<EOD |
||
111 | // |
||
112 | // ***{$lineNumber}: $msg |
||
113 | // {$line} |
||
114 | // *** |
||
115 | // |
||
116 | // EOD; |
||
117 | // } |
||
118 | |||
119 | |||
120 | |||
121 | /** |
||
122 | * Parse each line and look into it to see whats need to be done. |
||
123 | * |
||
124 | * @return array with the resulting text and optional additional items. |
||
125 | */ |
||
126 | 34 | public function parseLines() |
|
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * Get current line to parse. |
||
168 | * |
||
169 | * @return string|boolean containing text for current row or false when |
||
170 | * reached EOF. |
||
171 | */ |
||
172 | 34 | public function currentLine() |
|
178 | |||
179 | |||
180 | |||
181 | /** |
||
182 | * Get next line to parse and keep track on current line being parsed. |
||
183 | * |
||
184 | * @return string|boolean containing text for next row or false when |
||
185 | * reached EOF. |
||
186 | */ |
||
187 | 34 | public function nextLine() |
|
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * Detect and include external file, add it to lines array and parse it. |
||
197 | * |
||
198 | * @param string $line to begin parsing. |
||
199 | * |
||
200 | * @return boolean|void true when block is found and parsed, else void. |
||
201 | */ |
||
202 | 7 | protected function blockInclude($line) |
|
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Detect and extract block with YAML frontmatter from the lines array. |
||
232 | * |
||
233 | * @param string $line to begin parsing. |
||
234 | * |
||
235 | * @return boolean|void true when block is found and parsed, else void. |
||
236 | * |
||
237 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
238 | */ |
||
239 | 20 | protected function blockYamlFrontmatter($line) |
|
281 | |||
282 | |||
283 | |||
284 | /** |
||
285 | * Extract YAML frontmatter from text and merge into existing frontmatter. |
||
286 | * |
||
287 | * @param array $lines the YAML to parsed. |
||
288 | * |
||
289 | * @return void. |
||
290 | */ |
||
291 | 18 | protected function addYamlFrontmatter($lines) |
|
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * Parse YAML front matter from text, use one of several available |
||
307 | * implementations of a YAML parser. |
||
308 | * |
||
309 | * @param string $text the YAML to parsed. |
||
310 | * |
||
311 | * @return void. |
||
312 | * |
||
313 | * @throws Exception when parsing frontmatter fails of is not installed. |
||
314 | */ |
||
315 | 18 | protected function parseYaml($text) |
|
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * Detect and extract block with JSON frontmatter from the lines array. |
||
352 | * |
||
353 | * @param string $line to begin parsing. |
||
354 | * |
||
355 | * @return boolean|void true when block is found and parsed, else void. |
||
356 | * |
||
357 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
358 | */ |
||
359 | 13 | protected function blockJsonFrontmatter($line) |
|
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * Extract JSON frontmatter from text and merge into existing frontmatter. |
||
404 | * |
||
405 | * @param array $lines the JSON to parsed. |
||
406 | * |
||
407 | * @return void. |
||
408 | */ |
||
409 | 11 | protected function addJsonFrontmatter($lines) |
|
428 | } |
||
429 |
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..