Complex classes like Whitelist 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 Whitelist, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | final class Whitelist implements Countable |
||
| 38 | { |
||
| 39 | private $original; |
||
| 40 | private $symbols; |
||
| 41 | private $constants; |
||
| 42 | private $namespaces; |
||
| 43 | private $patterns; |
||
| 44 | |||
| 45 | private $whitelistGlobalConstants; |
||
| 46 | private $whitelistGlobalClasses; |
||
| 47 | private $whitelistGlobalFunctions; |
||
| 48 | |||
| 49 | private $whitelistedFunctions = []; |
||
| 50 | private $whitelistedClasses = []; |
||
| 51 | |||
| 52 | 15 | public static function create( |
|
| 116 | |||
| 117 | private static function assertValidPattern(string $element): void |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string[] $original |
||
| 131 | * @param string[] $patterns |
||
| 132 | * @param string[] $namespaces |
||
| 133 | */ |
||
| 134 | 15 | private function __construct( |
|
| 153 | |||
| 154 | 520 | public function belongsToWhitelistedNamespace(string $name): bool |
|
| 166 | |||
| 167 | 560 | public function isWhitelistedNamespace(string $name): bool |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @internal |
||
| 201 | */ |
||
| 202 | 550 | public function whitelistGlobalFunctions(): bool |
|
| 206 | |||
| 207 | 109 | public function isGlobalWhitelistedFunction(string $functionName): bool |
|
| 211 | |||
| 212 | 24 | public function recordWhitelistedFunction(FullyQualified $original, FullyQualified $alias): void |
|
| 216 | |||
| 217 | 554 | public function getRecordedWhitelistedFunctions(): array |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @internal |
||
| 224 | */ |
||
| 225 | 550 | public function whitelistGlobalConstants(): bool |
|
| 229 | |||
| 230 | 81 | public function isGlobalWhitelistedConstant(string $constantName): bool |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @internal |
||
| 237 | */ |
||
| 238 | 6 | public function whitelistGlobalClasses(): bool |
|
| 242 | |||
| 243 | 324 | public function isGlobalWhitelistedClass(string $className): bool |
|
| 247 | |||
| 248 | 82 | public function recordWhitelistedClass(FullyQualified $original, FullyQualified $alias): void |
|
| 252 | |||
| 253 | 554 | public function getRecordedWhitelistedClasses(): array |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Tells if a given symbol is whitelisted. Note however that it does not account for when:. |
||
| 260 | * |
||
| 261 | * - The symbol belongs to the global namespace and the symbols of the global namespace of this type are whitelisted |
||
| 262 | * - Belongs to a whitelisted namespace |
||
| 263 | * |
||
| 264 | * @param bool $constant Unlike other symbols, constants _can_ be case insensitive but 99% are not so we leave out |
||
| 265 | * the case where they are not case sensitive. |
||
| 266 | */ |
||
| 267 | 442 | public function isSymbolWhitelisted(string $name, bool $constant = false): bool |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @return string[] |
||
| 290 | * |
||
| 291 | * @deprecated To be replaced by getWhitelistedClasses |
||
| 292 | */ |
||
| 293 | public function getClassWhitelistArray(): array |
||
| 302 | |||
| 303 | 553 | public function toArray(): array |
|
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function count(): int |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the namespace remains case insensitive for |
||
| 318 | * constants regardless of whether or not constants actually are case insensitive. |
||
| 319 | */ |
||
| 320 | 124 | private static function lowerConstantName(string $name): string |
|
| 332 | |||
| 333 | 520 | private function retrieveNameNamespace(string $name): string |
|
| 347 | } |
||
| 348 |