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 |
||
4 | class SchemaValidationError |
||
5 | { |
||
6 | const LOAD_FAILED=1; |
||
7 | const SCHEMA_VIOLATION=8; |
||
8 | const FIELD_VALIDATION=21; |
||
9 | const ROW_FIELD_VALIDATION=22; |
||
10 | const ROW_VALIDATION=23; |
||
11 | |||
12 | public $code; |
||
13 | public $extraDetails; |
||
14 | |||
15 | /** |
||
16 | * @param integer $code |
||
17 | * @param mixed $extraDetails |
||
18 | */ |
||
19 | public function __construct($code, $extraDetails=null) |
||
24 | |||
25 | /** |
||
26 | * returns a string representation of the error |
||
27 | * @return string |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | public function getMessage() |
||
56 | |||
57 | /** |
||
58 | * given a list of validation errors, returns a single combined string message |
||
59 | * @param SchemaValidationError[] $validationErrors |
||
60 | * @return string |
||
61 | */ |
||
62 | public static function getErrorMessages($validationErrors) |
||
69 | } |
||
70 |
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.