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 // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
||
19 | class ChangeEntry implements JsonSerializable { |
||
20 | |||
21 | /** |
||
22 | * Entry significance. |
||
23 | * |
||
24 | * @var string|null |
||
25 | */ |
||
26 | protected $significance = null; |
||
27 | |||
28 | /** |
||
29 | * Entry timestamp. |
||
30 | * |
||
31 | * @var DateTime |
||
32 | */ |
||
33 | protected $timestamp; |
||
34 | |||
35 | /** |
||
36 | * Subheading the entry is under. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $subheading = ''; |
||
41 | |||
42 | /** |
||
43 | * Author of the entry. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $author = ''; |
||
48 | |||
49 | /** |
||
50 | * Content of the entry. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $content = ''; |
||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | * |
||
59 | * @param array $data Data for entry fields. Keys correspond to the setters, e.g. key 'content' calls `setContent()`. |
||
60 | * @throws InvalidArgumentException If an argument is invalid. |
||
61 | */ |
||
62 | View Code Duplication | public function __construct( array $data = array() ) { |
|
73 | |||
74 | /** |
||
75 | * Compare two ChangeEntry objects. |
||
76 | * |
||
77 | * @param ChangeEntry $a First ChangeEntry. |
||
78 | * @param ChangeEntry $b Second ChangeEntry. |
||
79 | * @param array $config Comparison configuration. Keys are: |
||
80 | * - ordering: (string[]) Order in which to consider the fields. Field |
||
81 | * names correspond to getters, e.g. 'significance' => |
||
82 | * `getSignificance()`. Default ordering is subheading, content. |
||
83 | * - knownSubheadings: (string[]) Change entries with these headings will |
||
84 | * compare, in this order, after those with no subheading and before any |
||
85 | * other subheadings. |
||
86 | * @return int <0 if $a should come before $b, >0 if $b should come before $a, or 0 otherwise. |
||
87 | * @throws InvalidArgumentException If an argument is invalid. |
||
88 | */ |
||
89 | public static function compare( ChangeEntry $a, ChangeEntry $b, array $config = array() ) { |
||
117 | |||
118 | /** |
||
119 | * Get the significance. |
||
120 | * |
||
121 | * @return string|null |
||
122 | */ |
||
123 | public function getSignificance() { |
||
126 | |||
127 | /** |
||
128 | * Set the significance. |
||
129 | * |
||
130 | * While entries coming in from users should always have a significance, we allow null here |
||
131 | * because entries created programmatically, particularly when parsing an existing changelog, |
||
132 | * may not include significance information. |
||
133 | * |
||
134 | * @param string|null $significance 'patch', 'minor', or 'major'. |
||
135 | * @returns $this |
||
136 | * @throws InvalidArgumentException If an argument is invalid. |
||
137 | */ |
||
138 | public function setSignificance( $significance ) { |
||
145 | |||
146 | /** |
||
147 | * Compare significance values. |
||
148 | * |
||
149 | * @param ChangeEntry $a First entry. |
||
150 | * @param ChangeEntry $b Second entry. |
||
151 | * @param array $config Passed from `compare()`, but unused here. |
||
152 | * @return int |
||
153 | */ |
||
154 | protected static function compareSignificance( ChangeEntry $a, ChangeEntry $b, array $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
160 | |||
161 | /** |
||
162 | * Get the timestamp. |
||
163 | * |
||
164 | * @return DateTime |
||
165 | */ |
||
166 | public function getTimestamp() { |
||
169 | |||
170 | /** |
||
171 | * Set the timestamp. |
||
172 | * |
||
173 | * @param DateTime|string $timestamp Timestamp to set. |
||
174 | * @returns $this |
||
175 | * @throws InvalidArgumentException If an argument is invalid. |
||
176 | */ |
||
177 | View Code Duplication | public function setTimestamp( $timestamp ) { |
|
188 | |||
189 | /** |
||
190 | * Compare timestamps. |
||
191 | * |
||
192 | * @param ChangeEntry $a First entry. |
||
193 | * @param ChangeEntry $b Second entry. |
||
194 | * @param array $config Passed from `compare()`, but unused here. |
||
195 | * @return int |
||
196 | */ |
||
197 | protected static function compareTimestamp( ChangeEntry $a, ChangeEntry $b, array $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
202 | |||
203 | /** |
||
204 | * Get the subheading. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getSubheading() { |
||
211 | |||
212 | /** |
||
213 | * Set the subheading. |
||
214 | * |
||
215 | * @param string $subheading Subheading to set. |
||
216 | * @returns $this |
||
217 | */ |
||
218 | public function setSubheading( $subheading ) { |
||
222 | |||
223 | /** |
||
224 | * Compare subheadings. |
||
225 | * |
||
226 | * @param ChangeEntry $a First entry. |
||
227 | * @param ChangeEntry $b Second entry. |
||
228 | * @param array $config Passed from `compare()`, used for 'knownSubheadings'. |
||
229 | * @return int |
||
230 | */ |
||
231 | protected static function compareSubheading( ChangeEntry $a, ChangeEntry $b, array $config ) { |
||
262 | |||
263 | /** |
||
264 | * Get the author. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getAuthor() { |
||
271 | |||
272 | /** |
||
273 | * Set the author. |
||
274 | * |
||
275 | * @param string $author Author to set. |
||
276 | * @returns $this |
||
277 | */ |
||
278 | public function setAuthor( $author ) { |
||
282 | |||
283 | /** |
||
284 | * Get the content. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getContent() { |
||
291 | |||
292 | /** |
||
293 | * Set the content. |
||
294 | * |
||
295 | * @param string $content Content to set. |
||
296 | * @returns $this |
||
297 | */ |
||
298 | public function setContent( $content ) { |
||
302 | |||
303 | /** |
||
304 | * Return data for serializing to JSON. |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | public function jsonSerialize() { |
||
318 | |||
319 | /** |
||
320 | * Unserialize from JSON. |
||
321 | * |
||
322 | * @param array $data JSON data as returned by self::jsonSerialize(). |
||
323 | * @return static |
||
324 | * @throws InvalidArgumentException If the data is invalid. |
||
325 | */ |
||
326 | public static function jsonUnserialize( $data ) { |
||
338 | |||
339 | } |
||
340 |