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 |
||
19 | class Statement |
||
20 | { |
||
21 | /** |
||
22 | * @var string The unique identifier |
||
23 | */ |
||
24 | private $id; |
||
25 | |||
26 | /** |
||
27 | * @var Verb $verb The {@link Verb} |
||
28 | */ |
||
29 | private $verb; |
||
30 | |||
31 | /** |
||
32 | * @var Actor The {@link Actor} |
||
33 | */ |
||
34 | private $actor; |
||
35 | |||
36 | /** |
||
37 | * @var Object The {@link Object} |
||
38 | */ |
||
39 | private $object; |
||
40 | |||
41 | /** |
||
42 | * @var Result The {@link Activity} {@link Result} |
||
43 | */ |
||
44 | private $result; |
||
45 | |||
46 | /** |
||
47 | * @var Actor The Authority that asserted the Statement true |
||
48 | */ |
||
49 | private $authority; |
||
50 | |||
51 | /** |
||
52 | * @var \DateTime The timestamp of when the events described in this statement occurred |
||
53 | */ |
||
54 | private $created; |
||
55 | |||
56 | /** |
||
57 | * @var \DateTime The timestamp of when this statement was recorded by the LRS |
||
58 | */ |
||
59 | private $stored; |
||
60 | |||
61 | public function __construct($id, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null) |
||
72 | |||
73 | /** |
||
74 | * Returns the Statement's unique identifier. |
||
75 | * |
||
76 | * @return string The identifier |
||
77 | */ |
||
78 | public function getId() |
||
82 | |||
83 | /** |
||
84 | * Returns the Statement's {@link Verb}. |
||
85 | * |
||
86 | * @return Verb The Verb |
||
87 | */ |
||
88 | public function getVerb() |
||
92 | |||
93 | /** |
||
94 | * Returns the Statement's {@link Actor}. |
||
95 | * |
||
96 | * @return Actor The Actor |
||
97 | */ |
||
98 | public function getActor() |
||
102 | |||
103 | /** |
||
104 | * Returns the Statement's {@link Object}. |
||
105 | * |
||
106 | * @return \Xabbuh\XApi\Model\Object The Object |
||
107 | */ |
||
108 | public function getObject() |
||
112 | |||
113 | /** |
||
114 | * Returns the {@link Activity} {@link Result}. |
||
115 | * |
||
116 | * @return Result The Result |
||
117 | */ |
||
118 | public function getResult() |
||
122 | |||
123 | /** |
||
124 | * Returns the Authority that asserted the Statement true. |
||
125 | * |
||
126 | * @return Actor The Authority |
||
127 | */ |
||
128 | public function getAuthority() |
||
132 | |||
133 | /** |
||
134 | * Returns the timestamp of when the events described in this statement |
||
135 | * occurred. |
||
136 | * |
||
137 | * @return \DateTime The timestamp |
||
138 | */ |
||
139 | public function getCreated() |
||
143 | |||
144 | /** |
||
145 | * Returns the timestamp of when this statement was recorded by the LRS. |
||
146 | * |
||
147 | * @return \DateTime The timestamp |
||
148 | */ |
||
149 | public function getStored() |
||
153 | |||
154 | /** |
||
155 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
156 | * another Statement). |
||
157 | * |
||
158 | * @return bool True if the Statement voids another Statement, false otherwise |
||
159 | */ |
||
160 | public function isVoidStatement() |
||
164 | |||
165 | /** |
||
166 | * Returns a {@link StatementReference} for the Statement. |
||
167 | * |
||
168 | * @return StatementReference The reference |
||
169 | */ |
||
170 | public function getStatementReference() |
||
176 | |||
177 | /** |
||
178 | * Returns a Statement that voids the current Statement. |
||
179 | * |
||
180 | * @param Actor $actor The Actor voiding this Statement |
||
181 | * |
||
182 | * @return Statement The voiding Statement |
||
183 | */ |
||
184 | public function getVoidStatement(Actor $actor) |
||
193 | |||
194 | /** |
||
195 | * Creates a new Statement based on the current one containing an Authority |
||
196 | * that asserts the Statement true. |
||
197 | * |
||
198 | * @param Actor $authority The Authority asserting the Statement true |
||
199 | * |
||
200 | * @return Statement The new Statement |
||
201 | */ |
||
202 | public function withAuthority(Actor $authority) |
||
206 | |||
207 | /** |
||
208 | * Checks if another statement is equal. |
||
209 | * |
||
210 | * Two statements are equal if and only if all of their properties are equal. |
||
211 | * |
||
212 | * @param Statement $statement The statement to compare with |
||
213 | * |
||
214 | * @return bool True if the statements are equal, false otherwise |
||
215 | */ |
||
216 | public function equals(Statement $statement) |
||
260 | } |
||
261 |
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.