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 ElementQuery 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 ElementQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ElementQuery extends OldCoreQuery |
||
18 | { |
||
19 | /** |
||
20 | * CIblock object or test double. |
||
21 | * |
||
22 | * @var object. |
||
23 | */ |
||
24 | public static $cIblockObject; |
||
25 | |||
26 | /** |
||
27 | * Query sort. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | public $sort = ['SORT' => 'ASC']; |
||
32 | |||
33 | /** |
||
34 | * Query group by. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | public $groupBy = false; |
||
39 | |||
40 | /** |
||
41 | * Iblock id. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $iblockId; |
||
46 | |||
47 | /** |
||
48 | * Iblock version. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $iblockVersion; |
||
53 | |||
54 | /** |
||
55 | * List of standard entity fields. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $standardFields = [ |
||
60 | 'ID', |
||
61 | 'TIMESTAMP_X', |
||
62 | 'TIMESTAMP_X_UNIX', |
||
63 | 'MODIFIED_BY', |
||
64 | 'DATE_CREATE', |
||
65 | 'DATE_CREATE_UNIX', |
||
66 | 'CREATED_BY', |
||
67 | 'IBLOCK_ID', |
||
68 | 'IBLOCK_SECTION_ID', |
||
69 | 'ACTIVE', |
||
70 | 'ACTIVE_FROM', |
||
71 | 'ACTIVE_TO', |
||
72 | 'SORT', |
||
73 | 'NAME', |
||
74 | 'PREVIEW_PICTURE', |
||
75 | 'PREVIEW_TEXT', |
||
76 | 'PREVIEW_TEXT_TYPE', |
||
77 | 'DETAIL_PICTURE', |
||
78 | 'DETAIL_TEXT', |
||
79 | 'DETAIL_TEXT_TYPE', |
||
80 | 'SEARCHABLE_CONTENT', |
||
81 | 'IN_SECTIONS', |
||
82 | 'SHOW_COUNTER', |
||
83 | 'SHOW_COUNTER_START', |
||
84 | 'CODE', |
||
85 | 'TAGS', |
||
86 | 'XML_ID', |
||
87 | 'EXTERNAL_ID', |
||
88 | 'TMP_ID', |
||
89 | 'CREATED_USER_NAME', |
||
90 | 'DETAIL_PAGE_URL', |
||
91 | 'LIST_PAGE_URL', |
||
92 | 'CREATED_DATE', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * Constructor. |
||
97 | * |
||
98 | * @param object $bxObject |
||
99 | * @param string $modelName |
||
100 | */ |
||
101 | public function __construct($bxObject, $modelName) |
||
109 | |||
110 | /** |
||
111 | * Instantiate bitrix entity object. |
||
112 | * |
||
113 | * @throws Exception |
||
114 | * |
||
115 | * @return object |
||
116 | */ |
||
117 | public static function instantiateCIblockObject() |
||
118 | { |
||
119 | if (static::$cIblockObject) { |
||
120 | return static::$cIblockObject; |
||
121 | } |
||
122 | |||
123 | if (class_exists('CIBlock')) { |
||
124 | return static::$cIblockObject = new CIBlock(); |
||
125 | } |
||
126 | |||
127 | throw new Exception('CIblock object initialization failed'); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Setter for groupBy. |
||
132 | * |
||
133 | * @param $value |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function groupBy($value) |
||
143 | |||
144 | /** |
||
145 | * Get list of items. |
||
146 | * |
||
147 | * @return Collection |
||
148 | */ |
||
149 | protected function loadModels() |
||
198 | |||
199 | /** |
||
200 | * Get the first element with a given code. |
||
201 | * |
||
202 | * @param string $code |
||
203 | * |
||
204 | * @return ElementModel |
||
205 | */ |
||
206 | public function getByCode($code) |
||
212 | |||
213 | /** |
||
214 | * Get the first element with a given external id. |
||
215 | * |
||
216 | * @param string $id |
||
217 | * |
||
218 | * @return ElementModel |
||
219 | */ |
||
220 | public function getByExternalId($id) |
||
226 | |||
227 | /** |
||
228 | * Get count of elements that match $filter. |
||
229 | * |
||
230 | * @return int |
||
231 | */ |
||
232 | View Code Duplication | public function count() |
|
247 | |||
248 | /** |
||
249 | * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'. |
||
250 | * |
||
251 | * @param array $fields |
||
252 | * |
||
253 | * @return null |
||
254 | */ |
||
255 | protected function normalizePropertyResultFormat(&$fields) |
||
272 | |||
273 | /** |
||
274 | * Normalize filter before sending it to getList. |
||
275 | * This prevents some inconsistency. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | protected function normalizeFilter() |
||
285 | |||
286 | /** |
||
287 | * Normalize select before sending it to getList. |
||
288 | * This prevents some inconsistency. |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | View Code Duplication | protected function normalizeSelect() |
|
303 | |||
304 | /** |
||
305 | * Fetch all iblock property codes from database |
||
306 | * |
||
307 | * return array |
||
308 | */ |
||
309 | protected function fetchAllPropsForSelect() |
||
319 | |||
320 | protected function multiplySelectForMaxJoinsRestrictionIfNeeded($select) |
||
342 | |||
343 | protected function mergeChunks($chunks) |
||
359 | |||
360 | protected function performFetchUsingSelectedMethod($rsItems) |
||
380 | |||
381 | /** |
||
382 | * Set fetch using from string or array. |
||
383 | * |
||
384 | * @param string|array $methodAndParams |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function fetchUsing($methodAndParams) |
||
413 | } |
||
414 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: