| Conditions | 26 |
| Paths | 1240 |
| Total Lines | 262 |
| Code Lines | 148 |
| 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 |
||
| 38 | public function execute() |
||
| 39 | { |
||
| 40 | $options = $this->getOptions(); |
||
| 41 | $optionsExport = $options['settings']['export']; |
||
| 42 | |||
| 43 | $optionsRenderer = $this->getOptionsRenderer(); |
||
| 44 | |||
| 45 | $phpExcel = new PHPExcel(); |
||
| 46 | |||
| 47 | // Sheet 1 |
||
| 48 | $phpExcel->setActiveSheetIndex(0); |
||
| 49 | $sheet = $phpExcel->getActiveSheet(); |
||
| 50 | $sheet->setTitle($this->translate($optionsRenderer['sheetName'])); |
||
| 51 | |||
| 52 | if (true === $optionsRenderer['displayTitle']) { |
||
| 53 | $sheet->setCellValue('A' . $optionsRenderer['rowTitle'], $this->getTitle()); |
||
| 54 | $sheet->getStyle('A' . $optionsRenderer['rowTitle']) |
||
| 55 | ->getFont() |
||
| 56 | ->setSize(15); |
||
| 57 | } |
||
| 58 | |||
| 59 | /* |
||
| 60 | * Print settings |
||
| 61 | */ |
||
| 62 | $this->setPrinting($phpExcel); |
||
| 63 | |||
| 64 | /* |
||
| 65 | * Calculate column width |
||
| 66 | */ |
||
| 67 | $this->calculateColumnWidth($sheet, $this->getColumnsToExport()); |
||
| 68 | |||
| 69 | /* |
||
| 70 | * Header |
||
| 71 | */ |
||
| 72 | $xColumn = 0; |
||
| 73 | $yRow = $optionsRenderer['startRowData']; |
||
| 74 | foreach ($this->getColumnsToExport() as $col) { |
||
| 75 | /* @var $column Column\AbstractColumn */ |
||
| 76 | $sheet->setCellValueByColumnAndRow($xColumn, $yRow, $this->translate($col->getLabel())); |
||
| 77 | |||
| 78 | $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($xColumn))->setWidth($col->getWidth()); |
||
| 79 | |||
| 80 | ++$xColumn; |
||
| 81 | } |
||
| 82 | |||
| 83 | /* |
||
| 84 | * Data |
||
| 85 | */ |
||
| 86 | $yRow = $optionsRenderer['startRowData'] + 1; |
||
| 87 | foreach ($this->getData() as $row) { |
||
| 88 | $xColumn = 0; |
||
| 89 | foreach ($this->getColumnsToExport() as $col) { |
||
| 90 | /* @var $col Column\AbstractColumn */ |
||
| 91 | |||
| 92 | $value = $row[$col->getUniqueId()]; |
||
| 93 | if (is_array($value)) { |
||
| 94 | $value = implode(PHP_EOL, $value); |
||
| 95 | } |
||
| 96 | |||
| 97 | /* @var $column Column\AbstractColumn */ |
||
| 98 | $currentColumn = PHPExcel_Cell::stringFromColumnIndex($xColumn); |
||
| 99 | $cell = $sheet->getCell($currentColumn . $yRow); |
||
| 100 | |||
| 101 | switch (get_class($col->getType())) { |
||
| 102 | |||
| 103 | case Column\Type\Number::class: |
||
| 104 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC); |
||
| 105 | break; |
||
| 106 | |||
| 107 | case Column\Type\DateTime::class: |
||
| 108 | /* @var $dateType Column\Type\DateTime */ |
||
| 109 | $dateType = $col->getType(); |
||
| 110 | |||
| 111 | if (! $value instanceof \DateTime && is_scalar($value)) { |
||
| 112 | $value = \DateTime::createFromFormat($dateType->getSourceDateTimeFormat(), $value); |
||
| 113 | if ($value instanceof \DateTime) { |
||
| 114 | $value->setTimezone(new \DateTimeZone($dateType->getSourceTimezone())); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($value instanceof \DateTime) { |
||
| 119 | // only apply this if we have a date object (else leave it blank) |
||
| 120 | $value->setTimezone(new \DateTimeZone($dateType->getOutputTimezone())); |
||
| 121 | $cell->setValue(\PHPExcel_Shared_Date::PHPToExcel($value)); |
||
|
|
|||
| 122 | |||
| 123 | if ($dateType->getOutputPattern()) { |
||
| 124 | $outputPattern = $dateType->getOutputPattern(); |
||
| 125 | } else { |
||
| 126 | $outputPattern = \PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME; |
||
| 127 | } |
||
| 128 | |||
| 129 | $cell->getStyle() |
||
| 130 | ->getNumberFormat() |
||
| 131 | ->setFormatCode($outputPattern); |
||
| 132 | } |
||
| 133 | break; |
||
| 134 | |||
| 135 | default: |
||
| 136 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | $columnStyle = $sheet->getStyle($currentColumn . $yRow); |
||
| 141 | $columnStyle->getAlignment()->setWrapText(true); |
||
| 142 | |||
| 143 | /* |
||
| 144 | * Styles |
||
| 145 | */ |
||
| 146 | $styles = array_merge($this->getRowStyles(), $col->getStyles()); |
||
| 147 | foreach ($styles as $style) { |
||
| 148 | /* @var $style Column\Style\AbstractStyle */ |
||
| 149 | if ($style->isApply($row) === true) { |
||
| 150 | switch (get_class($style)) { |
||
| 151 | |||
| 152 | case Column\Style\Bold::class: |
||
| 153 | $columnStyle->getFont()->setBold(true); |
||
| 154 | break; |
||
| 155 | |||
| 156 | case Column\Style\Italic::class: |
||
| 157 | $columnStyle->getFont()->setItalic(true); |
||
| 158 | break; |
||
| 159 | |||
| 160 | case Column\Style\Color::class: |
||
| 161 | $columnStyle->getFont() |
||
| 162 | ->getColor() |
||
| 163 | ->setRGB($style->getRgbHexString()); |
||
| 164 | break; |
||
| 165 | |||
| 166 | case Column\Style\BackgroundColor::class: |
||
| 167 | $columnStyle->getFill()->applyFromArray([ |
||
| 168 | 'type' => \PHPExcel_Style_Fill::FILL_SOLID, |
||
| 169 | 'color' => [ |
||
| 170 | 'rgb' => $style->getRgbHexString(), |
||
| 171 | ], |
||
| 172 | ]); |
||
| 173 | break; |
||
| 174 | |||
| 175 | case Column\Style\Align::class: |
||
| 176 | switch ($style->getAlignment()) { |
||
| 177 | case Column\Style\Align::$RIGHT: |
||
| 178 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); |
||
| 179 | break; |
||
| 180 | case Column\Style\Align::$LEFT: |
||
| 181 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); |
||
| 182 | break; |
||
| 183 | case Column\Style\Align::$CENTER: |
||
| 184 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); |
||
| 185 | break; |
||
| 186 | case Column\Style\Align::$JUSTIFY: |
||
| 187 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); |
||
| 188 | break; |
||
| 189 | default: |
||
| 190 | //throw new \Exception('Not defined yet: "'.get_class($style->getAlignment()).'"'); |
||
| 191 | break; |
||
| 192 | } |
||
| 193 | |||
| 194 | break; |
||
| 195 | |||
| 196 | case Column\Style\Strikethrough::class: |
||
| 197 | $columnStyle->getFont()->setStrikethrough(true); |
||
| 198 | break; |
||
| 199 | |||
| 200 | case Column\Style\Html::class: |
||
| 201 | // @todo strip the html? |
||
| 202 | break; |
||
| 203 | |||
| 204 | default: |
||
| 205 | throw new \Exception('Not defined yet: "' . get_class($style) . '"'); |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | ++$xColumn; |
||
| 212 | } |
||
| 213 | |||
| 214 | ++$yRow; |
||
| 215 | } |
||
| 216 | |||
| 217 | /* |
||
| 218 | * Autofilter, freezing, ... |
||
| 219 | */ |
||
| 220 | $highest = $sheet->getHighestRowAndColumn(); |
||
| 221 | |||
| 222 | // Letzte Zeile merken |
||
| 223 | |||
| 224 | // Autofilter + Freeze |
||
| 225 | $sheet->setAutoFilter('A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $highest['row']); |
||
| 226 | $freezeRow = $optionsRenderer['startRowData'] + 1; |
||
| 227 | $sheet->freezePane('A' . $freezeRow); |
||
| 228 | |||
| 229 | // repeat the data header for each page! |
||
| 230 | $sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd($optionsRenderer['startRowData'], $optionsRenderer['startRowData']); |
||
| 231 | |||
| 232 | // highlight header line |
||
| 233 | $style = [ |
||
| 234 | 'font' => [ |
||
| 235 | 'bold' => true, |
||
| 236 | ], |
||
| 237 | |||
| 238 | 'borders' => [ |
||
| 239 | 'allborders' => [ |
||
| 240 | 'style' => PHPExcel_Style_Border::BORDER_MEDIUM, |
||
| 241 | 'color' => [ |
||
| 242 | 'argb' => PHPExcel_Style_Color::COLOR_BLACK, |
||
| 243 | ], |
||
| 244 | ], |
||
| 245 | ], |
||
| 246 | 'fill' => [ |
||
| 247 | 'type' => PHPExcel_Style_Fill::FILL_SOLID, |
||
| 248 | 'startcolor' => [ |
||
| 249 | 'argb' => PHPExcel_Style_Color::COLOR_YELLOW, |
||
| 250 | ], |
||
| 251 | ], |
||
| 252 | ]; |
||
| 253 | $range = 'A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $optionsRenderer['startRowData']; |
||
| 254 | $sheet->getStyle($range)->applyFromArray($style); |
||
| 255 | |||
| 256 | // print borders |
||
| 257 | $range = 'A' . $freezeRow . ':' . $highest['column'] . $highest['row']; |
||
| 258 | $sheet->getStyle($range)->applyFromArray([ |
||
| 259 | 'borders' => [ |
||
| 260 | 'allborders' => [ |
||
| 261 | 'style' => PHPExcel_Style_Border::BORDER_THIN, |
||
| 262 | ], |
||
| 263 | ], |
||
| 264 | ]); |
||
| 265 | |||
| 266 | /* |
||
| 267 | * Save the file |
||
| 268 | */ |
||
| 269 | $path = $optionsExport['path']; |
||
| 270 | $saveFilename = date('Y-m-d_H-i-s') . $this->getCacheId() . '.xlsx'; |
||
| 271 | |||
| 272 | $excelWriter = new \PHPExcel_Writer_Excel2007($phpExcel); |
||
| 273 | $excelWriter->setPreCalculateFormulas(false); |
||
| 274 | $excelWriter->save($path . '/' . $saveFilename); |
||
| 275 | |||
| 276 | /* |
||
| 277 | * Send the response stream |
||
| 278 | */ |
||
| 279 | $response = new ResponseStream(); |
||
| 280 | $response->setStream(fopen($path . '/' . $saveFilename, 'r')); |
||
| 281 | |||
| 282 | $headers = new Headers(); |
||
| 283 | $headers->addHeaders([ |
||
| 284 | 'Content-Type' => [ |
||
| 285 | 'application/force-download', |
||
| 286 | 'application/octet-stream', |
||
| 287 | 'application/download', |
||
| 288 | ], |
||
| 289 | 'Content-Length' => filesize($path . '/' . $saveFilename), |
||
| 290 | 'Content-Disposition' => 'attachment;filename=' . $this->getFilename() . '.xlsx', |
||
| 291 | 'Cache-Control' => 'must-revalidate', |
||
| 292 | 'Pragma' => 'no-cache', |
||
| 293 | 'Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT', |
||
| 294 | ]); |
||
| 295 | |||
| 296 | $response->setHeaders($headers); |
||
| 297 | |||
| 298 | return $response; |
||
| 299 | } |
||
| 300 | |||
| 396 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: