Conditions | 22 |
Paths | 60 |
Total Lines | 85 |
Code Lines | 45 |
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 |
||
166 | public function generateExportFileData($gridField) |
||
167 | { |
||
168 | $csvColumns = $this->getExportColumnsForGridField($gridField); |
||
169 | |||
170 | $csvWriter = Writer::createFromFileObject(new \SplTempFileObject()); |
||
171 | $csvWriter->setDelimiter($this->getCsvSeparator()); |
||
172 | $csvWriter->setEnclosure($this->getCsvEnclosure()); |
||
173 | $csvWriter->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries |
||
174 | $csvWriter->setOutputBOM(Writer::BOM_UTF8); |
||
175 | |||
176 | if (!Config::inst()->get(get_class($this), 'xls_export_disabled')) { |
||
177 | $csvWriter->addFormatter(function (array $row) { |
||
178 | foreach ($row as &$item) { |
||
179 | // [SS-2017-007] Sanitise XLS executable column values with a leading tab |
||
180 | if (preg_match('/^[-@=+].*/', $item)) { |
||
181 | $item = "\t" . $item; |
||
182 | } |
||
183 | } |
||
184 | return $row; |
||
185 | }); |
||
186 | } |
||
187 | |||
188 | if ($this->csvHasHeader) { |
||
189 | $headers = []; |
||
190 | |||
191 | // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the |
||
192 | // source name as the header instead |
||
193 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
194 | if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) { |
||
195 | $headers[] = $columnHeader['title']; |
||
196 | } else { |
||
197 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $csvWriter->insertOne($headers); |
||
202 | unset($headers); |
||
203 | } |
||
204 | |||
205 | //Remove GridFieldPaginator as we're going to export the entire list. |
||
206 | $gridField->getConfig()->removeComponentsByType(GridFieldPaginator::class); |
||
207 | |||
208 | $items = $gridField->getManipulatedList(); |
||
209 | |||
210 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
||
211 | foreach ($gridField->getConfig()->getComponents() as $component) { |
||
212 | if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
||
213 | $items = $component->getManipulatedData($gridField, $items); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** @var DataObject $item */ |
||
218 | foreach ($items->limit(null) as $item) { |
||
219 | if (!$item->hasMethod('canView') || $item->canView()) { |
||
220 | $columnData = []; |
||
221 | |||
222 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
223 | if (!is_string($columnHeader) && is_callable($columnHeader)) { |
||
224 | if ($item->hasMethod($columnSource)) { |
||
225 | $relObj = $item->{$columnSource}(); |
||
226 | } else { |
||
227 | $relObj = $item->relObject($columnSource); |
||
228 | } |
||
229 | |||
230 | $value = $columnHeader($relObj); |
||
231 | } else { |
||
232 | $value = $gridField->getDataFieldValue($item, $columnSource); |
||
233 | |||
234 | if ($value === null) { |
||
235 | $value = $gridField->getDataFieldValue($item, $columnHeader); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | $columnData[] = $value; |
||
240 | } |
||
241 | |||
242 | $csvWriter->insertOne($columnData); |
||
243 | } |
||
244 | |||
245 | if ($item->hasMethod('destroy')) { |
||
246 | $item->destroy(); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | return (string)$csvWriter; |
||
251 | } |
||
329 |