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 |
||
| 15 | abstract class AbstractValidator |
||
| 16 | { |
||
| 17 | use AlternativesTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var \Closure[] |
||
| 21 | */ |
||
| 22 | protected $validationStack = []; |
||
| 23 | /** |
||
| 24 | * @var Comfort |
||
| 25 | */ |
||
| 26 | private $comfort; |
||
| 27 | /** |
||
| 28 | * @var boolean |
||
| 29 | */ |
||
| 30 | private $toBool = false; |
||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $optional = true; |
||
| 35 | /** |
||
| 36 | * @var mixed |
||
| 37 | */ |
||
| 38 | private $defaultValue; |
||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $errorHandlers = [ |
||
| 43 | 'default' => [ |
||
| 44 | 'message' => 'There was a validation error' |
||
| 45 | ], |
||
| 46 | 'required' => [ |
||
| 47 | 'message' => '%s is required', |
||
| 48 | 'default' => 'value' |
||
| 49 | ] |
||
| 50 | ]; |
||
| 51 | |||
| 52 | public function __construct(Comfort $comfort) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Execute validation stack |
||
| 60 | * |
||
| 61 | * @param mixed $value |
||
| 62 | * @param null|string $key |
||
| 63 | * @return bool|ValidationError|null |
||
| 64 | */ |
||
| 65 | public function __invoke($value, $key = null) |
||
| 98 | |||
| 99 | public function __call($name, $arguments) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Declare the value as being required |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function required() |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Declare data is optional |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function optional() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Add adhoc validator to validation stack |
||
| 138 | * |
||
| 139 | * @param callable $validation |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function add(callable $validation) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Validate given value matches any of the provided strings |
||
| 151 | * |
||
| 152 | * @param array $vals |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function anyOf(array $vals) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Map value to key in array and grab associated array value, or |
||
| 168 | * feed value into callable and use returned value |
||
| 169 | * |
||
| 170 | * @param array|callable $mapper |
||
| 171 | * @return $this |
||
| 172 | * @throws DiscomfortException |
||
| 173 | */ |
||
| 174 | public function map($mapper) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * On validation failure whether to return false or a validation error |
||
| 194 | * |
||
| 195 | * @param bool $val |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function toBool($val = true) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set default value |
||
| 207 | * |
||
| 208 | * @param $value |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function defaultValue($value) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set error messages |
||
| 220 | * |
||
| 221 | * @param array $errorMessages |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function errorMessages(array $errorMessages) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Create an error with a formatted message |
||
| 240 | * |
||
| 241 | * @param string $key |
||
| 242 | * @param null|string $value |
||
| 243 | * @param null|string $valueKey |
||
| 244 | * @param mixed $validationValue |
||
| 245 | * @throws DiscomfortException |
||
| 246 | * @throws ValidationException |
||
| 247 | */ |
||
| 248 | protected function createError($key, $value = null, $valueKey = null, $validationValue = null) |
||
| 287 | } |
||
| 288 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.