Complex classes like Configurator 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 Configurator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Configurator extends ConfiguratorBase |
||
24 | { |
||
25 | /** |
||
26 | * @var array[] Captures from current regexp |
||
27 | */ |
||
28 | protected $captures; |
||
29 | |||
30 | /** |
||
31 | * @var array[] List of [tagName, regexp, passthroughIdx] |
||
32 | */ |
||
33 | protected $collection = []; |
||
34 | |||
35 | /** |
||
36 | * @var string Delimiter used in current regexp |
||
37 | */ |
||
38 | protected $delimiter; |
||
39 | |||
40 | /** |
||
41 | * @var string Non-D modifiers used in current regexp |
||
42 | */ |
||
43 | protected $modifiers; |
||
44 | |||
45 | /** |
||
46 | * @var array References used in current template |
||
47 | */ |
||
48 | protected $references; |
||
49 | |||
50 | /** |
||
51 | * @var string Regexp used to find references in the templates. We check that the reference is |
||
52 | * not preceded with an odd number of backslashes |
||
53 | */ |
||
54 | protected $referencesRegexp = '((?<!\\\\)(?:\\\\\\\\)*\\K(?:[$\\\\]\\d+|\\$\\{\\d+\\}))S'; |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 6 | public function asConfig() |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 2 | public function getJSHints() |
|
93 | |||
94 | /** |
||
95 | * Configure a pattern-based match |
||
96 | * |
||
97 | * @param string $regexp Regexp to be used by the parser |
||
98 | * @param string $tagName Name of the tag that holds the matched text |
||
99 | * @return void |
||
100 | */ |
||
101 | 1 | public function match($regexp, $tagName) |
|
117 | |||
118 | /** |
||
119 | * Configure a pattern-based replacement |
||
120 | * |
||
121 | * @param string $regexp Regexp to be used by the parser |
||
122 | * @param string $template Template to be used for rendering |
||
123 | * @param string $tagName Name of the tag to create. A name based on the regexp is |
||
124 | * automatically generated if none is provided |
||
125 | * @return Tag The tag created to represent this replacement |
||
126 | */ |
||
127 | 34 | public function replace($regexp, $template, $tagName = null) |
|
149 | |||
150 | /** |
||
151 | * Add given attribute to given tag based on parsed captures |
||
152 | * |
||
153 | * @param Tag $tag |
||
154 | * @param string $attrName |
||
155 | * @return void |
||
156 | */ |
||
157 | 27 | protected function addAttribute(Tag $tag, $attrName) |
|
191 | |||
192 | /** |
||
193 | * Convert a preg-style replacement to a template |
||
194 | * |
||
195 | * @param string $template Original template |
||
196 | * @param integer $passthroughIdx Index of the passthrough capture |
||
197 | * @return string Modified template |
||
198 | */ |
||
199 | 33 | protected function convertTemplate($template, $passthroughIdx) |
|
242 | |||
243 | /** |
||
244 | * Create the tag that matches current regexp |
||
245 | * |
||
246 | * @param string $tagName |
||
247 | * @param string $template |
||
248 | * @return Tag |
||
249 | */ |
||
250 | 33 | protected function createTag($tagName, $template) |
|
278 | |||
279 | /** |
||
280 | * Give a name to unnamed captures that are referenced in current replacement |
||
281 | * |
||
282 | * @param string $regexp Original regexp |
||
283 | * @return string Modified regexp |
||
284 | */ |
||
285 | 33 | protected function fixUnnamedCaptures($regexp) |
|
317 | |||
318 | /** |
||
319 | * Get the index of the capture used for passthrough in current replacement |
||
320 | * |
||
321 | * @return integer |
||
322 | */ |
||
323 | 33 | protected function getPassthroughCapture() |
|
344 | |||
345 | /** |
||
346 | * Parse a regexp and return its info |
||
347 | * |
||
348 | * @param string $regexp |
||
349 | * @return array |
||
350 | */ |
||
351 | 35 | protected function getRegexpInfo($regexp) |
|
360 | |||
361 | /** |
||
362 | * Test whether given expression is a catch-all expression such as .*? |
||
363 | * |
||
364 | * @param string $expr Subpattern |
||
365 | * @return bool |
||
366 | */ |
||
367 | 22 | protected function isCatchAll($expr) |
|
371 | |||
372 | /** |
||
373 | * Parse given regexp and store its information |
||
374 | * |
||
375 | * @param string $regexp |
||
376 | * @return void |
||
377 | */ |
||
378 | 35 | protected function parseRegexp($regexp) |
|
397 | |||
398 | /** |
||
399 | * Parse given template and store the references it contains |
||
400 | * |
||
401 | * @param string $template |
||
402 | * @return void |
||
403 | */ |
||
404 | 33 | protected function parseTemplate($template) |
|
444 | |||
445 | /** |
||
446 | * Remove references that do not correspond to an existing capture |
||
447 | * |
||
448 | * @return void |
||
449 | */ |
||
450 | 33 | protected function removeUnknownReferences() |
|
457 | } |