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 |
||
8 | class WeburgClient |
||
9 | { |
||
10 | /** |
||
11 | * @var ClientInterface |
||
12 | */ |
||
13 | private $httpClient; |
||
14 | |||
15 | public function __construct(ClientInterface $httpClient) |
||
19 | |||
20 | public function getMovieInfoById($movieId) |
||
21 | { |
||
22 | $movieUrl = $this->getMovieUrl($movieId); |
||
23 | $body = $this->getUrlBody($movieUrl); |
||
24 | $body = iconv('WINDOWS-1251', 'UTF-8', $body); |
||
25 | $info = $this->getMovieInfo($body); |
||
26 | return $info; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param $movieId |
||
31 | * @return array urls of movie (not series) |
||
32 | */ |
||
33 | public function getMovieTorrentUrlsById($movieId) |
||
34 | { |
||
35 | $torrentUrl = $this->getMovieTorrentUrl($movieId); |
||
36 | $body = $this->getUrlBody($torrentUrl); |
||
37 | $torrentsUrls = $this->getTorrentsUrls($body); |
||
38 | return $torrentsUrls; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param $movieId |
||
43 | * @param $hashes array hashes of torrents from movieInfo |
||
44 | * @param int $daysMax torrents older last days will not matched |
||
45 | * @return array urls of matched torrent files |
||
46 | */ |
||
47 | public function getSeriesTorrents($movieId, $hashes, $daysMax = 1) |
||
48 | { |
||
49 | $torrentsUrls = []; |
||
50 | $timestampFrom = strtotime('-' . $daysMax . 'days'); |
||
51 | |||
52 | $hashes = array_reverse($hashes); |
||
53 | foreach ($hashes as $hash) { |
||
54 | $torrentUrl = $this->getMovieTorrentUrl($movieId, $hash); |
||
55 | $body = $this->getUrlBody($torrentUrl); |
||
56 | |||
57 | if (!$this->checkTorrentDate($body, $timestampFrom)) { |
||
58 | break; |
||
59 | } |
||
60 | $torrentsUrls = array_merge($torrentsUrls, $this->getTorrentsUrls($body)); |
||
61 | } |
||
62 | |||
63 | return $torrentsUrls; |
||
64 | } |
||
65 | |||
66 | public function getMoviesIds() |
||
67 | { |
||
68 | $moviesUrl = 'http://weburg.net/movies/new/?clever_title=1&template=0&last=0'; |
||
69 | |||
70 | $jsonRaw = $this->getUrlBody($moviesUrl, [ |
||
71 | 'Content-Type' => 'text/html; charset=utf-8', |
||
72 | 'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0', |
||
73 | 'X-Requested-With' => 'XMLHttpRequest', |
||
74 | ]); |
||
75 | |||
76 | $moviesJson = json_decode($jsonRaw); |
||
77 | |||
78 | $moviesIds = $this->getInfoUrls($moviesJson->items); |
||
79 | |||
80 | return $moviesIds; |
||
81 | } |
||
82 | |||
83 | public function getMovieUrl($movieId) |
||
84 | { |
||
85 | return 'http://weburg.net/movies/info/' . $movieId; |
||
86 | } |
||
87 | |||
88 | public function getUrlBody($url, $headers = []) |
||
106 | |||
107 | public function isTorrentPopular($movieInfo, $commentsMin, $imdbMin, $kinopoiskMin, $votesMin) |
||
108 | { |
||
109 | return $movieInfo['comments'] >= $commentsMin |
||
110 | || $movieInfo['rating_imdb'] >= $imdbMin |
||
111 | || $movieInfo['rating_kinopoisk'] >= $kinopoiskMin |
||
112 | || $movieInfo['rating_votes'] >= $votesMin; |
||
113 | } |
||
114 | |||
115 | public function downloadTorrent($url, $torrentsDir) |
||
134 | |||
135 | public function cleanMovieId($idOrUrl) |
||
144 | |||
145 | private function getMovieTorrentUrl($movieId, $hash = '') |
||
146 | { |
||
147 | return 'http://weburg.net/ajax/download/movie?' |
||
148 | . ($hash ? 'hash=' . $hash . '&' : '') |
||
149 | . 'obj_id=' . $movieId; |
||
150 | } |
||
151 | |||
152 | private function getMovieInfo($body) |
||
174 | |||
175 | /** |
||
176 | * @param $body |
||
177 | * @param $fromTimestamp |
||
178 | * @return bool|string date if matched, false if not |
||
179 | */ |
||
180 | private function checkTorrentDate($body, $fromTimestamp) |
||
190 | |||
191 | private function getInfoUrls($body) |
||
197 | |||
198 | /** |
||
199 | * @param $body |
||
200 | * @return array |
||
201 | */ |
||
202 | private function getTorrentsUrls($body) |
||
209 | } |
||
210 |
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.