Complex classes like ExportService 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 ExportService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ExportService extends BaseApplicationComponent |
||
17 | { |
||
18 | /** |
||
19 | * Contains the working export service's name. |
||
20 | * |
||
21 | * @var IExportElementType |
||
22 | */ |
||
23 | private $_service; |
||
24 | |||
25 | /** |
||
26 | * Custom <tr> paths. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | public $customTableRowPaths = array(); |
||
31 | |||
32 | /** |
||
33 | * Whether custom table row paths have been loaded. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $_loadedTableRowPaths = false; |
||
38 | |||
39 | /** |
||
40 | * Saves an export map to the database. |
||
41 | * |
||
42 | * @param array $settings |
||
43 | * @param array $map |
||
44 | */ |
||
45 | 2 | public function saveMap(array $settings, array $map) |
|
72 | |||
73 | /** |
||
74 | * @codeCoverageIgnore |
||
75 | * |
||
76 | * @param \CDbCriteria $criteria |
||
77 | * |
||
78 | * @return Export_MapRecord|array|null |
||
79 | */ |
||
80 | public function findMap(\CDbCriteria $criteria) |
||
84 | |||
85 | /** |
||
86 | * @codeCoverageIgnore |
||
87 | * |
||
88 | * @return Export_MapRecord |
||
89 | */ |
||
90 | protected function getNewMap() |
||
94 | |||
95 | /** |
||
96 | * Download the export csv. |
||
97 | * |
||
98 | * @param array $settings |
||
99 | * |
||
100 | * @return string |
||
101 | * |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | 7 | public function download(array $settings) |
|
175 | |||
176 | /** |
||
177 | * Get service to use for exporting. |
||
178 | * |
||
179 | * @param string $elementType |
||
180 | * |
||
181 | * @return object|bool |
||
182 | */ |
||
183 | 4 | public function getService($elementType) |
|
206 | |||
207 | /** |
||
208 | * Get path to fieldtype's custom <tr> template. |
||
209 | * |
||
210 | * @param string $fieldHandle |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 2 | public function getCustomTableRow($fieldHandle) |
|
215 | { |
||
216 | // If table row paths haven't been loaded |
||
217 | 2 | if (!$this->_loadedTableRowPaths) { |
|
218 | |||
219 | // Call hook for all plugins |
||
220 | 2 | $responses = craft()->plugins->call('registerExportTableRowPaths'); |
|
221 | |||
222 | // Loop through responses from each plugin |
||
223 | 2 | foreach ($responses as $customPaths) { |
|
224 | |||
225 | // Append custom paths to master list |
||
226 | 1 | $this->customTableRowPaths = array_merge($this->customTableRowPaths, $customPaths); |
|
227 | 2 | } |
|
228 | |||
229 | // Table row paths have been loaded |
||
230 | 2 | $this->_loadedTableRowPaths = true; |
|
231 | 2 | } |
|
232 | |||
233 | // If fieldtype has been registered and is not falsey |
||
234 | 2 | if (array_key_exists($fieldHandle, $this->customTableRowPaths) && $this->customTableRowPaths[$fieldHandle]) { |
|
235 | |||
236 | // Return specified custom path |
||
237 | 1 | return $this->customTableRowPaths[$fieldHandle]; |
|
238 | } |
||
239 | |||
240 | 1 | return false; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Get data from sources. |
||
245 | * |
||
246 | * @param array $settings |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | 6 | protected function getData(array $settings) |
|
251 | { |
||
252 | // Get other sources |
||
253 | 6 | $sources = craft()->plugins->call('registerExportSource', array($settings)); |
|
254 | |||
255 | // Loop through sources, see if we can get any data |
||
256 | 6 | $data = array(); |
|
257 | 6 | foreach ($sources as $plugin) { |
|
258 | 1 | if (is_array($plugin)) { |
|
259 | 1 | foreach ($plugin as $source) { |
|
260 | 1 | $data[] = $source; |
|
261 | 1 | } |
|
262 | 1 | } |
|
263 | 6 | } |
|
264 | |||
265 | // Cut up data from source |
||
266 | 6 | if (!empty($settings['offset']) && !empty($settings['limit'])) { |
|
267 | $data = array_slice($data, $settings['offset'], $settings['limit']); |
||
268 | } |
||
269 | |||
270 | // If no data from source, get data by ourselves |
||
271 | 6 | if (!count($data)) { |
|
272 | |||
273 | // Find data |
||
274 | 5 | $criteria = $this->_service->setCriteria($settings); |
|
275 | |||
276 | // Gather element ids |
||
277 | 5 | $data = $criteria->ids(); |
|
278 | 5 | } |
|
279 | |||
280 | 6 | return $data; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Parse fields. |
||
285 | * |
||
286 | * @param array $settings |
||
287 | * @param $element |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | 3 | protected function parseFields(array $settings, $element) |
|
316 | |||
317 | /** |
||
318 | * Parse column names. |
||
319 | * |
||
320 | * @param array $settings [description] |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | 3 | protected function parseColumns(array $settings) |
|
341 | |||
342 | /** |
||
343 | * Parse reserved element values. |
||
344 | * |
||
345 | * @param $handle |
||
346 | * @param $data |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | 3 | protected function parseElementData($handle, $data) |
|
390 | |||
391 | /** |
||
392 | * Parse field values. |
||
393 | * |
||
394 | * @param string $handle |
||
395 | * @param mixed $data |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | 11 | public function parseFieldData($handle, $data) |
|
516 | } |
||
517 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.