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 Field extends \samson\activerecord\field |
||
12 | { |
||
13 | /** |
||
14 | * Find additional field database record by Name. |
||
15 | * This is generic method that should be used in nested classes to find its |
||
16 | * records by some its primary key value. |
||
17 | * |
||
18 | * @param QueryInterface $query Query object instance |
||
19 | * @param string $name Additional field name |
||
20 | * @param self $return Variable to return found database record |
||
21 | * @return bool|null|self Field instance or null if 3rd parameter not passed |
||
22 | */ |
||
23 | View Code Duplication | public static function byName(QueryInterface $query, $name, self & $return = null) |
|
31 | |||
32 | /** |
||
33 | * Find additional field database record by Name or ID. |
||
34 | * This is generic method that should be used in nested classes to find its |
||
35 | * records by some its primary key value. |
||
36 | * |
||
37 | * @param QueryInterface $query Query object instance |
||
38 | * @param string $nameOrID Additional field name or identifier |
||
39 | * @param self $return Variable to return found database record |
||
40 | * @return bool|null|self Field instance or null if 3rd parameter not passed |
||
41 | */ |
||
42 | View Code Duplication | public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null) |
|
54 | } |
||
55 |
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.