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