Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class ExcelWriter extends AbstractWriter |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $filename; |
||
20 | |||
21 | /** |
||
22 | * @var null|string |
||
23 | */ |
||
24 | protected $sheet; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $type; |
||
30 | |||
31 | /** |
||
32 | * @var boolean |
||
33 | */ |
||
34 | protected $prependHeaderRow; |
||
35 | |||
36 | /** |
||
37 | * @var PHPExcel |
||
38 | */ |
||
39 | protected $excel; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | protected $row = 1; |
||
45 | |||
46 | /** |
||
47 | * @param \SplFileObject $file File |
||
48 | * @param string $sheet Sheet title (optional) |
||
49 | * @param string $type Excel file type (defaults to Excel2007) |
||
50 | * @param boolean $prependHeaderRow |
||
51 | */ |
||
52 | 2 | public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 2 | public function prepare() |
|
64 | { |
||
65 | 2 | $reader = PHPExcel_IOFactory::createReader($this->type); |
|
66 | 2 | if ($reader->canRead($this->filename)) { |
|
67 | $this->excel = $reader->load($this->filename); |
||
68 | } else { |
||
69 | 2 | $this->excel = new PHPExcel(); |
|
70 | } |
||
71 | |||
72 | 2 | if (null !== $this->sheet) { |
|
73 | if (!$this->excel->sheetNameExists($this->sheet)) { |
||
74 | $this->excel->createSheet()->setTitle($this->sheet); |
||
75 | } |
||
76 | $this->excel->setActiveSheetIndexByName($this->sheet); |
||
77 | } |
||
78 | 2 | } |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 2 | public function writeItem(array $item) |
|
84 | { |
||
85 | 2 | $count = count($item); |
|
86 | |||
87 | 2 | if ($this->prependHeaderRow && 1 == $this->row) { |
|
88 | $headers = array_keys($item); |
||
89 | |||
90 | View Code Duplication | for ($i = 0; $i < $count; $i++) { |
|
|
|||
91 | $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]); |
||
92 | } |
||
93 | $this->row++; |
||
94 | } |
||
95 | |||
96 | 2 | $values = array_values($item); |
|
97 | |||
98 | 2 | View Code Duplication | for ($i = 0; $i < $count; $i++) { |
99 | 2 | $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]); |
|
100 | } |
||
101 | |||
102 | 2 | $this->row++; |
|
103 | 2 | } |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 2 | public function finish() |
|
113 | } |
||
114 |
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.