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 |
||
38 | final class Whitelist implements Countable |
||
39 | { |
||
40 | private $original; |
||
41 | private $symbols; |
||
42 | private $constants; |
||
43 | private $namespaces; |
||
44 | private $patterns; |
||
45 | |||
46 | private $whitelistGlobalConstants; |
||
47 | private $whitelistGlobalClasses; |
||
48 | private $whitelistGlobalFunctions; |
||
49 | |||
50 | private $whitelistedFunctions = []; |
||
51 | private $whitelistedClasses = []; |
||
52 | |||
53 | 14 | public static function create( |
|
117 | |||
118 | private static function assertValidPattern(string $element): void |
||
129 | |||
130 | /** |
||
131 | * @param string[] $original |
||
132 | * @param string[] $patterns |
||
133 | * @param string[] $namespaces |
||
134 | */ |
||
135 | 14 | private function __construct( |
|
154 | |||
155 | 529 | public function belongsToWhitelistedNamespace(string $name): bool |
|
171 | |||
172 | /** |
||
173 | * @internal |
||
174 | */ |
||
175 | 523 | public function whitelistGlobalFunctions(): bool |
|
179 | |||
180 | 50 | public function isGlobalWhitelistedFunction(string $functionName): bool |
|
184 | |||
185 | 11 | public function recordWhitelistedFunction(FullyQualified $original, FullyQualified $alias): void |
|
189 | |||
190 | 525 | public function getRecordedWhitelistedFunctions(): array |
|
199 | |||
200 | /** |
||
201 | * @internal |
||
202 | */ |
||
203 | 523 | public function whitelistGlobalConstants(): bool |
|
207 | |||
208 | 81 | public function isGlobalWhitelistedConstant(string $constantName): bool |
|
212 | |||
213 | /** |
||
214 | * @internal |
||
215 | */ |
||
216 | 6 | public function whitelistGlobalClasses(): bool |
|
220 | |||
221 | 324 | public function isGlobalWhitelistedClass(string $className): bool |
|
225 | |||
226 | 81 | public function recordWhitelistedClass(FullyQualified $original, FullyQualified $alias): void |
|
230 | |||
231 | 524 | public function getRecordedWhitelistedClasses(): array |
|
235 | |||
236 | /** |
||
237 | * Tells if a given symbol is whitelisted. Note however that it does not account for when:. |
||
238 | * |
||
239 | * - The symbol belongs to the global namespace and the symbols of the global namespace of this type are whitelisted |
||
240 | * - Belongs to a whitelisted namespace |
||
241 | * |
||
242 | * @param bool $constant Unlike other symbols, constants _can_ be case insensitive but 99% are not so we leave out |
||
243 | * the case where they are not case sensitive. |
||
244 | */ |
||
245 | 400 | public function isSymbolWhitelisted(string $name, bool $constant = false): bool |
|
265 | |||
266 | /** |
||
267 | * @return string[] |
||
268 | * |
||
269 | * @deprecated To be replaced by getWhitelistedClasses |
||
270 | */ |
||
271 | public function getClassWhitelistArray(): array |
||
272 | { |
||
273 | return array_filter( |
||
274 | $this->original, |
||
275 | function (string $name): bool { |
||
276 | return '*' !== $name && '\*' !== substr($name, -2); |
||
277 | } |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | 526 | public function toArray(): array |
|
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function count(): int |
||
293 | |||
294 | /** |
||
295 | * Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the namespace remains case insensitive for |
||
296 | * constants regardless of whether or not constants actually are case insensitive. |
||
297 | */ |
||
298 | 120 | private static function lowerConstantName(string $name): string |
|
310 | } |
||
311 |