Complex classes like Promocodes 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 Promocodes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Promocodes |
||
12 | { |
||
13 | /** |
||
14 | * Prefix for code generation |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $prefix; |
||
19 | |||
20 | /** |
||
21 | * Suffix for code generation |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $suffix; |
||
26 | |||
27 | /** |
||
28 | * Number of codes to be generated |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $amount = 1; |
||
33 | |||
34 | /** |
||
35 | * Reward value which will be sticked to code |
||
36 | * |
||
37 | * @var null |
||
38 | */ |
||
39 | protected $reward = null; |
||
40 | |||
41 | /** |
||
42 | * Additional data to be returned with code |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $data = []; |
||
47 | |||
48 | /** |
||
49 | * Number of days of code expiration |
||
50 | * |
||
51 | * @var null|int |
||
52 | */ |
||
53 | protected $expires_in = null; |
||
54 | |||
55 | /** |
||
56 | * Maximum number of available usage of code |
||
57 | * |
||
58 | * @var null|int |
||
59 | */ |
||
60 | protected $quantity = null; |
||
61 | |||
62 | /** |
||
63 | * If code should automatically invalidate after first use |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $disposable = false; |
||
68 | |||
69 | /** |
||
70 | * Generated codes will be saved here |
||
71 | * to be validated later. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $codes = []; |
||
76 | |||
77 | /** |
||
78 | * Length of code will be calculated from asterisks you have |
||
79 | * set as mask in your config file. |
||
80 | * |
||
81 | * @var int |
||
82 | */ |
||
83 | private $length; |
||
84 | |||
85 | /** |
||
86 | * Promocodes constructor. |
||
87 | */ |
||
88 | public function __construct() |
||
101 | |||
102 | /** |
||
103 | * Save one-time use promocodes into database |
||
104 | * Successful insert returns generated promocodes |
||
105 | * Fail will return empty collection. |
||
106 | * |
||
107 | * @param int $amount |
||
108 | * @param null $reward |
||
109 | * @param array $data |
||
110 | * @param int|null $expires_in |
||
111 | * @param int|null $quantity |
||
112 | * |
||
113 | * @return \Illuminate\Support\Collection |
||
114 | */ |
||
115 | public function createDisposable( |
||
125 | |||
126 | /** |
||
127 | * Save promocodes into database |
||
128 | * Successful insert returns generated promocodes |
||
129 | * Fail will return empty collection. |
||
130 | * |
||
131 | * @param int $amount |
||
132 | * @param null $reward |
||
133 | * @param array $data |
||
134 | * @param int|null $expires_in |
||
135 | * @param int|null $quantity |
||
136 | * @param bool $is_disposable |
||
137 | * @param array $custom_codes |
||
138 | * |
||
139 | * @return \Illuminate\Support\Collection |
||
140 | */ |
||
141 | public function create( |
||
176 | |||
177 | /** |
||
178 | * Generates promocodes as many as you wish. |
||
179 | * |
||
180 | * @param int $amount |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function output($amount = null) |
||
200 | |||
201 | /** |
||
202 | * Get number of codes to be generated |
||
203 | * |
||
204 | * @param null|int $request |
||
205 | * @return null|int |
||
206 | */ |
||
207 | public function getAmount($request) |
||
211 | |||
212 | /** |
||
213 | * Set how much code you want to be generated |
||
214 | * |
||
215 | * @param int $amount |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setAmount($amount) |
||
223 | |||
224 | /** |
||
225 | * Here will be generated single code using your parameters from config. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | private function generate() |
||
255 | |||
256 | /** |
||
257 | * Your code will be validated to be unique for one request. |
||
258 | * |
||
259 | * @param $collection |
||
260 | * @param $new |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | private function validate($collection, $new) |
||
268 | |||
269 | /** |
||
270 | * Get custom set reward value |
||
271 | * |
||
272 | * @param null|int $request |
||
273 | * @return null|int |
||
274 | */ |
||
275 | public function getReward($request) |
||
279 | |||
280 | /** |
||
281 | * Set custom reward value |
||
282 | * |
||
283 | * @param int $reward |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function setReward($reward) |
||
291 | |||
292 | /** |
||
293 | * Get custom set data value |
||
294 | * |
||
295 | * @param null|array $data |
||
296 | * @return null|array |
||
297 | */ |
||
298 | public function getData($request) |
||
302 | |||
303 | /** |
||
304 | * Set custom data value |
||
305 | * |
||
306 | * @param array $data |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function setData($data) |
||
314 | |||
315 | /** |
||
316 | * Get custom set expiration days value |
||
317 | * |
||
318 | * @param null|int $request |
||
319 | * @return null|int |
||
320 | */ |
||
321 | public function getExpiresIn($request) |
||
325 | |||
326 | /** |
||
327 | * Set custom expiration days value |
||
328 | * |
||
329 | * @param int $expires_in |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setExpiresIn($expires_in) |
||
337 | |||
338 | /** |
||
339 | * Get custom disposable value |
||
340 | * |
||
341 | * @param null|bool $request |
||
342 | * @return null|bool |
||
343 | */ |
||
344 | public function getDisposable($request) |
||
348 | |||
349 | /** |
||
350 | * Set custom disposable value |
||
351 | * |
||
352 | * @param bool $disposable |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setDisposable($disposable = true) |
||
360 | |||
361 | /** |
||
362 | * Get custom set quantity value |
||
363 | * |
||
364 | * @param null|int $request |
||
365 | * @return null|int |
||
366 | */ |
||
367 | public function getQuantity($request) |
||
371 | |||
372 | /** |
||
373 | * Set custom quantity value |
||
374 | * |
||
375 | * @param int $quantity |
||
376 | * @return $this |
||
377 | */ |
||
378 | public function setQuantity($quantity) |
||
383 | |||
384 | /** |
||
385 | * Set custom prefix for next generation |
||
386 | * |
||
387 | * @param string $prefix |
||
388 | * @return $this |
||
389 | */ |
||
390 | public function setPrefix($prefix) |
||
395 | |||
396 | /** |
||
397 | * Set custom suffix for next generation |
||
398 | * |
||
399 | * @param string $suffix |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function setSuffix($suffix) |
||
407 | |||
408 | /** |
||
409 | * Reedem promocode to user that it's used from now. |
||
410 | * |
||
411 | * @param string $code |
||
412 | * |
||
413 | * @return bool|Promocode |
||
414 | * @throws AlreadyUsedException |
||
415 | * @throws UnauthenticatedException |
||
416 | */ |
||
417 | public function redeem($code) |
||
421 | |||
422 | /** |
||
423 | * Apply promocode to user that it's used from now. |
||
424 | * |
||
425 | * @param string $code |
||
426 | * |
||
427 | * @return bool|Promocode |
||
428 | * @throws AlreadyUsedException |
||
429 | * @throws UnauthenticatedException |
||
430 | */ |
||
431 | public function apply($code) |
||
457 | |||
458 | /** |
||
459 | * Check promocode in database if it is valid. |
||
460 | * |
||
461 | * @param string $code |
||
462 | * |
||
463 | * @return bool|Promocode |
||
464 | */ |
||
465 | public function check($code) |
||
475 | |||
476 | /** |
||
477 | * Check if user is trying to apply code again. |
||
478 | * |
||
479 | * @param Promocode $promocode |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function isSecondUsageAttempt(Promocode $promocode) |
||
488 | |||
489 | /** |
||
490 | * Expire code as it won't be usable anymore. |
||
491 | * |
||
492 | * @param string $code |
||
493 | * @return bool |
||
494 | * @throws InvalidPromocodeException |
||
495 | */ |
||
496 | public function disable($code) |
||
509 | |||
510 | /** |
||
511 | * Clear all expired and used promotion codes |
||
512 | * that can not be used anymore. |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function clearRedundant() |
||
525 | |||
526 | /** |
||
527 | * Get the list of valid promocodes |
||
528 | * |
||
529 | * @return Promocode[]|\Illuminate\Database\Eloquent\Collection |
||
530 | */ |
||
531 | public function all() |
||
537 | } |
||
538 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..