| Conditions | 22 | 
| Paths | 456 | 
| Total Lines | 245 | 
| Code Lines | 140 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 14 | ||
| Bugs | 0 | Features | 2 | 
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 \ZfcDatagrid\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 \ZfcDatagrid\Column\AbstractColumn */  | 
            ||
| 90 | |||
| 91 | $value = $row[$col->getUniqueId()];  | 
            ||
| 92 |                 if (is_array($value)) { | 
            ||
| 93 | $value = implode(PHP_EOL, $value);  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | /* @var $column \ZfcDatagrid\Column\AbstractColumn */  | 
            ||
| 97 | $currentColumn = PHPExcel_Cell::stringFromColumnIndex($xColumn);  | 
            ||
| 98 | $cell = $sheet->getCell($currentColumn . $yRow);  | 
            ||
| 99 | |||
| 100 |                 switch (get_class($col->getType())) { | 
            ||
| 101 | |||
| 102 | case 'ZfcDatagrid\Column\Type\Number':  | 
            ||
| 103 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);  | 
            ||
| 104 | break;  | 
            ||
| 105 | |||
| 106 | case 'ZfcDatagrid\Column\Type\DateTime':  | 
            ||
| 107 |                         if ($value instanceof \DateTime) { | 
            ||
| 108 | $value->setTimezone(new \DateTimeZone($col->getType()  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 109 | ->getOutputTimezone()));  | 
            ||
| 110 | }  | 
            ||
| 111 | $cell->setValue(\PHPExcel_Shared_Date::PHPToExcel($value));  | 
            ||
| 112 | $cell->getStyle()  | 
            ||
| 113 | ->getNumberFormat()  | 
            ||
| 114 | ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME);  | 
            ||
| 115 | break;  | 
            ||
| 116 | |||
| 117 | default:  | 
            ||
| 118 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);  | 
            ||
| 119 | break;  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | $columnStyle = $sheet->getStyle($currentColumn . $yRow);  | 
            ||
| 123 | $columnStyle->getAlignment()->setWrapText(true);  | 
            ||
| 124 | |||
| 125 | /*  | 
            ||
| 126 | * Styles  | 
            ||
| 127 | */  | 
            ||
| 128 | $styles = array_merge($this->getRowStyles(), $col->getStyles());  | 
            ||
| 129 |                 foreach ($styles as $style) { | 
            ||
| 130 | /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */  | 
            ||
| 131 |                     if ($style->isApply($row) === true) { | 
            ||
| 132 |                         switch (get_class($style)) { | 
            ||
| 133 | |||
| 134 | case 'ZfcDatagrid\Column\Style\Bold':  | 
            ||
| 135 | $columnStyle->getFont()->setBold(true);  | 
            ||
| 136 | break;  | 
            ||
| 137 | |||
| 138 | case 'ZfcDatagrid\Column\Style\Italic':  | 
            ||
| 139 | $columnStyle->getFont()->setItalic(true);  | 
            ||
| 140 | break;  | 
            ||
| 141 | |||
| 142 | case 'ZfcDatagrid\Column\Style\Color':  | 
            ||
| 143 | $columnStyle->getFont()  | 
            ||
| 144 | ->getColor()  | 
            ||
| 145 | ->setRGB($style->getRgbHexString());  | 
            ||
| 146 | break;  | 
            ||
| 147 | |||
| 148 | case 'ZfcDatagrid\Column\Style\BackgroundColor':  | 
            ||
| 149 | $columnStyle->getFill()->applyFromArray([  | 
            ||
| 150 | 'type' => \PHPExcel_Style_Fill::FILL_SOLID,  | 
            ||
| 151 | 'color' => [  | 
            ||
| 152 | 'rgb' => $style->getRgbHexString(),  | 
            ||
| 153 | ],  | 
            ||
| 154 | ]);  | 
            ||
| 155 | break;  | 
            ||
| 156 | |||
| 157 | case 'ZfcDatagrid\Column\Style\Align':  | 
            ||
| 158 |                                 switch ($style->getAlignment()) { | 
            ||
| 159 | case \ZfcDatagrid\Column\Style\Align::$RIGHT:  | 
            ||
| 160 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  | 
            ||
| 161 | break;  | 
            ||
| 162 | case \ZfcDatagrid\Column\Style\Align::$LEFT:  | 
            ||
| 163 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);  | 
            ||
| 164 | break;  | 
            ||
| 165 | case \ZfcDatagrid\Column\Style\Align::$CENTER:  | 
            ||
| 166 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  | 
            ||
| 167 | break;  | 
            ||
| 168 | case \ZfcDatagrid\Column\Style\Align::$JUSTIFY:  | 
            ||
| 169 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);  | 
            ||
| 170 | break;  | 
            ||
| 171 | default:  | 
            ||
| 172 |                                         //throw new \Exception('Not defined yet: "'.get_class($style->getAlignment()).'"'); | 
            ||
| 173 | break;  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | break;  | 
            ||
| 177 | |||
| 178 | case 'ZfcDatagrid\Column\Style\Strikethrough':  | 
            ||
| 179 | $columnStyle->getFont()->setStrikethrough(true);  | 
            ||
| 180 | break;  | 
            ||
| 181 | |||
| 182 | case 'ZfcDatagrid\Column\Style\Html':  | 
            ||
| 183 | // @todo strip the html?  | 
            ||
| 184 | break;  | 
            ||
| 185 | |||
| 186 | default:  | 
            ||
| 187 |                                 throw new \Exception('Not defined yet: "' . get_class($style) . '"'); | 
            ||
| 188 | break;  | 
            ||
| 189 | }  | 
            ||
| 190 | }  | 
            ||
