| Total Complexity | 41 |
| Total Lines | 149 |
| 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 |
||
| 27 | final class EnrichedReflector |
||
| 28 | { |
||
| 29 | private Reflector $reflector; |
||
| 30 | private SymbolsConfiguration $symbolsConfiguration; |
||
| 31 | |||
| 32 | public function __construct( |
||
| 33 | Reflector $reflector, |
||
| 34 | SymbolsConfiguration $symbolsConfiguration |
||
| 35 | ) { |
||
| 36 | $this->reflector = $reflector; |
||
| 37 | $this->symbolsConfiguration = $symbolsConfiguration; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function belongsToExcludedNamespace(string $name): bool |
||
| 41 | { |
||
| 42 | return $this->symbolsConfiguration |
||
| 43 | ->getExcludedNamespaces() |
||
| 44 | ->belongsToRegisteredNamespace($name); |
||
| 45 | } |
||
| 46 | |||
| 47 | private function belongsToExposedNamespace(string $name): bool |
||
| 48 | { |
||
| 49 | return $this->symbolsConfiguration |
||
| 50 | ->getExposedNamespaces() |
||
| 51 | ->belongsToRegisteredNamespace($name); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function isFunctionInternal(string $name): bool |
||
| 57 | } |
||
| 58 | |||
| 59 | public function isFunctionExcluded(string $name): bool |
||
| 60 | { |
||
| 61 | return $this->reflector->isFunctionInternal($name) |
||
| 62 | || $this->belongsToExcludedNamespace($name); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function isClassInternal(string $name): bool |
||
| 66 | { |
||
| 67 | return $this->reflector->isClassInternal($name); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function isClassExcluded(string $name): bool |
||
| 71 | { |
||
| 72 | return $this->reflector->isClassInternal($name) |
||
| 73 | || $this->belongsToExcludedNamespace($name); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function isConstantInternal(string $name): bool |
||
| 77 | { |
||
| 78 | return $this->reflector->isConstantInternal($name); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function isConstantExcluded(string $name): bool |
||
| 82 | { |
||
| 83 | // TODO: double check not sure that internal should mean excluded for constants |
||
| 84 | // TODO: review as not used at the moment |
||
| 85 | return $this->reflector->isConstantInternal($name) |
||
| 86 | || $this->belongsToExcludedNamespace($name); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function isExposedFunction(string $resolvedName): bool |
||
| 90 | { |
||
| 91 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 92 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
| 93 | && ( |
||
| 94 | $this->_isExposedFunctionFromGlobalNamespace($resolvedName) |
||
| 95 | || $this->symbolsConfiguration |
||
| 96 | ->getExposedFunctions() |
||
| 97 | ->matches($resolvedName) |
||
| 98 | || $this->belongsToExposedNamespace($resolvedName) |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool |
||
| 103 | { |
||
| 104 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 105 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
| 106 | && $this->_isExposedFunctionFromGlobalNamespace($resolvedName); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function isExposedClass(string $resolvedName): bool |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function isExposedClassFromGlobalNamespace(string $resolvedName): bool |
||
| 123 | { |
||
| 124 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 125 | && !$this->reflector->isClassInternal($resolvedName) |
||
| 126 | && $this->_isExposedClassFromGlobalNamespace($resolvedName); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function isExposedConstant(string $name): bool |
||
| 130 | { |
||
| 131 | // Special case: internal constants must be treated as exposed symbols. |
||
| 132 | // |
||
| 133 | // Example: when declaring a new internal constant for compatibility |
||
| 134 | // reasons, it must remain un-prefixed. |
||
| 135 | return !$this->belongsToExcludedNamespace($name) |
||
| 136 | && ( |
||
| 137 | $this->reflector->isConstantInternal($name) |
||
| 138 | || $this->isExposedConstantFromGlobalNamespace($name) |
||
| 139 | || $this->symbolsConfiguration |
||
| 140 | ->getExposedConstants() |
||
| 141 | ->matches($name) |
||
| 142 | || $this->belongsToExposedNamespace($name) |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function isExposedConstantFromGlobalNamespace(string $constantName): bool |
||
| 147 | { |
||
| 148 | // TODO: leverage belongsToGlobalNamespace |
||
| 149 | return $this->symbolsConfiguration->shouldExposeGlobalConstants() && !strpos($constantName, '\\'); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function isExcludedNamespace(string $name): bool |
||
| 157 | } |
||
| 158 | |||
| 159 | private function _isExposedFunctionFromGlobalNamespace(string $functionName): bool |
||
| 160 | { |
||
| 161 | // TODO: leverage belongsToGlobalNamespace |
||
| 162 | return $this->symbolsConfiguration->shouldExposeGlobalFunctions() && !strpos($functionName, '\\'); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function _isExposedClassFromGlobalNamespace(string $className): bool |
||
| 166 | { |
||
| 167 | // TODO: leverage belongsToGlobalNamespace |
||
| 168 | return $this->symbolsConfiguration->shouldExposeGlobalClasses() && !strpos($className, '\\'); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function belongsToGlobalNamespace(string $symbolName): bool |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | } |
||
| 179 |