Complex classes like AbstractProxy 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 AbstractProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class AbstractProxy |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Indent for source code |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $indent = 4; |
||
30 | |||
31 | /** |
||
32 | * List of advices that are used for generation of child |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $advices = []; |
||
37 | |||
38 | /** |
||
39 | * PHP expression string for accessing LSB information |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected static $staticLsbExpression = 'static::class'; |
||
44 | |||
45 | /** |
||
46 | * Constructs an abstract proxy class |
||
47 | * |
||
48 | * @param array $advices List of advices |
||
49 | */ |
||
50 | 4 | public function __construct(array $advices = []) |
|
51 | { |
||
52 | 4 | $this->advices = $this->flattenAdvices($advices); |
|
53 | 4 | } |
|
54 | |||
55 | /** |
||
56 | * Returns text representation of class |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | abstract public function __toString(); |
||
61 | |||
62 | /** |
||
63 | * Indent block of code |
||
64 | * |
||
65 | * @param string $text Non-indented text |
||
66 | * |
||
67 | * @return string Indented text |
||
68 | */ |
||
69 | 4 | protected function indent($text) |
|
78 | |||
79 | /** |
||
80 | * Returns list of string representation of parameters |
||
81 | * |
||
82 | * @param array|ReflectionParameter[] $parameters List of parameters |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 4 | protected function getParameters(array $parameters) |
|
95 | |||
96 | /** |
||
97 | * Return string representation of parameter |
||
98 | * |
||
99 | * @param ReflectionParameter $parameter Reflection parameter |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 2 | protected function getParameterCode(ReflectionParameter $parameter) |
|
143 | |||
144 | /** |
||
145 | * Replace concrete advices with list of ids |
||
146 | * |
||
147 | * @param array $advices |
||
148 | * |
||
149 | * @return array flatten list of advices |
||
150 | */ |
||
151 | 4 | private function flattenAdvices(array $advices) |
|
164 | |||
165 | /** |
||
166 | * Prepares a line with args from the method definition |
||
167 | * |
||
168 | * @param ReflectionFunctionAbstract $functionLike |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 4 | protected function prepareArgsLine(ReflectionFunctionAbstract $functionLike) |
|
200 | |||
201 | /** |
||
202 | * Creates a function code from Reflection |
||
203 | * |
||
204 | * @param ReflectionFunctionAbstract $functionLike Reflection for method |
||
205 | * @param string $body Body of method |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 4 | protected function getOverriddenFunction(ReflectionFunctionAbstract $functionLike, $body) |
|
244 | } |
||
245 |