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 | * Generated codes will be saved here |
||
| 15 | * to be validated later. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $codes = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Length of code will be calculated from asterisks you have |
||
| 23 | * set as mask in your config file. |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | private $length; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Promocodes constructor. |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Generates promocodes as many as you wish. |
||
| 40 | * |
||
| 41 | * @param int $amount |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | public function output($amount = 1) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Save promocodes into database |
||
| 64 | * Successful insert returns generated promocodes |
||
| 65 | * Fail will return empty collection. |
||
| 66 | * |
||
| 67 | * @param int $amount |
||
| 68 | * @param null $reward |
||
| 69 | * @param array $data |
||
| 70 | * @param int|null $expires_in |
||
| 71 | * @param bool $is_disposable |
||
| 72 | * @param int|null $quantity |
||
| 73 | * |
||
| 74 | * @return \Illuminate\Support\Collection |
||
| 75 | */ |
||
| 76 | public function create( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Save one-time use promocodes into database |
||
| 110 | * Successful insert returns generated promocodes |
||
| 111 | * Fail will return empty collection. |
||
| 112 | * |
||
| 113 | * @param int $amount |
||
| 114 | * @param null $reward |
||
| 115 | * @param array $data |
||
| 116 | * @param int|null $expires_in |
||
| 117 | * @param int|null $quantity |
||
| 118 | * |
||
| 119 | * @return \Illuminate\Support\Collection |
||
| 120 | */ |
||
| 121 | public function createDisposable( |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Check promocode in database if it is valid. |
||
| 133 | * |
||
| 134 | * @param string $code |
||
| 135 | * |
||
| 136 | * @return bool|Promocode |
||
| 137 | * @throws InvalidPromocodeException |
||
| 138 | */ |
||
| 139 | public function check($code) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Apply promocode to user that it's used from now. |
||
| 156 | * |
||
| 157 | * @param string $code |
||
| 158 | * |
||
| 159 | * @return bool|Promocode |
||
| 160 | * @throws AlreadyUsedException |
||
| 161 | * @throws UnauthenticatedException |
||
| 162 | */ |
||
| 163 | public function apply($code) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Reedem promocode to user that it's used from now. |
||
| 196 | * |
||
| 197 | * @param string $code |
||
| 198 | * |
||
| 199 | * @return bool|Promocode |
||
| 200 | * @throws AlreadyUsedException |
||
| 201 | * @throws UnauthenticatedException |
||
| 202 | */ |
||
| 203 | public function redeem($code) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Expire code as it won't usable anymore. |
||
| 210 | * |
||
| 211 | * @param string $code |
||
| 212 | * @return bool |
||
| 213 | * @throws InvalidPromocodeException |
||
| 214 | */ |
||
| 215 | public function disable($code) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Clear all expired and used promotion codes |
||
| 231 | * that can not be used anymore. |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function clearRedundant() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the list of valid promocodes |
||
| 247 | * |
||
| 248 | * @return Promocode[]|\Illuminate\Database\Eloquent\Collection |
||
| 249 | */ |
||
| 250 | public function all() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Here will be generated single code using your parameters from config. |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | private function generate() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Generate prefix with separator for promocode. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private function getPrefix() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Generate suffix with separator for promocode. |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | private function getSuffix() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Your code will be validated to be unique for one request. |
||
| 315 | * |
||
| 316 | * @param $collection |
||
| 317 | * @param $new |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | private function validate($collection, $new) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Check if user is trying to apply code again. |
||
| 328 | * |
||
| 329 | * @param Promocode $promocode |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function isSecondUsageAttempt(Promocode $promocode) |
||
| 338 | } |
||
| 339 |
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: