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 | View Code Duplication | public static function idsByNavigationID( |
|
92 | QueryInterface $query, |
||
93 | $navigationID, |
||
94 | &$return = array(), |
||
95 | $materialIDs = null |
||
96 | ) { |
||
97 | // Prepare query |
||
98 | $query->entity(CMS::MATERIAL_NAVIGATION_RELATION_ENTITY) |
||
99 | ->where('StructureID', $navigationID) |
||
100 | ->where('Active', 1); |
||
101 | |||
102 | // Add material identifier filter if passed |
||
103 | if (isset($materialIDs)) { |
||
104 | $query->where('MaterialID', $materialIDs); |
||
105 | } |
||
106 | |||
107 | // Perform database query and get only material identifiers collection |
||
108 | $return = $query->fields('MaterialID'); |
||
109 | |||
110 | // If only one argument is passed - return null, otherwise bool |
||
111 | return func_num_args() > 2 ? sizeof($return) : $return; |
||
112 | } |
||
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 | View Code Duplication | public static function byIDs(QueryInterface $query, $materialIDs, &$return = array(), $executor = 'exec') |
|
126 | { |
||
127 | $return = $query->entity(get_called_class()) |
||
128 | ->where('MaterialID', $materialIDs) |
||
129 | ->where('Active', 1) |
||
130 | ->where('Published', 1) |
||
131 | ->$executor(); |
||
132 | |||
133 | // If only one argument is passed - return null, otherwise bool |
||
134 | return func_num_args() > 2 ? sizeof($return) : $return; |
||
135 | } |
||
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 | View Code Duplication | public static function byUrl(QueryInterface $query, $url, & $return = array()) |
|
271 | { |
||
272 | // Get entities by filtered identifiers |
||
273 | $return = $query->entity(get_called_class()) |
||
274 | ->where('Url', $url) |
||
275 | ->where('Active', 1) |
||
276 | ->first(); |
||
277 | |||
278 | // If only one argument is passed - return null, otherwise bool |
||
279 | return func_num_args() > 2 ? $return !== null : $return; |
||
280 | } |
||
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 | * Copy this material related entities. |
||
381 | * |
||
382 | * @param QueryInterface $query Database query instance |
||
383 | * @param string $entity Entity identifier |
||
384 | * @param string $newIdentifier Copied material idetifier |
||
385 | * @param array $excludedIDs Collection of related entity identifier to exclude from copying |
||
386 | */ |
||
387 | protected function copyRelatedEntity(QueryInterface $query, $entity, $newIdentifier, $excludedIDs = array()) |
||
402 | |||
403 | /** |
||
404 | * Create copy of current object. |
||
405 | * |
||
406 | * @param mixed $clone Material for cloning |
||
407 | * @param array $excludedFields Additional fields identifiers not copied |
||
408 | * @returns self New copied instance |
||
409 | */ |
||
410 | public function ©(&$clone = null, $excludedFields = array()) |
||
424 | |||
425 | public function nestedIDs($navigationID = null, &$return = array()) |
||
444 | |||
445 | /** |
||
446 | * Get material additional fields table. |
||
447 | * |
||
448 | * @param string $navigationID Navigation table identifier |
||
449 | * @param array $tableColumns Columns names list |
||
450 | * @param string $externalHandler External handler to perform some extra code |
||
451 | * @param array $params External handler params |
||
452 | * @return array Collection of collections of table cells, represented as materialfield objects |
||
453 | */ |
||
454 | public function table($navigationID, &$tableColumns = null) { |
||
520 | |||
521 | /** |
||
522 | * Function to retrieve this material table by specified field |
||
523 | * @param string $tableSelector Selector to identify table structure |
||
524 | * @param string $selector Database field by which search is performed |
||
525 | * @param array $tableColumns Columns names list |
||
526 | * @param string $externalHandler External handler to perform some extra code |
||
527 | * @param array $params External handler params |
||
528 | * @return array Collection of collections of table cells, represented as materialfield objects |
||
529 | * @deprecated Use table() |
||
530 | */ |
||
531 | public function getTable( |
||
628 | } |
||
629 |
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: