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 |
||
11 | View Code Duplication | class HowToDirection extends ListItem |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * A media object representing the circumstances after performing this |
||
15 | * direction. |
||
16 | * |
||
17 | * @param MediaObject|MediaObject[]|string|string[] $afterMedia |
||
18 | * |
||
19 | * @return static |
||
20 | * |
||
21 | * @see http://schema.org/afterMedia |
||
22 | */ |
||
23 | public function afterMedia($afterMedia) |
||
27 | |||
28 | /** |
||
29 | * A media object representing the circumstances before performing this |
||
30 | * direction. |
||
31 | * |
||
32 | * @param MediaObject|MediaObject[]|string|string[] $beforeMedia |
||
33 | * |
||
34 | * @return static |
||
35 | * |
||
36 | * @see http://schema.org/beforeMedia |
||
37 | */ |
||
38 | public function beforeMedia($beforeMedia) |
||
42 | |||
43 | /** |
||
44 | * A media object representing the circumstances while performing this |
||
45 | * direction. |
||
46 | * |
||
47 | * @param MediaObject|MediaObject[]|string|string[] $duringMedia |
||
48 | * |
||
49 | * @return static |
||
50 | * |
||
51 | * @see http://schema.org/duringMedia |
||
52 | */ |
||
53 | public function duringMedia($duringMedia) |
||
57 | |||
58 | /** |
||
59 | * The length of time it takes to perform instructions or a direction (not |
||
60 | * including time to prepare the supplies), in [ISO 8601 duration |
||
61 | * format](http://en.wikipedia.org/wiki/ISO_8601). |
||
62 | * |
||
63 | * @param Duration|Duration[] $performTime |
||
64 | * |
||
65 | * @return static |
||
66 | * |
||
67 | * @see http://schema.org/performTime |
||
68 | */ |
||
69 | public function performTime($performTime) |
||
73 | |||
74 | /** |
||
75 | * The length of time it takes to prepare the items to be used in |
||
76 | * instructions or a direction, in [ISO 8601 duration |
||
77 | * format](http://en.wikipedia.org/wiki/ISO_8601). |
||
78 | * |
||
79 | * @param Duration|Duration[] $prepTime |
||
80 | * |
||
81 | * @return static |
||
82 | * |
||
83 | * @see http://schema.org/prepTime |
||
84 | */ |
||
85 | public function prepTime($prepTime) |
||
89 | |||
90 | /** |
||
91 | * A sub-property of instrument. A supply consumed when performing |
||
92 | * instructions or a direction. |
||
93 | * |
||
94 | * @param HowToSupply|HowToSupply[]|string|string[] $supply |
||
95 | * |
||
96 | * @return static |
||
97 | * |
||
98 | * @see http://schema.org/supply |
||
99 | */ |
||
100 | public function supply($supply) |
||
104 | |||
105 | /** |
||
106 | * A sub property of instrument. An object used (but not consumed) when |
||
107 | * performing instructions or a direction. |
||
108 | * |
||
109 | * @param HowToTool|HowToTool[]|string|string[] $tool |
||
110 | * |
||
111 | * @return static |
||
112 | * |
||
113 | * @see http://schema.org/tool |
||
114 | */ |
||
115 | public function tool($tool) |
||
119 | |||
120 | /** |
||
121 | * The total time required to perform instructions or a direction (including |
||
122 | * time to prepare the supplies), in [ISO 8601 duration |
||
123 | * format](http://en.wikipedia.org/wiki/ISO_8601). |
||
124 | * |
||
125 | * @param Duration|Duration[] $totalTime |
||
126 | * |
||
127 | * @return static |
||
128 | * |
||
129 | * @see http://schema.org/totalTime |
||
130 | */ |
||
131 | public function totalTime($totalTime) |
||
135 | |||
136 | } |
||
137 |
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.