Complex classes like AdviceMatcher 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 AdviceMatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class AdviceMatcher |
||
25 | { |
||
26 | /** |
||
27 | * Loader of aspects |
||
28 | * |
||
29 | * @var AspectLoader |
||
30 | */ |
||
31 | protected $loader; |
||
32 | |||
33 | /** |
||
34 | * Flag to enable/disable support of global function interception |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $isInterceptFunctions; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param AspectLoader $loader Instance of aspect loader |
||
44 | * @param bool $isInterceptFunctions Optional flag to enable function interception |
||
45 | */ |
||
46 | 4 | public function __construct(AspectLoader $loader, bool $isInterceptFunctions = false) |
|
52 | |||
53 | /** |
||
54 | * Returns list of function advices for namespace |
||
55 | * |
||
56 | * @param ReflectionFileNamespace $namespace |
||
57 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | 7 | public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors): array |
|
85 | |||
86 | /** |
||
87 | * Return list of advices for class |
||
88 | * |
||
89 | * @param ReflectionClass $class Class to advise |
||
90 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
91 | * |
||
92 | * @return array|Aop\Advice[] List of advices for class |
||
93 | */ |
||
94 | 3 | public function getAdvicesForClass(ReflectionClass $class, array $advisors): array |
|
124 | |||
125 | /** |
||
126 | * Returns list of advices from advisor and point filter |
||
127 | * |
||
128 | * @param ReflectionClass $class Class to inject advices |
||
129 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
130 | * @param string $advisorId Identifier of advisor |
||
131 | * @param Aop\PointFilter $filter Filter for points |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | 2 | private function getAdvicesFromAdvisor( |
|
187 | |||
188 | /** |
||
189 | * Returns list of introduction advices from advisor |
||
190 | * |
||
191 | * @param ReflectionClass $class Class to inject advices |
||
192 | * @param Aop\IntroductionAdvisor $advisor Advisor for class |
||
193 | * @param string $advisorId Identifier of advisor |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | private function getIntroductionFromAdvisor( |
||
225 | |||
226 | /** |
||
227 | * Returns list of function advices for specific namespace |
||
228 | * |
||
229 | * @param ReflectionFileNamespace $namespace |
||
230 | * @param Aop\PointcutAdvisor $advisor Advisor for class |
||
231 | * @param string $advisorId Identifier of advisor |
||
232 | * @param Aop\PointFilter $pointcut Filter for points |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | private function getFunctionAdvicesFromAdvisor( |
||
258 | } |
||
259 |