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|bool  | 
            ||
| 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)  | 
            |
| 105 |     { | 
            ||
| 106 | // Get max power  | 
            ||
| 107 | 7 | craft()->config->maxPowerCaptain();  | 
            |
| 108 | |||
| 109 | // Check what service we're gonna need  | 
            ||
| 110 | 7 |         if (!($this->_service = $this->getService($settings['type']))) { | 
            |
| 111 | 1 |             throw new Exception(Craft::t('Unknown Element Type Service called.')); | 
            |
| 112 | }  | 
            ||
| 113 | |||
| 114 | // Get delimiter  | 
            ||
| 115 | 6 |         $delimiter = craft()->plugins->callFirst('registerExportCsvDelimiter'); | 
            |
| 116 | 6 | $delimiter = is_null($delimiter) ? ',' : $delimiter;  | 
            |
| 117 | |||
| 118 | // Open output buffer  | 
            ||
| 119 | 6 | ob_start();  | 
            |
| 120 | |||
| 121 | // Write to output stream  | 
            ||
| 122 | 6 |         $export = fopen('php://output', 'w'); | 
            |
| 123 | |||
| 124 | // Get data  | 
            ||
| 125 | 6 | $data = $this->getData($settings);  | 
            |
| 126 | |||
| 127 | // If there is data, process  | 
            ||
| 128 | 6 |         if (is_array($data) && count($data)) { | 
            |
| 129 | |||
| 130 | // Put down columns  | 
            ||
| 131 | 3 | fputcsv($export, $this->parseColumns($settings), $delimiter);  | 
            |
| 132 | |||
| 133 | // Loop through data  | 
            ||
| 134 | 3 |             foreach ($data as $element) { | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 135 | |||
| 136 | // Fetch element in case of element id  | 
            ||
| 137 | 3 |                 if (is_numeric($element)) { | 
            |
| 138 | 2 | $element = craft()->elements->getElementById($element, $settings['type']);  | 
            |
| 139 | 2 | }  | 
            |
| 140 | |||
| 141 | // Get fields  | 
            ||
| 142 | 3 | $fields = $this->parseFields($settings, $element);  | 
            |
| 143 | |||
| 144 | // Gather row data  | 
            ||
| 145 | 3 | $rows = array();  | 
            |
| 146 | |||
| 147 | // Loop trough the fields  | 
            ||
| 148 | 3 |                 foreach ($fields as $handle => $data) { | 
            |
| 149 | |||
| 150 | // Parse element data  | 
            ||
| 151 | 3 | $data = $this->parseElementData($handle, $data);  | 
            |
| 152 | |||
| 153 | // Parse field data  | 
            ||
| 154 | 3 | $data = $this->parseFieldData($handle, $data);  | 
            |
| 155 | |||
| 156 | // Encode and add to rows  | 
            ||
| 157 | 3 | $rows[] = StringHelper::convertToUTF8($data);  | 
            |
| 158 | 3 | }  | 
            |
| 159 | |||
| 160 | // Add rows to export  | 
            ||
| 161 | 3 | fputcsv($export, $rows, $delimiter);  | 
            |
| 162 | 3 | }  | 
            |
| 163 | 3 | }  | 
            |
| 164 | |||
| 165 | // Close buffer and return data  | 
            ||
| 166 | 6 | fclose($export);  | 
            |
| 167 | 6 | $data = ob_get_clean();  | 
            |
| 168 | |||
| 169 | // Use windows friendly newlines  | 
            ||
| 170 | 6 |         $data = str_replace("\n", "\r\n", $data); | 
            |
| 171 | |||
| 172 | // Return the data to controller  | 
            ||
| 173 | 6 | return $data;  | 
            |
| 174 | }  | 
            ||
| 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|bool  | 
            ||
| 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 (array_key_exists('offset', $settings) && is_numeric($settings['offset'])) { | 
            |
| 267 | 2 | $data = array_slice($data, $settings['offset'], $settings['limit']);  | 
            |
| 268 | 2 | }  | 
            |
| 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 mixed $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  | 
            ||
| 321 | *  | 
            ||
| 322 | * @return string  | 
            ||
| 323 | */  | 
            ||
| 324 | 3 | protected function parseColumns(array $settings)  | 
            |
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * Parse reserved element values.  | 
            ||
| 344 | *  | 
            ||
| 345 | * @param string $handle  | 
            ||
| 346 | * @param string $data  | 
            ||
| 347 | *  | 
            ||
| 348 | * @return string  | 
            ||
| 349 | */  | 
            ||
| 350 | 3 | protected function parseElementData($handle, $data)  | 
            |
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * Parse field values.  | 
            ||
| 373 | *  | 
            ||
| 374 | * @param string $handle  | 
            ||
| 375 | * @param mixed $data  | 
            ||
| 376 | *  | 
            ||
| 377 | * @return string  | 
            ||
| 378 | */  | 
            ||
| 379 | 11 | public function parseFieldData($handle, $data)  | 
            |
| 490 | }  | 
            ||
| 491 | 
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.