| 191 | }  | 
            ||
| 192 | |||
| 193 | $xColumn ++;  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | $yRow ++;  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | /*  | 
            ||
| 200 | * Autofilter, freezing, ...  | 
            ||
| 201 | */  | 
            ||
| 202 | $highest = $sheet->getHighestRowAndColumn();  | 
            ||
| 203 | |||
| 204 | // Letzte Zeile merken  | 
            ||
| 205 | |||
| 206 | // Autofilter + Freeze  | 
            ||
| 207 |         $sheet->setAutoFilter('A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $highest['row']); | 
            ||
| 208 | $freezeRow = $optionsRenderer['startRowData'] + 1;  | 
            ||
| 209 |         $sheet->freezePane('A' . $freezeRow); | 
            ||
| 210 | |||
| 211 | // repeat the data header for each page!  | 
            ||
| 212 | $sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd($optionsRenderer['startRowData'], $optionsRenderer['startRowData']);  | 
            ||
| 213 | |||
| 214 | // highlight header line  | 
            ||
| 215 | $style = [  | 
            ||
| 216 | 'font' => [  | 
            ||
| 217 | 'bold' => true,  | 
            ||
| 218 | ],  | 
            ||
| 219 | |||
| 220 | 'borders' => [  | 
            ||
| 221 | 'allborders' => [  | 
            ||
| 222 | 'style' => PHPExcel_Style_Border::BORDER_MEDIUM,  | 
            ||
| 223 | 'color' => [  | 
            ||
| 224 | 'argb' => PHPExcel_Style_Color::COLOR_BLACK,  | 
            ||
| 225 | ],  | 
            ||
| 226 | ],  | 
            ||
| 227 | ],  | 
            ||
| 228 | 'fill' => [  | 
            ||
| 229 | 'type' => PHPExcel_Style_Fill::FILL_SOLID,  | 
            ||
| 230 | 'startcolor' => [  | 
            ||
| 231 | 'argb' => PHPExcel_Style_Color::COLOR_YELLOW,  | 
            ||
| 232 | ],  | 
            ||
| 233 | ],  | 
            ||
| 234 | ];  | 
            ||
| 235 | $range = 'A' . $optionsRenderer['startRowData'] . ':' . $highest['column'] . $optionsRenderer['startRowData'];  | 
            ||
| 236 | $sheet->getStyle($range)->applyFromArray($style);  | 
            ||
| 237 | |||
| 238 | // print borders  | 
            ||
| 239 | $range = 'A' . $freezeRow . ':' . $highest['column'] . $highest['row'];  | 
            ||
| 240 | $sheet->getStyle($range)->applyFromArray([  | 
            ||
| 241 | 'borders' => [  | 
            ||
| 242 | 'allborders' => [  | 
            ||
| 243 | 'style' => PHPExcel_Style_Border::BORDER_THIN,  | 
            ||
| 244 | ],  | 
            ||
| 245 | ],  | 
            ||
| 246 | ]);  | 
            ||
| 247 | |||
| 248 | /*  | 
            ||
| 249 | * Save the file  | 
            ||
| 250 | */  | 
            ||
| 251 | $path = $optionsExport['path'];  | 
            ||
| 252 |         $saveFilename = date('Y-m-d_H-i-s') . $this->getCacheId() . '.xlsx'; | 
            ||
| 253 | |||
| 254 | $excelWriter = new \PHPExcel_Writer_Excel2007($phpExcel);  | 
            ||
| 255 | $excelWriter->setPreCalculateFormulas(false);  | 
            ||
| 256 | $excelWriter->save($path . '/' . $saveFilename);  | 
            ||
| 257 | |||
| 258 | /*  | 
            ||
| 259 | * Send the response stream  | 
            ||
| 260 | */  | 
            ||
| 261 | $response = new ResponseStream();  | 
            ||
| 262 | $response->setStream(fopen($path . '/' . $saveFilename, 'r'));  | 
            ||
| 263 | |||
| 264 | $headers = new Headers();  | 
            ||
| 265 | $headers->addHeaders([  | 
            ||
| 266 | 'Content-Type' => [  | 
            ||
| 267 | 'application/force-download',  | 
            ||
| 268 | 'application/octet-stream',  | 
            ||
| 269 | 'application/download',  | 
            ||
| 270 | ],  | 
            ||
| 271 | 'Content-Length' => filesize($path . '/' . $saveFilename),  | 
            ||
| 272 | 'Content-Disposition' => 'attachment;filename=' . $this->getFilename() . '.xlsx',  | 
            ||
| 273 | 'Cache-Control' => 'must-revalidate',  | 
            ||
| 274 | 'Pragma' => 'no-cache',  | 
            ||
| 275 | 'Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT',  | 
            ||
| 276 | ]);  | 
            ||
| 277 | |||
| 278 | $response->setHeaders($headers);  | 
            ||
| 279 | |||
| 280 | return $response;  | 
            ||
| 281 | }  | 
            ||
| 282 | |||
| 378 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: