Conditions | 27 |
Paths | > 20000 |
Total Lines | 130 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
212 | public function generateExportFileData($gridField) |
||
213 | { |
||
214 | $class = $gridField->getModelClass(); |
||
215 | $columns = ($this->exportColumns) ? $this->exportColumns : self::exportFieldsForClass($class); |
||
216 | $fileData = ''; |
||
217 | |||
218 | // If we don't have an associative array |
||
219 | if (!ArrayLib::is_associative($columns)) { |
||
220 | array_combine($columns, $columns); |
||
221 | } |
||
222 | |||
223 | $singl = singleton($class); |
||
224 | |||
225 | $singular = $class ? $singl->i18n_singular_name() : ''; |
||
226 | $plural = $class ? $singl->i18n_plural_name() : ''; |
||
227 | |||
228 | $filter = new FileNameFilter; |
||
229 | if ($this->exportName) { |
||
230 | $this->exportName = $filter->filter($this->exportName); |
||
231 | } else { |
||
232 | $this->exportName = $filter->filter('fastexport-' . $plural); |
||
233 | } |
||
234 | |||
235 | $fileData = ''; |
||
236 | $separator = $this->csvSeparator; |
||
237 | |||
238 | $class = $gridField->getModelClass(); |
||
239 | $singl = singleton($class); |
||
240 | $baseTable = $singl->baseTable(); |
||
241 | |||
242 | $stream = fopen('data://text/plain,' . "", 'w+'); |
||
243 | |||
244 | // Filter columns |
||
245 | $sqlFields = []; |
||
246 | $baseFields = ['ID', 'Created', 'LastEdited']; |
||
247 | |||
248 | $joins = []; |
||
249 | $isSubsite = false; |
||
250 | $map = []; |
||
251 | if ($singl->hasMethod('fastExportMap')) { |
||
252 | $map = $singl->fastExportMap(); |
||
253 | } |
||
254 | foreach ($columns as $columnSource => $columnHeader) { |
||
255 | // Allow mapping methods to plain fields |
||
256 | if ($map && isset($map[$columnSource])) { |
||
257 | $columnSource = $map[$columnSource]; |
||
258 | } |
||
259 | if ($columnSource == 'SubsiteID') { |
||
260 | $isSubsite = true; |
||
261 | } |
||
262 | if (in_array($columnSource, $baseFields)) { |
||
263 | $sqlFields[] = $baseTable . '.' . $columnSource; |
||
264 | continue; |
||
265 | } |
||
266 | // Naive join support |
||
267 | if (strpos($columnSource, '.') !== false) { |
||
268 | $parts = explode('.', $columnSource); |
||
269 | |||
270 | $joinSingl = singleton($parts[0]); |
||
271 | $joinBaseTable = $joinSingl->baseTable(); |
||
272 | |||
273 | if (!isset($joins[$joinBaseTable])) { |
||
274 | $joins[$joinBaseTable] = []; |
||
275 | } |
||
276 | $joins[$joinBaseTable][] = $parts[1]; |
||
277 | |||
278 | $sqlFields[] = $joinBaseTable . '.' . $parts[1]; |
||
279 | continue; |
||
280 | } |
||
281 | $fieldTable = ClassInfo::table_for_object_field($class, $columnSource); |
||
282 | if ($fieldTable != $baseTable || !$fieldTable) { |
||
283 | unset($columns[$columnSource]); |
||
284 | } else { |
||
285 | $sqlFields[] = $fieldTable . '.' . $columnSource; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | if ($this->hasHeader) { |
||
290 | $headers = array(); |
||
291 | |||
292 | // determine the headers. If a field is callable (e.g. anonymous function) then use the |
||
293 | // source name as the header instead |
||
294 | foreach ($columns as $columnSource => $columnHeader) { |
||
295 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) |
||
296 | ? $columnSource : $columnHeader; |
||
297 | } |
||
298 | |||
299 | $row = array_values($headers); |
||
300 | // fputcsv($stream, $row, $separator); |
||
301 | |||
302 | // force quotes |
||
303 | fputs($stream, implode(",", array_map("self::encodeFunc", $row)) . "\n"); |
||
304 | } |
||
305 | |||
306 | if (empty($sqlFields)) { |
||
307 | $sqlFields = ['ID', 'Created', 'LastEdited']; |
||
308 | } |
||
309 | |||
310 | $where = []; |
||
311 | $sql = 'SELECT ' . implode(',', $sqlFields) . ' FROM ' . $baseTable; |
||
312 | foreach ($joins as $joinTable => $joinFields) { |
||
313 | $foreignKey = $joinTable . 'ID'; |
||
314 | $sql .= ' LEFT JOIN ' . $joinTable . ' ON ' . $joinTable . '.ID = ' . $baseTable . '.' . $foreignKey; |
||
315 | } |
||
316 | // Basic subsite support |
||
317 | if ($isSubsite && class_exists('Subsite') && Subsite::currentSubsiteID()) { |
||
318 | $where[] = $baseTable . '.SubsiteID = ' . Subsite::currentSubsiteID(); |
||
319 | } |
||
320 | |||
321 | $singl->extend('updateFastExport', $sql, $where); |
||
322 | |||
323 | // Basic where clause |
||
324 | if (!empty($where)) { |
||
325 | $sql .= ' WHERE ' . implode(' AND ', $where); |
||
326 | } |
||
327 | |||
328 | $query = DB::query($sql); |
||
329 | |||
330 | foreach ($query as $row) { |
||
331 | // fputcsv($stream, $row, $separator); |
||
332 | |||
333 | // force quotes |
||
334 | fputs($stream, implode(",", array_map("self::encodeFunc", $row)) . "\n"); |
||
335 | } |
||
336 | |||
337 | rewind($stream); |
||
338 | $fileData = stream_get_contents($stream); |
||
339 | fclose($stream); |
||
340 | |||
341 | return $fileData; |
||
342 | } |
||
441 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths