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 |
||
22 | class Record |
||
23 | { |
||
24 | /** @var string Table class name */ |
||
25 | protected static $identifier; |
||
26 | |||
27 | /** @var array Collection of all supported entity fields ids => names */ |
||
28 | protected static $fieldIDs = array(); |
||
29 | |||
30 | /** @var array Collection of all supported entity fields names => ids */ |
||
31 | protected static $fieldNames = array(); |
||
32 | |||
33 | /** @var string Table primary field name */ |
||
34 | protected static $primaryFieldName; |
||
35 | |||
36 | /** @var QueryInterface Database query instance */ |
||
37 | protected $query; |
||
38 | |||
39 | /** @var array Collection of entity fields to retrieved from database */ |
||
40 | protected $selectedFields; |
||
41 | |||
42 | /** @var ConditionInterface Query conditions */ |
||
43 | protected $conditions; |
||
44 | |||
45 | /** @var array Collection of ordering parameters */ |
||
46 | protected $orderBy = array(); |
||
47 | |||
48 | /** @var array Collection of limit parameters */ |
||
49 | protected $limit = array(); |
||
50 | |||
51 | /** @var array Collection of entity identifiers */ |
||
52 | protected $entityIDs = array(); |
||
53 | |||
54 | /** |
||
55 | * Generic constructor. |
||
56 | * |
||
57 | * @param QueryInterface $query Database query instance |
||
58 | */ |
||
59 | public function __construct(QueryInterface $query = null) |
||
64 | |||
65 | /** |
||
66 | * Select specified entity fields. |
||
67 | * If this method is called then only selected entity fields |
||
68 | * would be filled in entity instances. |
||
69 | * |
||
70 | * @param mixed $fieldNames Entity field name or collection of names |
||
71 | * |
||
72 | * @return $this Chaining |
||
73 | */ |
||
74 | View Code Duplication | public function select($fieldNames) |
|
90 | |||
91 | /** |
||
92 | * Set field for sorting. |
||
93 | * |
||
94 | * @param string $fieldName Additional field name |
||
95 | * @param string $order Sorting order |
||
96 | * |
||
97 | * @return $this Chaining |
||
98 | */ |
||
99 | View Code Duplication | public function orderBy($fieldName, $order = 'ASC') |
|
107 | |||
108 | /** |
||
109 | * Set query results limitation. |
||
110 | * |
||
111 | * @param int $count Amount of entities to receive |
||
112 | * @param int $offset Entities starting number |
||
113 | * |
||
114 | * @return $this Chaining |
||
115 | */ |
||
116 | public function limit($count, $offset = 0) |
||
122 | |||
123 | /** |
||
124 | * Perform internal query preparation. |
||
125 | */ |
||
126 | protected function prepareQuery() |
||
147 | |||
148 | /** |
||
149 | * Perform SamsonCMS query and get entities collection. |
||
150 | * |
||
151 | * @return \samsoncms\api\Entity[] Collection of found entities |
||
152 | */ |
||
153 | public function find() |
||
165 | |||
166 | /** |
||
167 | * Add primary field query condition. |
||
168 | * |
||
169 | * @param string $value Field value |
||
170 | * @param string $fieldRelation |
||
171 | * |
||
172 | * @return $this Chaining |
||
173 | * @see Material::where() |
||
174 | */ |
||
175 | public function primary($value, $fieldRelation = ArgumentInterface::EQUAL) |
||
179 | |||
180 | /** |
||
181 | * Add condition to current query. |
||
182 | * |
||
183 | * @param string $fieldName Entity field name |
||
184 | * @param mixed $fieldValue Value |
||
185 | * @param string $fieldRelation |
||
186 | * |
||
187 | * @return $this Chaining |
||
188 | * |
||
189 | * @throws WrongQueryConditionArgument |
||
190 | */ |
||
191 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
206 | |||
207 | /** |
||
208 | * Reorder elements in one array according to keys of another. |
||
209 | * |
||
210 | * @param array $array Source array |
||
211 | * @param array $orderArray Ideal array |
||
212 | * @return array Ordered array |
||
213 | */ |
||
214 | protected function sortArrayByArray(array $array, array $orderArray) |
||
225 | |||
226 | /** |
||
227 | * Perform SamsonCMS query and get collection of entities fields. |
||
228 | * |
||
229 | * @param string $fieldName Entity field name |
||
230 | * |
||
231 | * @return array Collection of entity fields |
||
232 | */ |
||
233 | public function fields($fieldName) |
||
242 | |||
243 | /** |
||
244 | * Perform SamsonCMS query and get first matching entity. |
||
245 | * |
||
246 | * @return \samsoncms\api\Entity First matching entity |
||
247 | */ |
||
248 | public function first() |
||
260 | |||
261 | /** |
||
262 | * Perform SamsonCMS query and get amount resulting entities. |
||
263 | * |
||
264 | * @return int Amount of resulting entities |
||
265 | */ |
||
266 | public function count() |
||
274 | |||
275 | /** |
||
276 | * Convert date value to database format. |
||
277 | * TODO: Must implement at database layer |
||
278 | * |
||
279 | * @param string $date Date value for conversion |
||
280 | * |
||
281 | * @return string Converted date to correct format |
||
282 | */ |
||
283 | protected function convertToDateTime($date) |
||
287 | |||
288 | /** |
||
289 | * Add sorting to entity identifiers. |
||
290 | * |
||
291 | * @param array $entityIDs |
||
292 | * @param string $fieldName Additional field name for sorting |
||
293 | * @param string $order Sorting order(ASC|DESC) |
||
294 | * |
||
295 | * @return array Collection of entity identifiers ordered by additional field value |
||
296 | */ |
||
297 | View Code Duplication | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
|
310 | } |
||
311 |
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.