Conditions | 16 |
Paths | 60 |
Total Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
32 | public function generateExportFileData($gridField) |
||
33 | { |
||
34 | $separator = $this->csvSeparator; |
||
35 | $csvColumns = ($this->exportColumns) |
||
36 | ? $this->exportColumns |
||
37 | : singleton($gridField->getModelClass())->summaryFields(); |
||
38 | $fileData = ''; |
||
39 | $columnData = array(); |
||
|
|||
40 | |||
41 | if ($this->csvHasHeader) { |
||
42 | $headers = array(); |
||
43 | |||
44 | // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the |
||
45 | // source name as the header instead |
||
46 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
47 | if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) { |
||
48 | $headers[] = $columnHeader['title']; |
||
49 | } else { |
||
50 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) |
||
51 | ? $columnSource |
||
52 | : $columnHeader; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | $fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\""; |
||
57 | $fileData .= "\n"; |
||
58 | } |
||
59 | |||
60 | // The is the only variation from the parent, using getList() instead of getManipulatedList() |
||
61 | $items = $gridField->getList(); |
||
62 | |||
63 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
||
64 | foreach ($gridField->getConfig()->getComponents() as $component) { |
||
65 | if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
||
66 | $items = $component->getManipulatedData($gridField, $items); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | foreach ($items->limit(null) as $item) { |
||
71 | $columnData = array(); |
||
72 | |||
73 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
74 | if (!is_string($columnHeader) && is_callable($columnHeader)) { |
||
75 | if ($item->hasMethod($columnSource)) { |
||
76 | $relObj = $item->{$columnSource}(); |
||
77 | } else { |
||
78 | $relObj = $item->relObject($columnSource); |
||
79 | } |
||
80 | |||
81 | $value = $columnHeader($relObj); |
||
82 | } else { |
||
83 | $value = $gridField->getDataFieldValue($item, $columnSource); |
||
84 | } |
||
85 | |||
86 | $value = str_replace(array("\r", "\n"), "\n", $value); |
||
87 | $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; |
||
88 | } |
||
89 | $fileData .= implode($separator, $columnData); |
||
90 | $fileData .= "\n"; |
||
91 | |||
92 | $item->destroy(); |
||
93 | } |
||
94 | |||
95 | return $fileData; |
||
96 | } |
||
97 | } |
||
98 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.