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 |
||
27 | class Protection extends Supervisor implements \PhpOffice\PhpSpreadsheet\IComparable |
||
28 | { |
||
29 | /** Protection styles */ |
||
30 | const PROTECTION_INHERIT = 'inherit'; |
||
31 | const PROTECTION_PROTECTED = 'protected'; |
||
32 | const PROTECTION_UNPROTECTED = 'unprotected'; |
||
33 | |||
34 | /** |
||
35 | * Locked. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $locked; |
||
40 | |||
41 | /** |
||
42 | * Hidden. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $hidden; |
||
47 | |||
48 | /** |
||
49 | * Create a new Protection. |
||
50 | * |
||
51 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
52 | * Leave this value at default unless you understand exactly what |
||
53 | * its ramifications are |
||
54 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
55 | * Leave this value at default unless you understand exactly what |
||
56 | * its ramifications are |
||
57 | */ |
||
58 | 73 | public function __construct($isSupervisor = false, $isConditional = false) |
|
69 | |||
70 | /** |
||
71 | * Get the shared style component for the currently active cell in currently active sheet. |
||
72 | * Only used for style supervisor. |
||
73 | * |
||
74 | * @return Protection |
||
75 | */ |
||
76 | public function getSharedComponent() |
||
80 | |||
81 | /** |
||
82 | * Build style array from subcomponents. |
||
83 | * |
||
84 | * @param array $array |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 13 | public function getStyleArray($array) |
|
92 | |||
93 | /** |
||
94 | * Apply styles from array. |
||
95 | * |
||
96 | * <code> |
||
97 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray( |
||
98 | * array( |
||
99 | * 'locked' => TRUE, |
||
100 | * 'hidden' => FALSE |
||
101 | * ) |
||
102 | * ); |
||
103 | * </code> |
||
104 | * |
||
105 | * @param array $pStyles Array containing style information |
||
106 | * |
||
107 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
108 | * |
||
109 | * @return Protection |
||
110 | */ |
||
111 | 13 | View Code Duplication | public function applyFromArray($pStyles = null) |
|
|||
112 | { |
||
113 | 13 | if (is_array($pStyles)) { |
|
114 | 13 | if ($this->isSupervisor) { |
|
115 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
||
116 | } else { |
||
117 | 13 | if (isset($pStyles['locked'])) { |
|
118 | 13 | $this->setLocked($pStyles['locked']); |
|
119 | } |
||
120 | 13 | if (isset($pStyles['hidden'])) { |
|
121 | $this->setHidden($pStyles['hidden']); |
||
122 | } |
||
123 | } |
||
124 | } else { |
||
125 | throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid style array passed.'); |
||
126 | } |
||
127 | |||
128 | 13 | return $this; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get locked. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 58 | public function getLocked() |
|
144 | |||
145 | /** |
||
146 | * Set locked. |
||
147 | * |
||
148 | * @param string $pValue |
||
149 | * |
||
150 | * @return Protection |
||
151 | */ |
||
152 | 16 | View Code Duplication | public function setLocked($pValue = self::PROTECTION_INHERIT) |
163 | |||
164 | /** |
||
165 | * Get hidden. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | 58 | public function getHidden() |
|
177 | |||
178 | /** |
||
179 | * Set hidden. |
||
180 | * |
||
181 | * @param string $pValue |
||
182 | * |
||
183 | * @return Protection |
||
184 | */ |
||
185 | 4 | View Code Duplication | public function setHidden($pValue = self::PROTECTION_INHERIT) |
196 | |||
197 | /** |
||
198 | * Get hash code. |
||
199 | * |
||
200 | * @return string Hash code |
||
201 | */ |
||
202 | 69 | public function getHashCode() |
|
214 | } |
||
215 |
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.