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 |
||
12 | class OpenGraphAdapter extends AbstractAdapter |
||
13 | { |
||
14 | /** |
||
15 | * extract title information from crawler object |
||
16 | * @param Crawler $crawler |
||
17 | * @return string |
||
18 | */ |
||
19 | public function extractTitle(Crawler $crawler) |
||
51 | |||
52 | /** |
||
53 | * extract image url from crawler open graph |
||
54 | * @param Crawler $crawler |
||
55 | * @return string |
||
56 | */ |
||
57 | public function extractImage(Crawler $crawler) |
||
103 | |||
104 | View Code Duplication | public function extractDescription(Crawler $crawler) |
|
117 | |||
118 | /** |
||
119 | * extract keywords out of crawler object |
||
120 | * @param Crawler $crawler |
||
121 | * @return array |
||
122 | */ |
||
123 | View Code Duplication | public function extractKeywords(Crawler $crawler) |
|
124 | { |
||
125 | $ret = array(); |
||
126 | |||
127 | $crawler->filterXPath("//head/meta[@property='og:keywords']") |
||
128 | ->each( |
||
129 | function(Crawler $node) use (&$ret) { |
||
130 | |||
131 | $node_txt = trim($node->attr('content')); |
||
132 | if (!empty($node_txt)) { |
||
133 | $ret = explode(',', $node_txt); |
||
134 | |||
135 | } |
||
136 | } |
||
137 | ); |
||
138 | |||
139 | return $ret; |
||
140 | } |
||
141 | |||
142 | public function extractBody(Crawler $crawler) |
||
147 | |||
148 | View Code Duplication | public function extractPublishDate(Crawler $crawler) |
|
166 | |||
167 | View Code Duplication | public function extractAuthor(Crawler $crawler) |
|
168 | { |
||
169 | $ret = null; |
||
170 | $crawler->filterXPath("//head/meta[@property='article:author']") |
||
171 | ->each( |
||
172 | function(Crawler $node) use (&$ret) { |
||
173 | $ret = $node->attr('content'); |
||
174 | } |
||
175 | ); |
||
176 | |||
177 | return $ret; |
||
178 | } |
||
179 | |||
180 | public function getCheckSmallImage($imageUrl){ |
||
193 | } |
||
194 |
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.