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 $whitelistGlobalFunctions; |
||
48 | |||
49 | private $whitelistedFunctions = []; |
||
50 | private $whitelistedClasses = []; |
||
51 | |||
52 | public static function create(bool $whitelistGlobalConstants, bool $whitelistGlobalFunctions, string ...$elements): self |
||
53 | { |
||
54 | $symbols = []; |
||
55 | $constants = []; |
||
56 | $namespaces = []; |
||
57 | $patterns = []; |
||
58 | $original = []; |
||
59 | |||
60 | foreach ($elements as $element) { |
||
61 | if (isset($element[0]) && '\\' === $element[0]) { |
||
62 | $element = substr($element, 1); |
||
63 | } |
||
64 | |||
65 | if ('' === trim($element)) { |
||
66 | throw new InvalidArgumentException( |
||
67 | sprintf( |
||
68 | 'Invalid whitelist element "%s": cannot accept an empty string', |
||
69 | $element |
||
70 | ) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | $original[] = $element; |
||
75 | |||
76 | if ('\*' === substr($element, -2)) { |
||
77 | $namespaces[] = strtolower(substr($element, 0, -2)); |
||
78 | } elseif ('*' === $element) { |
||
79 | $namespaces[] = ''; |
||
80 | } elseif (false !== strpos($element, '*')) { |
||
81 | self::assertValidPattern($element); |
||
82 | |||
83 | $patterns[] = sprintf( |
||
84 | '/^%s$/u', |
||
85 | str_replace( |
||
86 | '\\', |
||
87 | '\\\\', |
||
88 | str_replace( |
||
89 | '*', |
||
90 | '.*', |
||
91 | $element |
||
92 | ) |
||
93 | ) |
||
94 | ); |
||
95 | } else { |
||
96 | $symbols[] = strtolower($element); |
||
97 | $constants[] = self::lowerConstantName($element); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return new self( |
||
102 | $whitelistGlobalConstants, |
||
103 | $whitelistGlobalFunctions, |
||
104 | array_unique($original), |
||
105 | array_flip($symbols), |
||
106 | array_flip($constants), |
||
107 | array_unique($patterns), |
||
108 | array_unique($namespaces) |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | private static function assertValidPattern(string $element): void |
||
123 | |||
124 | /** |
||
125 | * @param string[] $original |
||
126 | * @param string[] $patterns |
||
127 | * @param string[] $namespaces |
||
128 | */ |
||
129 | private function __construct( |
||
146 | |||
147 | public function belongsToWhitelistedNamespace(string $name): bool |
||
163 | |||
164 | /** |
||
165 | * @internal |
||
166 | */ |
||
167 | public function whitelistGlobalFunctions(): bool |
||
171 | |||
172 | public function isGlobalWhitelistedFunction(string $functionName): bool |
||
176 | |||
177 | public function recordWhitelistedFunction(FullyQualified $original, FullyQualified $alias): void |
||
181 | |||
182 | public function getRecordedWhitelistedFunctions(): array |
||
183 | { |
||
184 | return array_values( |
||
185 | array_unique( |
||
186 | $this->whitelistedFunctions, |
||
187 | SORT_REGULAR |
||
188 | ) |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @internal |
||
194 | */ |
||
195 | public function whitelistGlobalConstants(): bool |
||
199 | |||
200 | public function isGlobalWhitelistedConstant(string $constantName): bool |
||
204 | |||
205 | /** |
||
206 | * @internal |
||
207 | */ |
||
208 | public function whitelistGlobalClasses(): bool |
||
212 | |||
213 | public function isGlobalWhitelistedClass(string $className): bool |
||
217 | |||
218 | public function recordWhitelistedClass(FullyQualified $original, FullyQualified $alias): void |
||
222 | |||
223 | public function getRecordedWhitelistedClasses(): array |
||
224 | { |
||
225 | return $this->whitelistedClasses; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Tells if a given symbol is whitelisted. Note however that it does not account for when:. |
||
230 | * |
||
231 | * - The symbol belongs to the global namespace and the symbols of the global namespace of this type are whitelisted |
||
232 | * - Belongs to a whitelisted namespace |
||
233 | * |
||
234 | * @param bool $constant Unlike other symbols, constants _can_ be case insensitive but 99% are not so we leave out |
||
235 | * the case where they are not case sensitive. |
||
236 | */ |
||
237 | public function isSymbolWhitelisted(string $name, bool $constant = false): bool |
||
257 | |||
258 | /** |
||
259 | * @return string[] |
||
260 | * |
||
261 | * @deprecated To be replaced by getWhitelistedClasses |
||
262 | */ |
||
263 | public function getClassWhitelistArray(): array |
||
272 | |||
273 | public function toArray(): array |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function count(): int |
||
285 | |||
286 | /** |
||
287 | * Transforms the constant FQ name "Acme\Foo\X" to "acme\foo\X" since the namespace remains case insensitive for |
||
288 | * constants regardless of whether or not constants actually are case insensitive. |
||
289 | */ |
||
290 | private static function lowerConstantName(string $name): string |
||
302 | } |
||
303 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.