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 | * Generated codes will be saved here |
||
29 | * to be validated later. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $codes = []; |
||
34 | |||
35 | /** |
||
36 | * Length of code will be calculated from asterisks you have |
||
37 | * set as mask in your config file. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $length; |
||
42 | |||
43 | /** |
||
44 | * Promocodes constructor. |
||
45 | */ |
||
46 | public function __construct() |
||
59 | |||
60 | /** |
||
61 | * Save one-time use promocodes into database |
||
62 | * Successful insert returns generated promocodes |
||
63 | * Fail will return empty collection. |
||
64 | * |
||
65 | * @param int $amount |
||
66 | * @param null $reward |
||
67 | * @param array $data |
||
68 | * @param int|null $expires_in |
||
69 | * @param int|null $quantity |
||
70 | * |
||
71 | * @return \Illuminate\Support\Collection |
||
72 | */ |
||
73 | public function createDisposable( |
||
83 | |||
84 | /** |
||
85 | * Save promocodes into database |
||
86 | * Successful insert returns generated promocodes |
||
87 | * Fail will return empty collection. |
||
88 | * |
||
89 | * @param int $amount |
||
90 | * @param null $reward |
||
91 | * @param array $data |
||
92 | * @param int|null $expires_in |
||
93 | * @param bool $is_disposable |
||
94 | * @param int|null $quantity |
||
95 | * |
||
96 | * @return \Illuminate\Support\Collection |
||
97 | */ |
||
98 | public function create( |
||
130 | |||
131 | /** |
||
132 | * Generates promocodes as many as you wish. |
||
133 | * |
||
134 | * @param int $amount |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function output($amount = 1) |
||
154 | |||
155 | /** |
||
156 | * Set custom prefix for next generation |
||
157 | * |
||
158 | * @param $prefix |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setPrefix($prefix) |
||
166 | |||
167 | /** |
||
168 | * Set custom suffix for next generation |
||
169 | * |
||
170 | * @param $suffix |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setSuffix($suffix) |
||
178 | |||
179 | /** |
||
180 | * Here will be generated single code using your parameters from config. |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | private function generate() |
||
210 | |||
211 | /** |
||
212 | * Your code will be validated to be unique for one request. |
||
213 | * |
||
214 | * @param $collection |
||
215 | * @param $new |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | private function validate($collection, $new) |
||
223 | |||
224 | /** |
||
225 | * Reedem promocode to user that it's used from now. |
||
226 | * |
||
227 | * @param string $code |
||
228 | * |
||
229 | * @return bool|Promocode |
||
230 | * @throws AlreadyUsedException |
||
231 | * @throws UnauthenticatedException |
||
232 | */ |
||
233 | public function redeem($code) |
||
237 | |||
238 | /** |
||
239 | * Apply promocode to user that it's used from now. |
||
240 | * |
||
241 | * @param string $code |
||
242 | * |
||
243 | * @return bool|Promocode |
||
244 | * @throws AlreadyUsedException |
||
245 | * @throws UnauthenticatedException |
||
246 | */ |
||
247 | public function apply($code) |
||
277 | |||
278 | /** |
||
279 | * Check promocode in database if it is valid. |
||
280 | * |
||
281 | * @param string $code |
||
282 | * |
||
283 | * @return bool|Promocode |
||
284 | * @throws InvalidPromocodeException |
||
285 | */ |
||
286 | public function check($code) |
||
300 | |||
301 | /** |
||
302 | * Check if user is trying to apply code again. |
||
303 | * |
||
304 | * @param Promocode $promocode |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function isSecondUsageAttempt(Promocode $promocode) |
||
313 | |||
314 | /** |
||
315 | * Expire code as it won't usable anymore. |
||
316 | * |
||
317 | * @param string $code |
||
318 | * @return bool |
||
319 | * @throws InvalidPromocodeException |
||
320 | */ |
||
321 | public function disable($code) |
||
334 | |||
335 | /** |
||
336 | * Clear all expired and used promotion codes |
||
337 | * that can not be used anymore. |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function clearRedundant() |
||
350 | |||
351 | /** |
||
352 | * Get the list of valid promocodes |
||
353 | * |
||
354 | * @return Promocode[]|\Illuminate\Database\Eloquent\Collection |
||
355 | */ |
||
356 | public function all() |
||
362 | } |
||
363 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: