| Total Complexity | 45 |
| Total Lines | 192 |
| 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 |
||
| 26 | final class EnrichedReflector |
||
| 27 | { |
||
| 28 | private Reflector $reflector; |
||
| 29 | private SymbolsConfiguration $symbolsConfiguration; |
||
| 30 | |||
| 31 | public function __construct( |
||
| 32 | Reflector $reflector, |
||
| 33 | SymbolsConfiguration $symbolsConfiguration |
||
| 34 | ) { |
||
| 35 | $this->reflector = $reflector; |
||
| 36 | $this->symbolsConfiguration = $symbolsConfiguration; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function belongsToExcludedNamespace(string $name): bool |
||
| 40 | { |
||
| 41 | return $this->symbolsConfiguration |
||
| 42 | ->getExcludedNamespaces() |
||
| 43 | ->belongsToRegisteredNamespace($name); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function isFunctionInternal(string $name): bool |
||
| 49 | } |
||
| 50 | |||
| 51 | public function isFunctionExcluded(string $name): bool |
||
| 52 | { |
||
| 53 | return $this->reflector->isFunctionInternal($name) |
||
| 54 | || $this->belongsToExcludedNamespace($name); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function isClassInternal(string $name): bool |
||
| 58 | { |
||
| 59 | return $this->reflector->isClassInternal($name); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function isClassExcluded(string $name): bool |
||
| 63 | { |
||
| 64 | return $this->reflector->isClassInternal($name) |
||
| 65 | || $this->belongsToExcludedNamespace($name); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function isConstantInternal(string $name): bool |
||
| 69 | { |
||
| 70 | return $this->reflector->isConstantInternal($name); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function isConstantExcluded(string $name): bool |
||
| 74 | { |
||
| 75 | // TODO: double check not sure that internal should mean excluded for constants |
||
| 76 | return $this->reflector->isConstantInternal($name) |
||
| 77 | || $this->belongsToExcludedNamespace($name); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function isExposedFunction(string $resolvedName): bool |
||
| 81 | { |
||
| 82 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 83 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
| 84 | && ( |
||
| 85 | $this->_isExposedFunctionFromGlobalNamespace($resolvedName) |
||
| 86 | || $this->isSymbolExposed($resolvedName) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool |
||
| 91 | { |
||
| 92 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 93 | && !$this->reflector->isFunctionInternal($resolvedName) |
||
| 94 | && $this->_isExposedFunctionFromGlobalNamespace($resolvedName); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function isExposedClass(string $resolvedName): bool |
||
| 98 | { |
||
| 99 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 100 | && !$this->reflector->isClassInternal($resolvedName) |
||
| 101 | && ( |
||
| 102 | $this->_isExposedClassFromGlobalNamespace($resolvedName) |
||
| 103 | || $this->isSymbolExposed($resolvedName) |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function isExposedClassFromGlobalNamespace(string $resolvedName): bool |
||
| 108 | { |
||
| 109 | return !$this->belongsToExcludedNamespace($resolvedName) |
||
| 110 | && !$this->reflector->isClassInternal($resolvedName) |
||
| 111 | && $this->_isExposedClassFromGlobalNamespace($resolvedName); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function isExposedConstant(string $name): bool |
||
| 115 | { |
||
| 116 | // Special case: internal constants must be treated as exposed symbols. |
||
| 117 | // |
||
| 118 | // Example: when declaring a new internal constant for compatibility |
||
| 119 | // reasons, it must remain un-prefixed. |
||
| 120 | return !$this->belongsToExcludedNamespace($name) |
||
| 121 | && ( |
||
| 122 | $this->reflector->isConstantInternal($name) |
||
| 123 | || $this->isExposedConstantFromGlobalNamespace($name) |
||
| 124 | || $this->isSymbolExposed($name, true) |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function isExposedConstantFromGlobalNamespace(string $constantName): bool |
||
| 129 | { |
||
| 130 | return $this->symbolsConfiguration->shouldExposeGlobalConstants() && !strpos($constantName, '\\'); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function isExcludedNamespace(string $name): bool |
||
| 134 | { |
||
| 135 | return $this->symbolsConfiguration |
||
| 136 | ->getExcludedNamespaces() |
||
| 137 | ->isRegisteredNamespace($name); |
||
| 138 | } |
||
| 139 | |||
| 140 | private function _isExposedFunctionFromGlobalNamespace(string $functionName): bool |
||
| 141 | { |
||
| 142 | return $this->symbolsConfiguration->shouldExposeGlobalFunctions() && !strpos($functionName, '\\'); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function _isExposedClassFromGlobalNamespace(string $className): bool |
||
| 146 | { |
||
| 147 | return $this->symbolsConfiguration->shouldExposeGlobalClasses() && !strpos($className, '\\'); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Tells if a given symbol is exposed. Note however that it does not account for when: |
||
| 152 | * |
||
| 153 | * - The symbol belongs to the global namespace and the symbols of the global namespace of this type are exposed |
||
| 154 | * - Belongs to an excluded namespace |
||
| 155 | * |
||
| 156 | * @param bool $constant Unlike other symbols, constants _can_ be case insensitive but 99% are not so we leave out |
||
| 157 | * the case where they are not case sensitive. |
||
| 158 | */ |
||
| 159 | private function isSymbolExposed(string $name, bool $constant = false): bool |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the namespace remains case insensitive for |
||
| 205 | * constants regardless of whether or not constants actually are case insensitive. |
||
| 206 | */ |
||
| 207 | private static function lowerCaseConstantName(string $name): string |
||
| 218 | } |
||
| 219 | } |
||
| 220 |