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 |
||
30 | class GlLinkChecker |
||
31 | { |
||
32 | /** |
||
33 | * @var \GuzzleHttp\Client |
||
34 | */ |
||
35 | private $client; |
||
36 | |||
37 | /** |
||
38 | * @var array|null $internalurls |
||
39 | */ |
||
40 | private $internalurls; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | public function __construct($rooturl = null, array $internalurls = null) |
||
46 | { |
||
47 | $this->client = new Client([ |
||
48 | 'base_uri' => $rooturl, |
||
49 | 'verify' => false, |
||
50 | 'defaults' => [ |
||
51 | 'headers' => [ |
||
52 | 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0', |
||
53 | 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
||
54 | 'Accept-Language' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', |
||
55 | 'Accept-Encoding' => 'gzip, deflate' |
||
56 | ] |
||
57 | ] |
||
58 | ]); |
||
59 | $this->internalurls = $internalurls; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * get all links in an object |
||
64 | * |
||
65 | * @param $obj |
||
66 | * @param array $links |
||
67 | */ |
||
68 | private function searchInArray($obj, array &$links) |
||
84 | |||
85 | /** |
||
86 | * get all links in a json |
||
87 | * |
||
88 | * @param string $json |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | private function getJsonLinks($json) |
||
100 | |||
101 | |||
102 | /** |
||
103 | * check links in a sitemap |
||
104 | * |
||
105 | * @param string $sitemap |
||
106 | * |
||
107 | * @return array |
||
108 | * @throws \Exception |
||
109 | */ |
||
110 | private function checkSitemap($sitemap) |
||
126 | |||
127 | /** |
||
128 | * check http error status code |
||
129 | * |
||
130 | * @param array $result |
||
131 | * @param array $urls |
||
132 | * @param int $statuscode |
||
133 | */ |
||
134 | private function checkStatus(array &$result, array $urls, $statuscode) { |
||
144 | |||
145 | /** |
||
146 | * check 403 and 404 errors |
||
147 | * |
||
148 | * @param array $urlerrors |
||
149 | * @param array $urlforbiddens |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function checkErrors(array $urlerrors, array $urlforbiddens) |
||
162 | |||
163 | /** |
||
164 | * check links in robots.txt and sitemap |
||
165 | * |
||
166 | * @return array |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | public function checkRobotsSitemap() |
||
203 | |||
204 | |||
205 | /** |
||
206 | * check links in html and json files |
||
207 | * |
||
208 | * @param Finder $files |
||
209 | * @param callable $checkstart |
||
210 | * @param callable $checking |
||
211 | * @param callable $checkend |
||
212 | * |
||
213 | * @throws \Exception |
||
214 | * @return GlLinkCheckerError[] |
||
215 | */ |
||
216 | public function checkFiles(Finder $files, callable $checkstart, callable $checking, callable $checkend) |
||
263 | } |
||
264 |
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.