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 | * |
||
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 | 5 | 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 | * |
||
196 | * @return ValidationResult Validation result |
||
197 | */ |
||
198 | 12 | public function validate(ServerRequestInterface $request): ValidationResult |
|
237 | |||
238 | /** |
||
239 | * Create and return the raw armor input elements |
||
240 | * |
||
241 | * @param ServerRequestInterface $request Request |
||
242 | * |
||
243 | * @return InputElement[] Armor input elements |
||
244 | */ |
||
245 | 5 | public function armorInputs(ServerRequestInterface $request): array |
|
261 | |||
262 | /** |
||
263 | * Compare and sort validators |
||
264 | * |
||
265 | * @param ValidatorInterface $validator1 Validator 1 |
||
266 | * @param ValidatorInterface $validator2 Validator 2 |
||
267 | * |
||
268 | * @return int Sorting |
||
269 | */ |
||
270 | protected function sortValidators(ValidatorInterface $validator1, ValidatorInterface $validator2): int |
||
280 | |||
281 | /** |
||
282 | * Pre-validation initialization |
||
283 | * |
||
284 | * @param ServerRequestInterface $request Request |
||
285 | */ |
||
286 | 13 | protected function initialize(ServerRequestInterface $request): void |
|
296 | |||
297 | /** |
||
298 | * Calculate the unique signature |
||
299 | * |
||
300 | * @return string Signature |
||
301 | */ |
||
302 | 13 | protected function calculateSignature(): string |
|
308 | |||
309 | /** |
||
310 | * Extract the antibot data from GET and POST parameters |
||
311 | * |
||
312 | * @param ServerRequestInterface $request Request |
||
313 | */ |
||
314 | 13 | protected function extractData(ServerRequestInterface $request): void |
|
320 | |||
321 | /** |
||
322 | * Extract scoped data |
||
323 | * |
||
324 | * @param array $data Source data |
||
325 | * |
||
326 | * @return array|null Scoped data |
||
327 | */ |
||
328 | 13 | protected function extractScopedData(array $data): ?array |
|
341 | |||
342 | /** |
||
343 | * Check whether this Antibot instance is immutable |
||
344 | * |
||
345 | * @throws RuntimeException If the Antibot instance is immutable |
||
346 | */ |
||
347 | 12 | protected function checkImmutable(): void |
|
356 | |||
357 | /** |
||
358 | * Check whether this Antibot instance is already initialized |
||
359 | * |
||
360 | * @throws RuntimeException If the Antibot instance still needs to be initialized |
||
361 | */ |
||
362 | 15 | protected function checkInitialized(): void |
|
372 | |||
373 | /** |
||
374 | * Return the logger |
||
375 | * |
||
376 | * @return LoggerInterface Logger |
||
377 | */ |
||
378 | 6 | public function getLogger(): LoggerInterface |
|
382 | |||
383 | /** |
||
384 | * Sets a logger instance on the object |
||
385 | * |
||
386 | * @param LoggerInterface $logger Logger |
||
387 | * |
||
388 | * @return void |
||
389 | */ |
||
390 | 12 | public function setLogger(LoggerInterface $logger): void |
|
394 | |||
395 | /** |
||
396 | * Set the parameter scope |
||
397 | * |
||
398 | * @param string[] ...$scope Parameter scope |
||
399 | */ |
||
400 | 1 | public function setParameterScope(...$scope): void |
|
414 | |||
415 | /** |
||
416 | * Scope a set of parameters |
||
417 | * |
||
418 | * @param array $params Parameters |
||
419 | * |
||
420 | * @return array Scoped parameters |
||
421 | */ |
||
422 | 2 | public function getScopedParameters(array $params): array |
|
432 | } |
||
433 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.