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 HowTo extends CreativeWork |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * The estimated cost of the supply or supplies consumed when performing |
||
15 | * instructions. |
||
16 | * |
||
17 | * @param MonetaryAmount|MonetaryAmount[]|string|string[] $estimatedCost |
||
18 | * |
||
19 | * @return static |
||
20 | * |
||
21 | * @see http://schema.org/estimatedCost |
||
22 | */ |
||
23 | public function estimatedCost($estimatedCost) |
||
27 | |||
28 | /** |
||
29 | * The length of time it takes to perform instructions or a direction (not |
||
30 | * including time to prepare the supplies), in [ISO 8601 duration |
||
31 | * format](http://en.wikipedia.org/wiki/ISO_8601). |
||
32 | * |
||
33 | * @param Duration|Duration[] $performTime |
||
34 | * |
||
35 | * @return static |
||
36 | * |
||
37 | * @see http://schema.org/performTime |
||
38 | */ |
||
39 | public function performTime($performTime) |
||
43 | |||
44 | /** |
||
45 | * The length of time it takes to prepare the items to be used in |
||
46 | * instructions or a direction, in [ISO 8601 duration |
||
47 | * format](http://en.wikipedia.org/wiki/ISO_8601). |
||
48 | * |
||
49 | * @param Duration|Duration[] $prepTime |
||
50 | * |
||
51 | * @return static |
||
52 | * |
||
53 | * @see http://schema.org/prepTime |
||
54 | */ |
||
55 | public function prepTime($prepTime) |
||
59 | |||
60 | /** |
||
61 | * A single step item (as HowToStep, text, document, video, etc.) or a |
||
62 | * HowToSection. |
||
63 | * |
||
64 | * @param CreativeWork|CreativeWork[]|HowToSection|HowToSection[]|HowToStep|HowToStep[]|string|string[] $step |
||
65 | * |
||
66 | * @return static |
||
67 | * |
||
68 | * @see http://schema.org/step |
||
69 | */ |
||
70 | public function step($step) |
||
74 | |||
75 | /** |
||
76 | * A single step item (as HowToStep, text, document, video, etc.) or a |
||
77 | * HowToSection (originally misnamed 'steps'; 'step' is preferred). |
||
78 | * |
||
79 | * @param CreativeWork|CreativeWork[]|ItemList|ItemList[]|string|string[] $steps |
||
80 | * |
||
81 | * @return static |
||
82 | * |
||
83 | * @see http://schema.org/steps |
||
84 | */ |
||
85 | public function steps($steps) |
||
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 | * The quantity that results by performing instructions. For example, a |
||
138 | * paper airplane, 10 personalized candles. |
||
139 | * |
||
140 | * @param QuantitativeValue|QuantitativeValue[]|string|string[] $yield |
||
141 | * |
||
142 | * @return static |
||
143 | * |
||
144 | * @see http://schema.org/yield |
||
145 | */ |
||
146 | public function yield($yield) |
||
150 | |||
151 | } |
||
152 |
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.