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 |
||
9 | class Preview |
||
10 | { |
||
11 | protected $result; |
||
12 | |||
13 | protected $client; |
||
14 | |||
15 | public $document; |
||
16 | |||
17 | public $url; |
||
18 | |||
19 | protected $urlComponents; |
||
20 | |||
21 | 33 | public function __construct(Client $client) |
|
25 | |||
26 | /** |
||
27 | * @param string $url |
||
28 | * @return Document |
||
29 | */ |
||
30 | 33 | public function fetch($url) |
|
44 | |||
45 | /** |
||
46 | * @return mixed |
||
47 | */ |
||
48 | 6 | public function title() |
|
52 | |||
53 | /** |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 12 | View Code Duplication | public function metaDescription() |
64 | |||
65 | /** |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 3 | public function metaKeywords() |
|
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | 12 | View Code Duplication | public function metaTitle() |
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | 3 | public function meta() |
|
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | 12 | public function images() |
|
118 | |||
119 | 11 | public function render($type = 'media') |
|
143 | |||
144 | /** |
||
145 | * @param string $url |
||
146 | * @return array |
||
147 | */ |
||
148 | 12 | private function formatUrl($url) |
|
160 | } |
||
161 |
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.