Conditions | 22 |
Paths | 60 |
Total Lines | 82 |
Code Lines | 44 |
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 |
||
129 | public function generateExportFileData($gridField) { |
||
130 | $separator = $this->getCsvSeparator(); |
||
131 | $csvColumns = $this->getExportColumnsForGridField($gridField); |
||
132 | $fileData = array(); |
||
133 | |||
134 | if($this->csvHasHeader) { |
||
135 | $headers = array(); |
||
136 | |||
137 | // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the |
||
138 | // source name as the header instead |
||
139 | |||
140 | foreach($csvColumns as $columnSource => $columnHeader) { |
||
141 | if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) { |
||
142 | $headers[] = $columnHeader['title']; |
||
143 | } else { |
||
144 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $fileData[] = $headers; |
||
149 | } |
||
150 | |||
151 | //Remove GridFieldPaginator as we're going to export the entire list. |
||
152 | $gridField->getConfig()->removeComponentsByType('GridFieldPaginator'); |
||
153 | |||
154 | $items = $gridField->getManipulatedList(); |
||
155 | |||
156 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
||
157 | foreach($gridField->getConfig()->getComponents() as $component){ |
||
158 | if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
||
159 | $items = $component->getManipulatedData($gridField, $items); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | foreach($items->limit(null) as $item) { |
||
164 | if(!$item->hasMethod('canView') || $item->canView()) { |
||
165 | $columnData = array(); |
||
166 | |||
167 | foreach($csvColumns as $columnSource => $columnHeader) { |
||
168 | if(!is_string($columnHeader) && is_callable($columnHeader)) { |
||
169 | if($item->hasMethod($columnSource)) { |
||
170 | $relObj = $item->{$columnSource}(); |
||
171 | } else { |
||
172 | $relObj = $item->relObject($columnSource); |
||
173 | } |
||
174 | |||
175 | $value = $columnHeader($relObj); |
||
176 | } else { |
||
177 | $value = $gridField->getDataFieldValue($item, $columnSource); |
||
178 | |||
179 | if($value === null) { |
||
180 | $value = $gridField->getDataFieldValue($item, $columnHeader); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | $value = str_replace(array("\r", "\n"), "\n", $value); |
||
185 | |||
186 | // [SS-2017-007] Sanitise XLS executable column values with a leading tab |
||
187 | if (!Config::inst()->get(get_class($this), 'xls_export_disabled') |
||
188 | && preg_match('/^[-@=+].*/', $value) |
||
189 | ) { |
||
190 | $value = "\t" . $value; |
||
191 | } |
||
192 | $columnData[] = $value; |
||
193 | } |
||
194 | |||
195 | $fileData[] = $columnData; |
||
196 | } |
||
197 | |||
198 | if($item->hasMethod('destroy')) { |
||
199 | $item->destroy(); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | // Convert the $fileData array into csv by capturing fputcsv's output |
||
204 | $csv = fopen('php://temp', 'r+'); |
||
205 | foreach($fileData as $line) { |
||
206 | fputcsv($csv, $line, $separator); |
||
207 | } |
||
208 | rewind($csv); |
||
209 | return stream_get_contents($csv); |
||
210 | } |
||
211 | |||
259 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.