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 | class Page |
||
12 | { |
||
13 | protected $config = array(); |
||
14 | protected $providers = array(); |
||
15 | protected $metas = array(); |
||
16 | protected $info = array( |
||
17 | 'url' => null, |
||
18 | 'title' => null, |
||
19 | 'text' => null, |
||
20 | 'image' => null, |
||
21 | 'twitterUser' => null, |
||
22 | ); |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param array $info The page info. Only url, title, text, image and twitterUser fields are available |
||
28 | * @param array $config Configuration options |
||
29 | */ |
||
30 | public function __construct(array $info, array $config = array()) |
||
39 | |||
40 | /** |
||
41 | * Normalize value before save it: |
||
42 | * - remove html tags |
||
43 | * - remove line-ending and multiple spaces |
||
44 | * - remove spaces around |
||
45 | * - decode escaped html entities. |
||
46 | * |
||
47 | * @param string |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | protected static function normalize($value) |
||
55 | |||
56 | /** |
||
57 | * Magic method to check if a provider exists. |
||
58 | * |
||
59 | * @param string $key |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function __isset($key) |
||
75 | |||
76 | /** |
||
77 | * Magic method to instantiate and return providers in lazy mode. |
||
78 | * |
||
79 | * @param string $key The provider name |
||
80 | * |
||
81 | * @throws \Exception if the provider does not exists |
||
82 | * |
||
83 | * @return Providers\ProviderInterface |
||
84 | */ |
||
85 | View Code Duplication | public function __get($key) |
|
101 | |||
102 | /** |
||
103 | * Magic method to instantiate and return metas in lazy mode. |
||
104 | * |
||
105 | * @param string $key The meta collection name |
||
106 | * @param array $arguments The arguments passed to the method |
||
107 | * |
||
108 | * @throws \Exception if the meta does not exists |
||
109 | * |
||
110 | * @return Metas\MetaInterface |
||
111 | */ |
||
112 | View Code Duplication | public function __call($key, $arguments) |
|
128 | |||
129 | /** |
||
130 | * Preload the counter. |
||
131 | * |
||
132 | * @param array $providers |
||
133 | */ |
||
134 | public function shareCount(array $providers) |
||
176 | |||
177 | /** |
||
178 | * Gets the page url. |
||
179 | * |
||
180 | * @return string|null |
||
181 | */ |
||
182 | public function getUrl() |
||
186 | |||
187 | /** |
||
188 | * Gets the page title. |
||
189 | * |
||
190 | * @return string|null |
||
191 | */ |
||
192 | public function getTitle() |
||
196 | |||
197 | /** |
||
198 | * Gets the page text description. |
||
199 | * |
||
200 | * @return string|null |
||
201 | */ |
||
202 | public function getText() |
||
206 | |||
207 | /** |
||
208 | * Gets the page image. |
||
209 | * |
||
210 | * @return string|null |
||
211 | */ |
||
212 | public function getImage() |
||
216 | |||
217 | /** |
||
218 | * Gets the page twitterUser. |
||
219 | * |
||
220 | * @return string|null |
||
221 | */ |
||
222 | public function getTwitterUser() |
||
226 | |||
227 | /** |
||
228 | * Gets some page info. |
||
229 | * |
||
230 | * @param array|null Array with the page fields to return as $name => $rename. Set null to return all info |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function get(array $info = null) |
||
256 | |||
257 | /** |
||
258 | * Gets one or all configuration option. |
||
259 | * |
||
260 | * @param string|null $name |
||
261 | * |
||
262 | * @return string|array|null |
||
263 | */ |
||
264 | public function getConfig($name = null) |
||
272 | } |
||
273 |
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.