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 |
||
15 | class Record |
||
16 | { |
||
17 | /** @var string Table class name */ |
||
18 | protected static $identifier; |
||
19 | |||
20 | /** @var string Table primary field name */ |
||
21 | protected static $primaryFieldName; |
||
22 | |||
23 | /** @var array Collection of all entity fields */ |
||
24 | protected static $parentFields = array(); |
||
25 | |||
26 | /** @var QueryInterface Database query instance */ |
||
27 | protected $query; |
||
28 | |||
29 | /** @var array Collection of entity fields to retrieved from database */ |
||
30 | protected $selectedFields; |
||
31 | |||
32 | /** @var ConditionInterface Query conditions */ |
||
33 | protected $conditions; |
||
34 | |||
35 | /** @var array Collection of ordering parameters */ |
||
36 | protected $orderBy = array(); |
||
37 | |||
38 | /** @var array Collection of limit parameters */ |
||
39 | protected $limit = array(); |
||
40 | |||
41 | /** @var array Collection of entity identifiers */ |
||
42 | protected $entityIDs = array(); |
||
43 | |||
44 | /** |
||
45 | * Convert date value to database format. |
||
46 | * TODO: Must implement at database layer |
||
47 | * |
||
48 | * @param string $date Date value for conversion |
||
49 | * @return string Converted date to correct format |
||
50 | */ |
||
51 | protected function convertToDateTime($date) |
||
55 | |||
56 | /** |
||
57 | * Add sorting to entity identifiers. |
||
58 | * |
||
59 | * @param array $entityIDs |
||
60 | * @param string $fieldName Additional field name for sorting |
||
61 | * @param string $order Sorting order(ASC|DESC) |
||
62 | * @return array Collection of entity identifiers ordered by additional field value |
||
63 | */ |
||
64 | protected function applySorting(array $entityIDs, $fieldName, $order = 'ASC') |
||
77 | |||
78 | /** |
||
79 | * Add condition to current query. |
||
80 | * |
||
81 | * @param string $fieldName Entity field name |
||
82 | * @param string $fieldValue Value |
||
83 | * @param string $fieldRelation |
||
84 | * @return $this Chaining |
||
85 | */ |
||
86 | public function where($fieldName, $fieldValue = null, $fieldRelation = ArgumentInterface::EQUAL) |
||
92 | |||
93 | /** |
||
94 | * Set field for sorting. |
||
95 | * |
||
96 | * @param string $fieldName Additional field name |
||
97 | * @param string $order Sorting order |
||
98 | * @return $this Chaining |
||
99 | */ |
||
100 | View Code Duplication | public function orderBy($fieldName, $order = 'ASC') |
|
108 | |||
109 | /** |
||
110 | * Add primary field query condition. |
||
111 | * |
||
112 | * @param string $value Field value |
||
113 | * @return $this Chaining |
||
114 | * @see Material::where() |
||
115 | */ |
||
116 | public function primary($value) |
||
120 | |||
121 | /** |
||
122 | * Reorder elements in one array according to keys of another. |
||
123 | * |
||
124 | * @param array $array Source array |
||
125 | * @param array $orderArray Ideal array |
||
126 | * @return array Ordered array |
||
127 | */ |
||
128 | View Code Duplication | protected function sortArrayByArray(array $array, array $orderArray) { |
|
138 | |||
139 | /** |
||
140 | * Perform SamsonCMS query and get entities collection. |
||
141 | * |
||
142 | * @return \samsoncms\api\Entity[] Collection of found entities |
||
143 | */ |
||
144 | View Code Duplication | public function find() |
|
166 | |||
167 | /** |
||
168 | * Perform SamsonCMS query and get collection of entities fields. |
||
169 | * |
||
170 | * @param string $fieldName Entity field name |
||
171 | * @return array Collection of entity fields |
||
172 | */ |
||
173 | public function fields($fieldName) |
||
181 | |||
182 | /** |
||
183 | * Perform SamsonCMS query and get first matching entity. |
||
184 | * |
||
185 | * @return \samsoncms\api\Entity First matching entity |
||
186 | */ |
||
187 | View Code Duplication | public function first() |
|
198 | |||
199 | /** |
||
200 | * Perform SamsonCMS query and get amount resulting entities. |
||
201 | * |
||
202 | * @return int Amount of resulting entities |
||
203 | */ |
||
204 | public function count() |
||
212 | |||
213 | /** |
||
214 | * Generic constructor. |
||
215 | * |
||
216 | * @param QueryInterface $query Database query instance |
||
217 | */ |
||
218 | public function __construct(QueryInterface $query) |
||
223 | } |
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.