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 |
||
8 | class Fill extends Supervisor implements IComparable |
||
9 | { |
||
10 | /* Fill types */ |
||
11 | const FILL_NONE = 'none'; |
||
12 | const FILL_SOLID = 'solid'; |
||
13 | const FILL_GRADIENT_LINEAR = 'linear'; |
||
14 | const FILL_GRADIENT_PATH = 'path'; |
||
15 | const FILL_PATTERN_DARKDOWN = 'darkDown'; |
||
16 | const FILL_PATTERN_DARKGRAY = 'darkGray'; |
||
17 | const FILL_PATTERN_DARKGRID = 'darkGrid'; |
||
18 | const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal'; |
||
19 | const FILL_PATTERN_DARKTRELLIS = 'darkTrellis'; |
||
20 | const FILL_PATTERN_DARKUP = 'darkUp'; |
||
21 | const FILL_PATTERN_DARKVERTICAL = 'darkVertical'; |
||
22 | const FILL_PATTERN_GRAY0625 = 'gray0625'; |
||
23 | const FILL_PATTERN_GRAY125 = 'gray125'; |
||
24 | const FILL_PATTERN_LIGHTDOWN = 'lightDown'; |
||
25 | const FILL_PATTERN_LIGHTGRAY = 'lightGray'; |
||
26 | const FILL_PATTERN_LIGHTGRID = 'lightGrid'; |
||
27 | const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal'; |
||
28 | const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis'; |
||
29 | const FILL_PATTERN_LIGHTUP = 'lightUp'; |
||
30 | const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical'; |
||
31 | const FILL_PATTERN_MEDIUMGRAY = 'mediumGray'; |
||
32 | |||
33 | /** |
||
34 | * Fill type. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $fillType = self::FILL_NONE; |
||
39 | |||
40 | /** |
||
41 | * Rotation. |
||
42 | * |
||
43 | * @var float |
||
44 | */ |
||
45 | protected $rotation = 0; |
||
46 | |||
47 | /** |
||
48 | * Start color. |
||
49 | * |
||
50 | * @var Color |
||
51 | */ |
||
52 | protected $startColor; |
||
53 | |||
54 | /** |
||
55 | * End color. |
||
56 | * |
||
57 | * @var Color |
||
58 | */ |
||
59 | protected $endColor; |
||
60 | |||
61 | /** |
||
62 | * Create a new Fill. |
||
63 | * |
||
64 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
65 | * Leave this value at default unless you understand exactly what |
||
66 | * its ramifications are |
||
67 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
68 | * Leave this value at default unless you understand exactly what |
||
69 | * its ramifications are |
||
70 | */ |
||
71 | 28 | public function __construct($isSupervisor = false, $isConditional = false) |
|
72 | { |
||
73 | // Supervisor? |
||
74 | 28 | parent::__construct($isSupervisor); |
|
75 | |||
76 | // Initialise values |
||
77 | 28 | if ($isConditional) { |
|
78 | $this->fillType = null; |
||
79 | } |
||
80 | 28 | $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional); |
|
81 | 28 | $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
|
82 | |||
83 | // bind parent if we are a supervisor |
||
84 | 28 | if ($isSupervisor) { |
|
85 | 28 | $this->startColor->bindParent($this, 'startColor'); |
|
86 | 28 | $this->endColor->bindParent($this, 'endColor'); |
|
87 | } |
||
88 | 28 | } |
|
89 | |||
90 | /** |
||
91 | * Get the shared style component for the currently active cell in currently active sheet. |
||
92 | * Only used for style supervisor. |
||
93 | * |
||
94 | * @return Fill |
||
95 | */ |
||
96 | 1 | public function getSharedComponent() |
|
100 | |||
101 | /** |
||
102 | * Build style array from subcomponents. |
||
103 | * |
||
104 | * @param array $array |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 2 | public function getStyleArray($array) |
|
112 | |||
113 | /** |
||
114 | * Apply styles from array. |
||
115 | * <code> |
||
116 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray( |
||
117 | * array( |
||
118 | * 'fillType' => Fill::FILL_GRADIENT_LINEAR, |
||
119 | * 'rotation' => 0, |
||
120 | * 'startColor' => array( |
||
121 | * 'rgb' => '000000' |
||
122 | * ), |
||
123 | * 'endColor' => array( |
||
124 | * 'argb' => 'FFFFFFFF' |
||
125 | * ) |
||
126 | * ) |
||
127 | * ); |
||
128 | * </code>. |
||
129 | * |
||
130 | * @param array $pStyles Array containing style information |
||
131 | * |
||
132 | * @throws PhpSpreadsheetException |
||
133 | * |
||
134 | * @return Fill |
||
135 | */ |
||
136 | 3 | public function applyFromArray(array $pStyles) |
|
161 | |||
162 | /** |
||
163 | * Get Fill Type. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | 11 | public function getFillType() |
|
175 | |||
176 | /** |
||
177 | * Set Fill Type. |
||
178 | * |
||
179 | * @param string $pValue Fill type, see self::FILL_* |
||
180 | * |
||
181 | * @return Fill |
||
182 | */ |
||
183 | 4 | View Code Duplication | public function setFillType($pValue) |
194 | |||
195 | /** |
||
196 | * Get Rotation. |
||
197 | * |
||
198 | * @return float |
||
199 | */ |
||
200 | 10 | public function getRotation() |
|
208 | |||
209 | /** |
||
210 | * Set Rotation. |
||
211 | * |
||
212 | * @param float $pValue |
||
213 | * |
||
214 | * @return Fill |
||
215 | */ |
||
216 | 1 | View Code Duplication | public function setRotation($pValue) |
227 | |||
228 | /** |
||
229 | * Get Start Color. |
||
230 | * |
||
231 | * @return Color |
||
232 | */ |
||
233 | 11 | public function getStartColor() |
|
237 | |||
238 | /** |
||
239 | * Set Start Color. |
||
240 | * |
||
241 | * @param Color $pValue |
||
242 | * |
||
243 | * @throws PhpSpreadsheetException |
||
244 | * |
||
245 | * @return Fill |
||
246 | */ |
||
247 | 1 | View Code Duplication | public function setStartColor(Color $pValue) |
261 | |||
262 | /** |
||
263 | * Get End Color. |
||
264 | * |
||
265 | * @return Color |
||
266 | */ |
||
267 | 11 | public function getEndColor() |
|
271 | |||
272 | /** |
||
273 | * Set End Color. |
||
274 | * |
||
275 | * @param Color $pValue |
||
276 | * |
||
277 | * @throws PhpSpreadsheetException |
||
278 | * |
||
279 | * @return Fill |
||
280 | */ |
||
281 | View Code Duplication | public function setEndColor(Color $pValue) |
|
295 | |||
296 | /** |
||
297 | * Get hash code. |
||
298 | * |
||
299 | * @return string Hash code |
||
300 | */ |
||
301 | 10 | public function getHashCode() |
|
315 | } |
||
316 |
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.