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 |
||
10 | class RichText implements IComparable |
||
11 | { |
||
12 | /** |
||
13 | * Rich text elements. |
||
14 | * |
||
15 | * @var ITextElement[] |
||
16 | */ |
||
17 | private $richTextElements; |
||
18 | |||
19 | /** |
||
20 | * Create a new RichText instance. |
||
21 | * |
||
22 | * @param Cell $pCell |
||
23 | * |
||
24 | * @throws Exception |
||
25 | */ |
||
26 | 39 | public function __construct(Cell $pCell = null) |
|
44 | |||
45 | /** |
||
46 | * Add text. |
||
47 | * |
||
48 | * @param ITextElement $pText Rich text element |
||
49 | * |
||
50 | * @throws Exception |
||
51 | * |
||
52 | * @return RichText |
||
53 | */ |
||
54 | 39 | public function addText(ITextElement $pText) |
|
60 | |||
61 | /** |
||
62 | * Create text. |
||
63 | * |
||
64 | * @param string $pText Text |
||
65 | * |
||
66 | * @throws Exception |
||
67 | * |
||
68 | * @return TextElement |
||
69 | */ |
||
70 | 24 | public function createText($pText) |
|
77 | |||
78 | /** |
||
79 | * Create text run. |
||
80 | * |
||
81 | * @param string $pText Text |
||
82 | * |
||
83 | * @throws Exception |
||
84 | * |
||
85 | * @return Run |
||
86 | */ |
||
87 | 30 | public function createTextRun($pText) |
|
94 | |||
95 | /** |
||
96 | * Get plain text. |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 14 | public function getPlainText() |
|
112 | |||
113 | /** |
||
114 | * Convert to string. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 2 | public function __toString() |
|
122 | |||
123 | /** |
||
124 | * Get Rich Text elements. |
||
125 | * |
||
126 | * @return ITextElement[] |
||
127 | */ |
||
128 | 31 | public function getRichTextElements() |
|
132 | |||
133 | /** |
||
134 | * Set Rich Text elements. |
||
135 | * |
||
136 | * @param ITextElement[] $textElements Array of elements |
||
137 | * |
||
138 | * @throws Exception |
||
139 | * |
||
140 | * @return RichText |
||
141 | */ |
||
142 | public function setRichTextElements(array $textElements) |
||
148 | |||
149 | /** |
||
150 | * Get hash code. |
||
151 | * |
||
152 | * @return string Hash code |
||
153 | */ |
||
154 | 10 | public function getHashCode() |
|
166 | |||
167 | /** |
||
168 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
169 | */ |
||
170 | 1 | View Code Duplication | public function __clone() |
181 | } |
||
182 |
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.