BurningFlipside /
CommonCode
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Serialize; |
||
| 3 | |||
| 4 | require_once dirname(__FILE__).'/../libs/PHPExcel/Classes/PHPExcel.php'; |
||
| 5 | |||
| 6 | class ExcelSerializer extends SpreadSheetSerializer |
||
| 7 | { |
||
| 8 | protected $types = array('xlsx', 'xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'); |
||
| 9 | |||
| 10 | public function serializeData($type, $array) |
||
| 11 | { |
||
| 12 | if($this->supportsType($type) === false) |
||
| 13 | { |
||
| 14 | return null; |
||
| 15 | } |
||
| 16 | if(count($array) === 0) |
||
| 17 | { |
||
| 18 | return null; |
||
| 19 | } |
||
| 20 | $data = $this->getArray($array); |
||
| 21 | $ssheat = new \PHPExcel(); |
||
| 22 | $sheat = $ssheat->setActiveSheetIndex(0); |
||
| 23 | $keys = array_shift($data); |
||
| 24 | $rowCount = count($data); |
||
| 25 | $colCount = count($keys); |
||
| 26 | View Code Duplication | for($i = 0; $i < $colCount; $i++) |
|
|
0 ignored issues
–
show
|
|||
| 27 | { |
||
| 28 | $sheat->setCellValueByColumnAndRow($i, 1, $keys[$i]); |
||
| 29 | } |
||
| 30 | for($i = 0; $i < $rowCount; $i++) |
||
| 31 | { |
||
| 32 | View Code Duplication | for($j = 0; $j < $colCount; $j++) |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 33 | { |
||
| 34 | $sheat->setCellValueByColumnAndRow($j, (2 + $i), $data[$i][$j]); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | $writerType = 'Excel5'; |
||
| 38 | if(strcasecmp($type, 'xlsx') === 0 || strcasecmp($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') === 0) |
||
| 39 | { |
||
| 40 | $writerType = 'Excel2007'; |
||
| 41 | } |
||
| 42 | $writer = \PHPExcel_IOFactory::createWriter($ssheat, $writerType); |
||
| 43 | ob_start(); |
||
| 44 | $writer->save('php://output'); |
||
| 45 | return ob_get_clean(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.