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 | /** @var string Additional field value type */ |
||
14 | public $type; |
||
15 | |||
16 | /** @var string Additional field name */ |
||
17 | public $Name; |
||
18 | |||
19 | /** @var string Default field value */ |
||
20 | public $Value; |
||
21 | |||
22 | /** @var bool Flag is localized */ |
||
23 | public $local; |
||
24 | |||
25 | /** |
||
26 | * Get current entity instances collection by their identifiers. |
||
27 | * Method can accept different query executors. |
||
28 | * |
||
29 | * @param QueryInterface $query Database query |
||
30 | * @param string|array $fieldIDs Field identifier or their colleciton |
||
31 | * @param self[]|array|null $return Variable where request result would be returned |
||
32 | * @param string $executor Method name for query execution |
||
33 | * @return bool|self[] True if material entities has been found and $return is passed |
||
34 | * or self[] if only two parameters is passed. |
||
35 | */ |
||
36 | View Code Duplication | public static function byIDs(QueryInterface $query, $fieldIDs, &$return = array(), $executor = 'exec') |
|
47 | |||
48 | /** |
||
49 | * Get current entity identifiers collection by navigation identifier. |
||
50 | * |
||
51 | * @param QueryInterface $query Database query |
||
52 | * @param string $navigationID Navigation identifier |
||
53 | * @param array $return Variable where request result would be returned |
||
54 | * @param array $materialIDs Collection of material identifiers for filtering query |
||
55 | * @return bool|array True if field entities has been found and $return is passed |
||
56 | * or collection of identifiers if only two parameters is passed. |
||
57 | */ |
||
58 | View Code Duplication | public static function idsByNavigationID( |
|
80 | |||
81 | /** |
||
82 | * Get current entity instances collection by navigation identifier. |
||
83 | * |
||
84 | * @param QueryInterface $query Database query |
||
85 | * @param string $navigationID Navigation identifier |
||
86 | * @param self[]|array|null $return Variable where request result would be returned |
||
87 | * @return bool|self[] True if field entities has been found and $return is passed |
||
88 | * or self[] if only two parameters is passed. |
||
89 | */ |
||
90 | View Code Duplication | public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
101 | |||
102 | /** |
||
103 | * Find additional field database record by Name. |
||
104 | * This is generic method that should be used in nested classes to find its |
||
105 | * records by some its primary key value. |
||
106 | * |
||
107 | * @param QueryInterface $query Query object instance |
||
108 | * @param string $name Additional field name |
||
109 | * @param self $return Variable to return found database record |
||
110 | * @return bool|null|self Field instance or null if 3rd parameter not passed |
||
111 | */ |
||
112 | public static function byName(QueryInterface $query, $name, self & $return = null) |
||
120 | |||
121 | /** |
||
122 | * Find additional field database record by Name or ID. |
||
123 | * This is generic method that should be used in nested classes to find its |
||
124 | * records by some its primary key value. |
||
125 | * |
||
126 | * @param QueryInterface $query Query object instance |
||
127 | * @param string $nameOrID Additional field name or identifier |
||
128 | * @param self $return Variable to return found database record |
||
129 | * @return bool|null|self Field instance or null if 3rd parameter not passed |
||
130 | */ |
||
131 | public static function byNameOrID(QueryInterface $query, $nameOrID, self & $return = null) |
||
143 | |||
144 | /** |
||
145 | * If this field has defined key=>value set. |
||
146 | * |
||
147 | * @return array|mixed Grouped collection of field key => value possible values or value for key passed. |
||
148 | */ |
||
149 | public function options($key = null) |
||
163 | |||
164 | /** @return string Get additional field value field name depending on its type */ |
||
165 | public function valueFieldName() |
||
176 | } |
||
177 |
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.