Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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 $amount_codes |
||
| 73 | * |
||
| 74 | * @return \Illuminate\Support\Collection |
||
| 75 | */ |
||
| 76 | public function create($amount = 1, $reward = null, array $data = [], $expires_in = null, $is_disposable = false, $amount_codes = null) |
||
| 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 $amount_codes |
||
| 112 | * |
||
| 113 | * @return \Illuminate\Support\Collection |
||
| 114 | */ |
||
| 115 | public function createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null, $amount_codes = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check promocode in database if it is valid. |
||
| 122 | * |
||
| 123 | * @param string $code |
||
| 124 | * |
||
| 125 | * @return bool|Promocode |
||
| 126 | * @throws InvalidPromocodeException |
||
| 127 | */ |
||
| 128 | public function check($code) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Apply promocode to user that it's used from now. |
||
| 145 | * |
||
| 146 | * @param string $code |
||
| 147 | * |
||
| 148 | * @return bool|Promocode |
||
| 149 | * @throws AlreadyUsedException |
||
| 150 | * @throws UnauthenticatedException |
||
| 151 | */ |
||
| 152 | public function apply($code) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Reedem promocode to user that it's used from now. |
||
| 185 | * |
||
| 186 | * @param string $code |
||
| 187 | * |
||
| 188 | * @return bool|Promocode |
||
| 189 | * @throws AlreadyUsedException |
||
| 190 | * @throws UnauthenticatedException |
||
| 191 | */ |
||
| 192 | public function redeem($code) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Expire code as it won't usable anymore. |
||
| 199 | * |
||
| 200 | * @param string $code |
||
| 201 | * @return bool |
||
| 202 | * @throws InvalidPromocodeException |
||
| 203 | */ |
||
| 204 | public function disable($code) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Clear all expired and used promotion codes |
||
| 220 | * that can not be used anymore. |
||
| 221 | * |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | public function clearRedundant() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Here will be generated single code using your parameters from config. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | private function generate() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Generate prefix with separator for promocode. |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | private function getPrefix() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Generate suffix with separator for promocode. |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | private function getSuffix() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Your code will be validated to be unique for one request. |
||
| 292 | * |
||
| 293 | * @param $collection |
||
| 294 | * @param $new |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | private function validate($collection, $new) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Check if user is trying to apply code again. |
||
| 305 | * |
||
| 306 | * @param Promocode $promocode |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function isSecondUsageAttempt(Promocode $promocode) |
||
| 314 | } |
||
| 315 |
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: