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 |
||
| 18 | final class Validator |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var ValidationResultProcessor |
||
| 22 | */ |
||
| 23 | private static $ValidationResultProcessor = null; |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * Use to separate rules. |
||
| 27 | * |
||
| 28 | * firstname: max(255)|min(8) |
||
| 29 | */ |
||
| 30 | private static $ruleSeparator = '|'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Use to separate field(s) and rule(s). |
||
| 34 | * |
||
| 35 | * firstname: max(255) |
||
| 36 | */ |
||
| 37 | private static $ruleParamSeparator = ':'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Used to separate multiple field definitions for the same rules. |
||
| 41 | * |
||
| 42 | * firstname,lastname: max(255) |
||
| 43 | */ |
||
| 44 | private static $multiFieldSeparator = ','; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Provide predefined config. |
||
| 48 | */ |
||
| 49 | private static $config = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Rules factory. |
||
| 53 | */ |
||
| 54 | private $rulesFactory; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param ValidationProviderInterface $validationProvider |
||
| 58 | * @param ValidationResultProcessorInterface $validationResultProcessor |
||
| 59 | * @param RulesFactoryInterface $rulesFactory |
||
| 60 | * @param array $inputData |
||
| 61 | * @param array $rules |
||
| 62 | * @param array $errorMessages |
||
| 63 | */ |
||
| 64 | public function __construct( |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Validate input data against the rules provided. |
||
| 82 | */ |
||
| 83 | public function validate() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Make validation. |
||
| 93 | * |
||
| 94 | * @param array $data user request data |
||
| 95 | * @param array $rules validation rules |
||
| 96 | * |
||
| 97 | * @return ValidatorRule |
||
| 98 | */ |
||
| 99 | private function make( |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Prepare user data for validator. |
||
| 157 | * |
||
| 158 | * @param array $data |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | private function prepareData(array $data) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Merges all field's rules into one |
||
| 181 | * if you have elegant implementation, you are welcome. |
||
| 182 | * |
||
| 183 | * @param array $rules |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | private function prepareRules(array $rules) |
||
| 219 | } |
||
| 220 |
This check marks private properties in classes that are never used. Those properties can be removed.