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 CustomAdapter extends AbstractAdapter |
||
13 | { |
||
14 | |||
15 | private $authorSelector; |
||
16 | private $bodySelector; |
||
17 | private $descriptionSelector; |
||
18 | private $imageSelector; |
||
19 | private $keywordsSelector; |
||
20 | private $publishDateSelector; |
||
21 | private $titleSelector; |
||
22 | |||
23 | /** |
||
24 | * adapter used to fill in the missing selectors data by default values |
||
25 | * @var DefaultAdapter $fallbackAdapter |
||
26 | */ |
||
27 | private $fallbackAdapter; |
||
28 | |||
29 | public function __construct() |
||
33 | |||
34 | public function setAuthorSelector($selector) |
||
39 | |||
40 | public function setBodySelector($selector) |
||
45 | |||
46 | public function setDescriptionSelector($selector) |
||
51 | |||
52 | public function setImageSelector($selector) |
||
57 | |||
58 | public function setKeywordsSelector($selector) |
||
63 | |||
64 | public function setPublishDateSelector($selector) |
||
69 | |||
70 | public function setTitleSelector($selector) |
||
75 | |||
76 | View Code Duplication | public function extractAuthor(Crawler $crawler) |
|
84 | |||
85 | public function extractBody(Crawler $crawler) |
||
90 | |||
91 | View Code Duplication | public function extractDescription(Crawler $crawler) |
|
99 | |||
100 | public function extractImage(Crawler $crawler) |
||
116 | |||
117 | public function extractKeywords(Crawler $crawler) |
||
126 | |||
127 | View Code Duplication | public function extractPublishDate(Crawler $crawler) |
|
135 | |||
136 | View Code Duplication | public function extractTitle(Crawler $crawler) |
|
144 | |||
145 | /** |
||
146 | * getting text of element by selector (css selector or xpath ) |
||
147 | * @param Crawler $crawler |
||
148 | * @param string $selector |
||
149 | * @param \Closure $extractClosure callback function to be used for extraction |
||
150 | * @return string |
||
151 | */ |
||
152 | protected function getElementText(Crawler $crawler, $selector, $extractClosure = null) |
||
175 | } |
||
176 |
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.