Total Complexity | 41 |
Total Lines | 148 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like EnrichedReflector 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.
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 EnrichedReflector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class EnrichedReflector |
||
18 | { |
||
19 | private Reflector $reflector; |
||
20 | private SymbolsConfiguration $symbolsConfiguration; |
||
21 | |||
22 | public function __construct( |
||
23 | Reflector $reflector, |
||
24 | SymbolsConfiguration $symbolsConfiguration |
||
25 | ) { |
||
26 | $this->reflector = $reflector; |
||
27 | $this->symbolsConfiguration = $symbolsConfiguration; |
||
28 | } |
||
29 | |||
30 | public function belongsToExcludedNamespace(string $name): bool |
||
31 | { |
||
32 | return $this->symbolsConfiguration |
||
33 | ->getExcludedNamespaces() |
||
34 | ->belongsToRegisteredNamespace($name); |
||
35 | } |
||
36 | |||
37 | private function belongsToExposedNamespace(string $name): bool |
||
38 | { |
||
39 | return $this->symbolsConfiguration |
||
40 | ->getExposedNamespaces() |
||
41 | ->belongsToRegisteredNamespace($name); |
||
42 | } |
||
43 | |||
44 | public function isFunctionInternal(string $name): bool |
||
47 | } |
||
48 | |||
49 | public function isFunctionExcluded(string $name): bool |
||
50 | { |
||
51 | return $this->reflector->isFunctionInternal($name) |
||
52 | || $this->belongsToExcludedNamespace($name); |
||
53 | } |
||
54 | |||
55 | public function isClassInternal(string $name): bool |
||
56 | { |
||
57 | return $this->reflector->isClassInternal($name); |
||
58 | } |
||
59 | |||
60 | public function isClassExcluded(string $name): bool |
||
61 | { |
||
62 | return $this->reflector->isClassInternal($name) |
||
63 | || $this->belongsToExcludedNamespace($name); |
||
64 | } |
||
65 | |||
66 | public function isConstantInternal(string $name): bool |
||
67 | { |
||
68 | return $this->reflector->isConstantInternal($name); |
||
69 | } |
||
70 | |||
71 | public function isConstantExcluded(string $name): bool |
||
72 | { |
||
73 | // TODO: double check not sure that internal should mean excluded for constants |
||
74 | return $this->reflector->isConstantInternal($name) |
||
75 | || $this->belongsToExcludedNamespace($name); |
||
76 | } |
||
77 | |||
78 | public function isExposedFunction(string $resolvedName): bool |
||
79 | { |
||
80 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
81 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
82 | && ( |
||
83 | $this->_isExposedFunctionFromGlobalNamespace($resolvedName) |
||
84 | || $this->symbolsConfiguration |
||
85 | ->getExposedFunctions() |
||
86 | ->matches($resolvedName) |
||
87 | || $this->belongsToExposedNamespace($resolvedName) |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool |
||
92 | { |
||
93 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
94 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
95 | && $this->_isExposedFunctionFromGlobalNamespace($resolvedName); |
||
96 | } |
||
97 | |||
98 | public function isExposedClass(string $resolvedName): bool |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | public function isExposedClassFromGlobalNamespace(string $resolvedName): bool |
||
112 | { |
||
113 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
114 | && !$this->reflector->isClassInternal($resolvedName) |
||
115 | && $this->_isExposedClassFromGlobalNamespace($resolvedName); |
||
116 | } |
||
117 | |||
118 | public function isExposedConstant(string $name): bool |
||
119 | { |
||
120 | // Special case: internal constants must be treated as exposed symbols. |
||
121 | // |
||
122 | // Example: when declaring a new internal constant for compatibility |
||
123 | // reasons, it must remain un-prefixed. |
||
124 | return !$this->belongsToExcludedNamespace($name) |
||
125 | && ( |
||
126 | $this->reflector->isConstantInternal($name) |
||
127 | || $this->isExposedConstantFromGlobalNamespace($name) |
||
128 | || $this->symbolsConfiguration |
||
129 | ->getExposedConstants() |
||
130 | ->matches($name) |
||
131 | || $this->belongsToExposedNamespace($name) |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | public function isExposedConstantFromGlobalNamespace(string $constantName): bool |
||
136 | { |
||
137 | // TODO: leverage belongsToGlobalNamespace |
||
138 | return $this->symbolsConfiguration->shouldExposeGlobalConstants() && !strpos($constantName, '\\'); |
||
139 | } |
||
140 | |||
141 | public function isExcludedNamespace(string $name): bool |
||
146 | } |
||
147 | |||
148 | private function _isExposedFunctionFromGlobalNamespace(string $functionName): bool |
||
149 | { |
||
150 | // TODO: leverage belongsToGlobalNamespace |
||
151 | return $this->symbolsConfiguration->shouldExposeGlobalFunctions() && !strpos($functionName, '\\'); |
||
152 | } |
||
153 | |||
154 | public function _isExposedClassFromGlobalNamespace(string $className): bool |
||
155 | { |
||
156 | // TODO: leverage belongsToGlobalNamespace |
||
157 | return $this->symbolsConfiguration->shouldExposeGlobalClasses() && !strpos($className, '\\'); |
||
158 | } |
||
159 | |||
160 | public function belongsToGlobalNamespace(string $symbolName): bool |
||
165 | ); |
||
166 | } |
||
167 | } |
||
168 |