Complex classes like Antibot 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 Antibot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class Antibot implements LoggerAwareInterface |
||
60 | { |
||
61 | /** |
||
62 | * Session persistent, unique token |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $unique; |
||
67 | /** |
||
68 | * Antibot prefix |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $prefix; |
||
73 | /** |
||
74 | * Parameter scope nodes |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $scope = []; |
||
79 | /** |
||
80 | * Unique signature |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $signature; |
||
85 | /** |
||
86 | * Parameter prefix |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $parameterPrefix; |
||
91 | /** |
||
92 | * GET & POST data |
||
93 | * |
||
94 | * @var null|array |
||
95 | */ |
||
96 | protected $data = null; |
||
97 | /** |
||
98 | * Validators |
||
99 | * |
||
100 | * @var ValidatorInterface[] |
||
101 | */ |
||
102 | protected $validators = []; |
||
103 | /** |
||
104 | * Immutable instance |
||
105 | * |
||
106 | * @var bool |
||
107 | */ |
||
108 | protected $immutable = false; |
||
109 | /** |
||
110 | * Logger |
||
111 | * |
||
112 | * @var LoggerInterface |
||
113 | */ |
||
114 | protected $logger = null; |
||
115 | /** |
||
116 | * Default antibot prefix |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | const DEFAULT_PREFIX = 'antibot'; |
||
121 | |||
122 | /** |
||
123 | * Antibot constructor |
||
124 | * |
||
125 | * @param string $unique Session-persistent, unique key |
||
126 | * @param string $prefix Prefix |
||
127 | */ |
||
128 | 20 | public function __construct(string $unique, string $prefix = self::DEFAULT_PREFIX) |
|
134 | |||
135 | /** |
||
136 | * Return the session persistent, unique token |
||
137 | * |
||
138 | * @return string Session persistent, unique token |
||
139 | */ |
||
140 | 5 | public function getUnique(): string |
|
144 | |||
145 | /** |
||
146 | * Return the prefix |
||
147 | * |
||
148 | * @return string Prefix |
||
149 | */ |
||
150 | 2 | public function getPrefix(): string |
|
154 | |||
155 | /** |
||
156 | * Return the submitted Antibot data |
||
157 | * |
||
158 | * @return string[] Antibot data |
||
159 | */ |
||
160 | 6 | public function getData(): ?array |
|
166 | |||
167 | /** |
||
168 | * Return the parameter prefix |
||
169 | * |
||
170 | * @return string Parameter prefix |
||
171 | * @throws RuntimeException If Antibot needs to be initialized |
||
172 | */ |
||
173 | 14 | public function getParameterPrefix(): string |
|
179 | |||
180 | /** |
||
181 | * Add a validator |
||
182 | * |
||
183 | * @param ValidatorInterface $validator Validator |
||
184 | */ |
||
185 | 12 | public function addValidator(ValidatorInterface $validator): void |
|
190 | |||
191 | /** |
||
192 | * Validate a request |
||
193 | * |
||
194 | * @param ServerRequestInterface $request Request |
||
195 | * @param ValidationResultInterface $result Validation result |
||
196 | * |
||
197 | * @return ValidationResultInterface Validation result |
||
198 | * @internal |
||
199 | */ |
||
200 | 12 | public function validateRequest( |
|
240 | |||
241 | /** |
||
242 | * Create and return the raw armor input elements |
||
243 | * |
||
244 | * @param ServerRequestInterface $request Request |
||
245 | * |
||
246 | * @return InputElement[] Armor input elements |
||
247 | */ |
||
248 | 5 | public function armorInputs(ServerRequestInterface $request): array |
|
264 | |||
265 | /** |
||
266 | * Compare and sort validators |
||
267 | * |
||
268 | * @param ValidatorInterface $validator1 Validator 1 |
||
269 | * @param ValidatorInterface $validator2 Validator 2 |
||
270 | * |
||
271 | * @return int Sorting |
||
272 | */ |
||
273 | protected function sortValidators(ValidatorInterface $validator1, ValidatorInterface $validator2): int |
||
283 | |||
284 | /** |
||
285 | * Pre-validation initialization |
||
286 | * |
||
287 | * @param ServerRequestInterface $request Request |
||
288 | */ |
||
289 | 13 | protected function initialize(ServerRequestInterface $request): void |
|
299 | |||
300 | /** |
||
301 | * Calculate the unique signature |
||
302 | * |
||
303 | * @return string Signature |
||
304 | */ |
||
305 | 13 | protected function calculateSignature(): string |
|
311 | |||
312 | /** |
||
313 | * Extract the antibot data from GET and POST parameters |
||
314 | * |
||
315 | * @param ServerRequestInterface $request Request |
||
316 | */ |
||
317 | 13 | protected function extractData(ServerRequestInterface $request): void |
|
323 | |||
324 | /** |
||
325 | * Extract scoped data |
||
326 | * |
||
327 | * @param array $data Source data |
||
328 | * |
||
329 | * @return array|null Scoped data |
||
330 | */ |
||
331 | 13 | protected function extractScopedData(array $data): ?array |
|
344 | |||
345 | /** |
||
346 | * Check whether this Antibot instance is immutable |
||
347 | * |
||
348 | * @throws RuntimeException If the Antibot instance is immutable |
||
349 | */ |
||
350 | 12 | protected function checkImmutable(): void |
|
359 | |||
360 | /** |
||
361 | * Check whether this Antibot instance is already initialized |
||
362 | * |
||
363 | * @throws RuntimeException If the Antibot instance still needs to be initialized |
||
364 | */ |
||
365 | 15 | protected function checkInitialized(): void |
|
375 | |||
376 | /** |
||
377 | * Return the logger |
||
378 | * |
||
379 | * @return LoggerInterface Logger |
||
380 | */ |
||
381 | 6 | public function getLogger(): LoggerInterface |
|
385 | |||
386 | /** |
||
387 | * Sets a logger instance on the object |
||
388 | * |
||
389 | * @param LoggerInterface $logger Logger |
||
390 | * |
||
391 | * @return void |
||
392 | */ |
||
393 | 12 | public function setLogger(LoggerInterface $logger): void |
|
397 | |||
398 | /** |
||
399 | * Set the parameter scope |
||
400 | * |
||
401 | * @param string[] ...$scope Parameter scope |
||
402 | */ |
||
403 | 1 | public function setParameterScope(...$scope): void |
|
417 | |||
418 | /** |
||
419 | * Scope a set of parameters |
||
420 | * |
||
421 | * @param array $params Parameters |
||
422 | * |
||
423 | * @return array Scoped parameters |
||
424 | */ |
||
425 | 1 | public function getScopedParameters(array $params): array |
|
435 | } |
||
436 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.