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:
Complex classes like Material often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Material, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Material extends \samson\activerecord\material |
||
20 | { |
||
21 | /** Override table attributes for late static binding */ |
||
22 | public static $_attributes = array(); |
||
23 | public static $_sql_select = array(); |
||
24 | public static $_sql_from = array(); |
||
25 | public static $_own_group = array(); |
||
26 | public static $_map = array(); |
||
27 | |||
28 | /** @var integer Primary field */ |
||
29 | public $MaterialID; |
||
30 | |||
31 | /** @var string Unique identifier */ |
||
32 | public $Url; |
||
33 | |||
34 | /** @var bool Internal existence flag */ |
||
35 | public $Active; |
||
36 | |||
37 | /** |
||
38 | * Get identifiers collection by field identifier and its value. |
||
39 | * Method is optimized for performance. |
||
40 | * |
||
41 | * @param QueryInterface $query Database query instance |
||
42 | * @param string $fieldID Additional field identifier |
||
43 | * @param string $fieldValue Additional field value for searching |
||
44 | * @param array|null $return Variable where request result would be returned |
||
45 | * @param array $materialIDs Collection of material identifiers for filtering query |
||
46 | * @return bool|array True if material entities has been found and $return is passed |
||
47 | * or identifiers collection if only two parameters is passed. |
||
48 | */ |
||
49 | public static function idsByFieldValue( |
||
80 | |||
81 | /** |
||
82 | * Get current entity identifiers collection by navigation identifier. |
||
83 | * |
||
84 | * @param QueryInterface $query Database query |
||
85 | * @param string $navigationID Navigation identifier |
||
86 | * @param array $return Variable where request result would be returned |
||
87 | * @param array $materialIDs Collection of material identifiers for filtering query |
||
88 | * @return bool|array True if material entities has been found and $return is passed |
||
89 | * or collection of identifiers if only two parameters is passed. |
||
90 | */ |
||
91 | public static function idsByNavigationID( |
||
113 | |||
114 | /** |
||
115 | * Get current entity instances collection by their identifiers. |
||
116 | * Method can accept different query executors. |
||
117 | * |
||
118 | * @param QueryInterface $query Database query |
||
119 | * @param string|array $materialIDs Material identifier or their colleciton |
||
120 | * @param self[]|array|null $return Variable where request result would be returned |
||
121 | * @param string $executor Method name for query execution |
||
122 | * @return bool|self[] True if material entities has been found and $return is passed |
||
123 | * or self[] if only two parameters is passed. |
||
124 | */ |
||
125 | public static function byIDs(QueryInterface $query, $materialIDs, &$return = array(), $executor = 'exec') |
||
136 | |||
137 | /** |
||
138 | * Get self[] by field identifier and its value. |
||
139 | * Method is optimized for performance. |
||
140 | * |
||
141 | * @param QueryInterface $query Database query instance |
||
142 | * @param string $fieldID Additional field identifier |
||
143 | * @param string $fieldValue Additional field value for searching |
||
144 | * @param self[]|array|null $return Variable where request result would be returned |
||
145 | * @return bool|self[] True if material entities has been found and $return is passed |
||
146 | * or self[] if only two parameters is passed. |
||
147 | */ |
||
148 | View Code Duplication | public static function byFieldValue(QueryInterface $query, $fieldID, $fieldValue, &$return = array()) |
|
159 | |||
160 | /** |
||
161 | * Get current entity instances collection by navigation identifier. |
||
162 | * |
||
163 | * @param QueryInterface $query Database query |
||
164 | * @param string $navigationID Navigation identifier |
||
165 | * @param self[]|array|null $return Variable where request result would be returned |
||
166 | * @return bool|self[] True if material entities has been found and $return is passed |
||
167 | * or self[] if only two parameters is passed. |
||
168 | */ |
||
169 | View Code Duplication | public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
180 | |||
181 | /** |
||
182 | * Get current entity instances amount by navigation identifier. |
||
183 | * |
||
184 | * @param QueryInterface $query Database query |
||
185 | * @param string $navigationID Navigation identifier |
||
186 | * @param self[]|array|null $return Variable where request result would be returned |
||
187 | * @return bool|self[] True if material entities has been found and $return is passed |
||
188 | * or self[] if only two parameters is passed. |
||
189 | */ |
||
190 | View Code Duplication | public static function amountByNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
201 | |||
202 | /** |
||
203 | * Get current entity instances collection by navigation identifier and additional field value. |
||
204 | * |
||
205 | * @param QueryInterface $query Database query |
||
206 | * @param string $navigationID Navigation identifier |
||
207 | * @param string $fieldID Additional field identifier |
||
208 | * @param string $fieldValue Additional field value for searching |
||
209 | * @param self[]|array|null $return Variable where request result would be returned |
||
210 | * @return bool|self[] True if material entities has been found and $return is passed |
||
211 | * or self[] if only two parameters is passed. |
||
212 | */ |
||
213 | View Code Duplication | public static function byNavigationIdAndFieldValue( |
|
231 | |||
232 | /** |
||
233 | * Get current entity instances amount by navigation identifier and additional field value. |
||
234 | * |
||
235 | * @param QueryInterface $query Database query |
||
236 | * @param string $navigationID Navigation identifier |
||
237 | * @param string $fieldID Additional field identifier |
||
238 | * @param string $fieldValue Additional field value for searching |
||
239 | * @param self[]|array|null $return Variable where request result would be returned |
||
240 | * @return bool|self[] True if material entities has been found and $return is passed |
||
241 | * or self[] if only two parameters is passed. |
||
242 | */ |
||
243 | View Code Duplication | public static function amountByNavigationIdAndFieldValue( |
|
261 | |||
262 | /** |
||
263 | * Get material entity by URL(s). |
||
264 | * |
||
265 | * @param QueryInterface $query Object for performing database queries |
||
266 | * @param array|string $url Material URL or collection of material URLs |
||
267 | * @param self|array|null $return Variable where request result would be returned |
||
268 | * @return bool|self True if material entities has been found |
||
269 | */ |
||
270 | public static function byUrl(QueryInterface $query, $url, & $return = array()) |
||
281 | |||
282 | /** |
||
283 | * Set additional material field value by field identifier |
||
284 | * @param string $fieldID Field identifier |
||
285 | * @param string $value Value to be stored |
||
286 | * @param string $locale Locale identifier |
||
287 | */ |
||
288 | public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE) |
||
316 | |||
317 | /** |
||
318 | * Get select additional field text value. |
||
319 | * |
||
320 | * @param string $fieldID Field identifier |
||
321 | * @return string Select field text |
||
322 | */ |
||
323 | public function selectText($fieldID) |
||
337 | |||
338 | /** |
||
339 | * Get collection of images for material by gallery additional field selector. If none is passed |
||
340 | * all images from gallery table would be returned for this material entity. |
||
341 | * |
||
342 | * @param string|null $fieldSelector Additional field selector value |
||
343 | * @param string $selector Additional field field name to search for |
||
344 | * @return \samsonframework\orm\RecordInterface[] Collection of images in this gallery additional field for material |
||
345 | */ |
||
346 | public function &gallery($fieldSelector = null, $selector = 'FieldID') |
||
378 | |||
379 | /** |
||
380 | * Create copy of current object. |
||
381 | * |
||
382 | * @param mixed $clone Material for cloning |
||
383 | * @param array $excludedFields excluded from materialfield fields identifiers |
||
384 | * @returns void |
||
385 | */ |
||
386 | public function ©(& $clone = null, $excludedFields = array()) |
||
428 | |||
429 | /** |
||
430 | * Function to retrieve this material table by specified field |
||
431 | * @param string $tableSelector Selector to identify table structure |
||
432 | * @param string $selector Database field by which search is performed |
||
433 | * @param array $tableColumns Columns names list |
||
434 | * @param string $externalHandler External handler to perform some extra code |
||
435 | * @param array $params External handler params |
||
436 | * @return array Collection of collections of table cells, represented as materialfield objects |
||
437 | */ |
||
438 | public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array()) |
||
527 | } |
||
528 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: