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 |
||
| 25 | class AdviceMatcher |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Flag to enable/disable support of global function interception |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $isInterceptFunctions; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * |
||
| 37 | * @param bool $isInterceptFunctions Optional flag to enable function interception |
||
| 38 | */ |
||
| 39 | 5 | public function __construct(bool $isInterceptFunctions = false) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Returns list of function advices for namespace |
||
| 46 | * |
||
| 47 | * @param Aop\Advisor[] $advisors List of advisor to match |
||
| 48 | * |
||
| 49 | * @return Aop\Advice[] List of advices for class |
||
| 50 | */ |
||
| 51 | 8 | public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors): array |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Return list of advices for class |
||
| 78 | * |
||
| 79 | * @param array|Aop\Advisor[] $advisors List of advisor to match |
||
| 80 | * |
||
| 81 | * @return Aop\Advice[] List of advices for class |
||
| 82 | */ |
||
| 83 | 4 | public function getAdvicesForClass(ReflectionClass $class, array $advisors): array |
|
| 84 | { |
||
| 85 | 4 | $classAdvices = []; |
|
| 86 | 4 | $parentClass = $class->getParentClass(); |
|
| 87 | |||
| 88 | 4 | $originalClass = $class; |
|
| 89 | 4 | if ($parentClass && strpos($parentClass->name, AspectContainer::AOP_PROXIED_SUFFIX) !== false) { |
|
| 90 | $originalClass = $parentClass; |
||
| 91 | } |
||
| 92 | |||
| 93 | 4 | foreach ($advisors as $advisorId => $advisor) { |
|
| 94 | 3 | if ($advisor instanceof Aop\PointcutAdvisor) { |
|
| 95 | 3 | $pointcut = $advisor->getPointcut(); |
|
| 96 | 3 | if ($pointcut->getClassFilter()->matches($class)) { |
|
| 97 | 3 | $classAdvices[] = $this->getAdvicesFromAdvisor($originalClass, $advisor, $advisorId, $pointcut); |
|
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | 3 | if ($advisor instanceof Aop\IntroductionAdvisor) { |
|
| 102 | if ($advisor->getClassFilter()->matches($class)) { |
||
| 103 | $classAdvices[] = $this->getIntroductionFromAdvisor($originalClass, $advisor, $advisorId); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | 4 | if (count($classAdvices) > 0) { |
|
| 108 | 3 | $classAdvices = array_merge_recursive(...$classAdvices); |
|
| 109 | } |
||
| 110 | |||
| 111 | 4 | return $classAdvices; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns list of advices from advisor and point filter |
||
| 116 | */ |
||
| 117 | 3 | private function getAdvicesFromAdvisor( |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Returns list of introduction advices from advisor |
||
| 172 | */ |
||
| 173 | private function getIntroductionFromAdvisor( |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns list of function advices for specific namespace |
||
| 204 | */ |
||
| 205 | private function getFunctionAdvicesFromAdvisor( |
||
| 227 | } |
||
| 228 |