Conditions | 35 |
Paths | > 20000 |
Total Lines | 154 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 3 | 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 |
||
201 | public function generateExportFileData($gridField) |
||
202 | { |
||
203 | $class = $gridField->getModelClass(); |
||
204 | $columns = ($this->exportColumns) ? $this->exportColumns : ExcelImportExport::exportFieldsForClass($class); |
||
205 | |||
206 | $singl = singleton($class); |
||
207 | |||
208 | $plural = $class ? $singl->i18n_plural_name() : ''; |
||
209 | |||
210 | $filter = new FileNameFilter; |
||
211 | if ($this->exportName) { |
||
212 | $this->exportName = $filter->filter($this->exportName); |
||
213 | } else { |
||
214 | $this->exportName = $filter->filter('export-' . $plural); |
||
215 | } |
||
216 | |||
217 | $excel = new Spreadsheet(); |
||
218 | $excelProperties = $excel->getProperties(); |
||
219 | $excelProperties->setTitle($this->exportName); |
||
220 | |||
221 | $sheet = $excel->getActiveSheet(); |
||
222 | if ($plural) { |
||
223 | $sheet->setTitle($plural); |
||
224 | } |
||
225 | |||
226 | $row = 1; |
||
227 | $col = 1; |
||
228 | |||
229 | if ($this->hasHeader) { |
||
230 | $headers = array(); |
||
231 | |||
232 | // determine the headers. If a field is callable (e.g. anonymous function) then use the |
||
233 | // source name as the header instead |
||
234 | foreach ($columns as $columnSource => $columnHeader) { |
||
235 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) |
||
236 | ? $columnSource : $columnHeader; |
||
237 | } |
||
238 | |||
239 | foreach ($headers as $header) { |
||
240 | $sheet->setCellValue([$col, $row], $header); |
||
241 | $col++; |
||
242 | } |
||
243 | |||
244 | $endcol = Coordinate::stringFromColumnIndex($col - 1); |
||
245 | $sheet->setAutoFilter("A1:{$endcol}1"); |
||
246 | $sheet->getStyle("A1:{$endcol}1")->getFont()->setBold(true); |
||
247 | |||
248 | $col = 1; |
||
249 | $row++; |
||
250 | } |
||
251 | |||
252 | // Autosize |
||
253 | $cellIterator = $sheet->getRowIterator()->current()->getCellIterator(); |
||
254 | try { |
||
255 | $cellIterator->setIterateOnlyExistingCells(true); |
||
256 | } catch (Exception $ex) { |
||
257 | // Ignore exceptions |
||
258 | } |
||
259 | foreach ($cellIterator as $cell) { |
||
260 | $sheet->getColumnDimension($cell->getColumn())->setAutoSize(true); |
||
261 | } |
||
262 | |||
263 | //Remove GridFieldPaginator as we're going to export the entire list. |
||
264 | $gridField->getConfig()->removeComponentsByType(GridFieldPaginator::class); |
||
265 | |||
266 | $items = $gridField->getManipulatedList(); |
||
267 | |||
268 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
||
269 | foreach ($gridField->getConfig()->getComponents() as $component) { |
||
270 | if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
||
271 | $items = $component->getManipulatedData($gridField, $items); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | $list = $items; |
||
276 | $limit = ExcelImportExport::$limit_exports; |
||
277 | if ($items instanceof DataList && $this->isLimited && $limit > 0) { |
||
278 | $list = $items->limit($limit); |
||
279 | if (!empty($this->listFilters)) { |
||
280 | $list = $list->filter($this->listFilters); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | if (!$list) { |
||
285 | return $excel; |
||
286 | } |
||
287 | |||
288 | $exportFormat = ExcelImportExport::config()->export_format; |
||
289 | |||
290 | foreach ($list as $item) { |
||
291 | if ($this->checkCanView) { |
||
292 | $canView = true; |
||
293 | if ($item->hasMethod('canView') && !$item->canView()) { |
||
294 | $canView = false; |
||
295 | } |
||
296 | if (!$canView) { |
||
297 | continue; |
||
298 | } |
||
299 | } |
||
300 | foreach ($columns as $columnSource => $columnHeader) { |
||
301 | if (!is_string($columnHeader) && is_callable($columnHeader)) { |
||
302 | if ($item->hasMethod($columnSource)) { |
||
303 | $relObj = $item->{$columnSource}(); |
||
304 | } else { |
||
305 | $relObj = $item->relObject($columnSource); |
||
306 | } |
||
307 | |||
308 | $value = $columnHeader($relObj); |
||
309 | } else { |
||
310 | if (is_string($columnSource)) { |
||
311 | // It can be a method |
||
312 | if (strpos($columnSource, '(') !== false) { |
||
313 | $matches = []; |
||
314 | preg_match('/([a-zA-Z]*)\((.*)\)/', $columnSource, $matches); |
||
315 | $func = $matches[1]; |
||
316 | $params = explode(",", $matches[2]); |
||
317 | // Support only one param for now |
||
318 | $value = $item->$func($params[0]); |
||
319 | } else { |
||
320 | if (array_key_exists($columnSource, $exportFormat)) { |
||
321 | $format = $exportFormat[$columnSource]; |
||
322 | $value = $item->dbObject($columnSource)->$format(); |
||
323 | } else { |
||
324 | $value = $gridField->getDataFieldValue($item, $columnSource); |
||
325 | } |
||
326 | } |
||
327 | } else { |
||
328 | // We can also use a simple dot notation |
||
329 | $parts = explode(".", $columnHeader); |
||
330 | if (count($parts) == 1) { |
||
331 | $value = $item->$columnHeader; |
||
332 | } else { |
||
333 | $value = $item->relObject($parts[0]); |
||
334 | if ($value) { |
||
335 | $relObjField = $parts[1]; |
||
336 | $value = $value->$relObjField; |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | $sheet->setCellValue([$col, $row], $value); |
||
343 | $col++; |
||
344 | } |
||
345 | |||
346 | if ($item->hasMethod('destroy')) { |
||
347 | $item->destroy(); |
||
348 | } |
||
349 | |||
350 | $col = 1; |
||
351 | $row++; |
||
352 | } |
||
353 | |||
354 | return $excel; |
||
355 | } |
||
523 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.