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 |
||
29 | class Ods extends BaseWriter implements IWriter |
||
30 | { |
||
31 | /** |
||
32 | * Private writer parts. |
||
33 | * |
||
34 | * @var Ods\WriterPart[] |
||
35 | */ |
||
36 | private $writerParts = []; |
||
37 | |||
38 | /** |
||
39 | * Private PhpSpreadsheet. |
||
40 | * |
||
41 | * @var PhpSpreadsheet |
||
42 | */ |
||
43 | private $spreadSheet; |
||
44 | |||
45 | /** |
||
46 | * Create a new Ods. |
||
47 | * |
||
48 | * @param \PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet |
||
49 | */ |
||
50 | 2 | public function __construct(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet = null) |
|
51 | { |
||
52 | 2 | $this->setSpreadsheet($spreadsheet); |
|
53 | |||
54 | $writerPartsArray = [ |
||
55 | 2 | 'content' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Content::class, |
|
56 | 'meta' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Meta::class, |
||
57 | 'meta_inf' => \PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf::class, |
||
58 | 'mimetype' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype::class, |
||
59 | 'settings' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Settings::class, |
||
60 | 'styles' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Styles::class, |
||
61 | 'thumbnails' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails::class, |
||
62 | ]; |
||
63 | |||
64 | 2 | foreach ($writerPartsArray as $writer => $class) { |
|
65 | 2 | $this->writerParts[$writer] = new $class($this); |
|
66 | } |
||
67 | 2 | } |
|
68 | |||
69 | /** |
||
70 | * Get writer part. |
||
71 | * |
||
72 | * @param string $pPartName Writer part name |
||
73 | * |
||
74 | * @return Ods\WriterPart|null |
||
75 | */ |
||
76 | View Code Duplication | public function getWriterPart($pPartName = '') |
|
84 | |||
85 | /** |
||
86 | * Save PhpSpreadsheet to file. |
||
87 | * |
||
88 | * @param string $pFilename |
||
89 | * |
||
90 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
91 | */ |
||
92 | public function save($pFilename = null) |
||
133 | |||
134 | /** |
||
135 | * Create zip object. |
||
136 | * |
||
137 | * @param string $pFilename |
||
138 | * |
||
139 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
140 | * |
||
141 | * @return ZipArchive |
||
142 | */ |
||
143 | private function createZip($pFilename) |
||
167 | |||
168 | /** |
||
169 | * Get Spreadsheet object. |
||
170 | * |
||
171 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
172 | * |
||
173 | * @return Spreadsheet |
||
174 | */ |
||
175 | 2 | public function getSpreadsheet() |
|
182 | |||
183 | /** |
||
184 | * Set Spreadsheet object. |
||
185 | * |
||
186 | * @param \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet PhpSpreadsheet object |
||
187 | * |
||
188 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | 2 | public function setSpreadsheet(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet = null) |
|
198 | } |
||
199 |
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.