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 |
||
25 | class DeprecationWrapper implements \ArrayAccess { |
||
26 | /** @var object */ |
||
27 | protected $object; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $string; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $message; |
||
34 | |||
35 | /** @var string */ |
||
36 | protected $version; |
||
37 | |||
38 | /** @var callable */ |
||
39 | protected $reporter; |
||
40 | |||
41 | /** |
||
42 | * Create the wrapper |
||
43 | * |
||
44 | * @param mixed $object The object or string to wrap |
||
45 | * @param string $message The deprecation message to display when used |
||
46 | * @param string $version The Elgg version this was deprecated |
||
47 | * @param callable $reporter function called to report deprecation |
||
48 | */ |
||
49 | 9 | public function __construct($object, $message, $version, $reporter = 'elgg_deprecated_notice') { |
|
59 | |||
60 | /** |
||
61 | * Get a property on the object |
||
62 | * |
||
63 | * @param string $name Property name |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 1 | public function __get($name) { |
|
70 | |||
71 | /** |
||
72 | * Set a property on the object |
||
73 | * |
||
74 | * @param string $name Property name |
||
75 | * @param mixed $value Property value |
||
76 | * @return void |
||
77 | */ |
||
78 | public function __set($name, $value) { |
||
82 | |||
83 | /** |
||
84 | * Call a method on the object |
||
85 | * |
||
86 | * @param string $name Method name |
||
87 | * @param array $arguments Method arguments |
||
88 | * @return mixed |
||
89 | */ |
||
90 | 1 | public function __call($name, $arguments) { |
|
94 | |||
95 | /** |
||
96 | * Get the object as string |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 2 | public function __toString() { |
|
108 | |||
109 | /** |
||
110 | * Display a warning |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | 4 | protected function displayWarning() { |
|
121 | |||
122 | /** |
||
123 | * Array access interface |
||
124 | * |
||
125 | * @see \ArrayAccess::offsetSet() |
||
126 | * |
||
127 | * @param mixed $key Name |
||
128 | * @param mixed $value Value |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | 2 | public function offsetSet($key, $value) { |
|
145 | |||
146 | /** |
||
147 | * Array access interface |
||
148 | * |
||
149 | * @see \ArrayAccess::offsetGet() |
||
150 | * |
||
151 | * @param mixed $key Name |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 2 | View Code Duplication | public function offsetGet($key) { |
163 | |||
164 | /** |
||
165 | * Array access interface |
||
166 | * |
||
167 | * @see \ArrayAccess::offsetUnset() |
||
168 | * |
||
169 | * @param mixed $key Name |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | 1 | View Code Duplication | public function offsetUnset($key) { |
181 | |||
182 | /** |
||
183 | * Array access interface |
||
184 | * |
||
185 | * @see \ArrayAccess::offsetExists() |
||
186 | * |
||
187 | * @param mixed $offset Offset |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | View Code Duplication | public function offsetExists($offset) { |
|
199 | } |
||
200 | |||
201 |
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.