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 Carbon|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 Carbon|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 disposable value |
||
| 317 | * |
||
| 318 | * @param null|bool $request |
||
| 319 | * @return null|bool |
||
| 320 | */ |
||
| 321 | public function getDisposable($request) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set custom disposable value |
||
| 328 | * |
||
| 329 | * @param bool $disposable |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function setDisposable($disposable = true) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get custom set quantity value |
||
| 340 | * |
||
| 341 | * @param null|int $request |
||
| 342 | * @return null|int |
||
| 343 | */ |
||
| 344 | public function getQuantity($request) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set custom quantity value |
||
| 351 | * |
||
| 352 | * @param int $quantity |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | public function setQuantity($quantity) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set custom prefix for next generation |
||
| 363 | * |
||
| 364 | * @param string $prefix |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | public function setPrefix($prefix) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set custom suffix for next generation |
||
| 375 | * |
||
| 376 | * @param string $suffix |
||
| 377 | * @return $this |
||
| 378 | */ |
||
| 379 | public function setSuffix($suffix) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Reedem promocode to user that it's used from now. |
||
| 387 | * |
||
| 388 | * @param string $code |
||
| 389 | * |
||
| 390 | * @return bool|Promocode |
||
| 391 | * @throws AlreadyUsedException |
||
| 392 | * @throws UnauthenticatedException |
||
| 393 | */ |
||
| 394 | public function redeem($code) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Apply promocode to user that it's used from now. |
||
| 401 | * |
||
| 402 | * @param string $code |
||
| 403 | * |
||
| 404 | * @return bool|Promocode |
||
| 405 | * @throws AlreadyUsedException |
||
| 406 | * @throws UnauthenticatedException |
||
| 407 | */ |
||
| 408 | public function apply($code) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Check promocode in database if it is valid. |
||
| 437 | * |
||
| 438 | * @param string $code |
||
| 439 | * |
||
| 440 | * @return bool|Promocode |
||
| 441 | */ |
||
| 442 | public function check($code) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Check if user is trying to apply code again. |
||
| 455 | * |
||
| 456 | * @param Promocode $promocode |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function isSecondUsageAttempt(Promocode $promocode) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Expire code as it won't be usable anymore. |
||
| 468 | * |
||
| 469 | * @param string $code |
||
| 470 | * @return bool |
||
| 471 | * @throws InvalidPromocodeException |
||
| 472 | */ |
||
| 473 | public function disable($code) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Clear all expired and used promotion codes |
||
| 489 | * that can not be used anymore. |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function clearRedundant() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the list of valid promocodes |
||
| 505 | * |
||
| 506 | * @return Promocode[]|\Illuminate\Database\Eloquent\Collection |
||
| 507 | */ |
||
| 508 | public function all() |
||
| 514 | } |
||
| 515 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.