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