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 |
||
27 | class Fetcher { |
||
28 | |||
29 | const DEFAULT_BASE_URL = 'https://updates.owncloud.com/server/'; |
||
30 | |||
31 | /** |
||
32 | * @var Locator $locator |
||
33 | */ |
||
34 | protected $locator; |
||
35 | |||
36 | /** |
||
37 | * @var ConfigReader $configReader |
||
38 | */ |
||
39 | protected $configReader; |
||
40 | |||
41 | /** |
||
42 | * @var Client $httpClient |
||
43 | */ |
||
44 | protected $httpClient; |
||
45 | protected $requiredFeedEntries = [ |
||
46 | 'version', |
||
47 | 'versionstring', |
||
48 | 'url' |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Constructor |
||
53 | * |
||
54 | * @param Client $httpClient |
||
55 | * @param Locator $locator |
||
56 | * @param ConfigReader $configReader |
||
57 | */ |
||
58 | 3 | public function __construct(Client $httpClient, Locator $locator, ConfigReader $configReader){ |
|
63 | |||
64 | /** |
||
65 | * Download new owncloud package |
||
66 | * @param Feed $feed |
||
67 | * @param Callable $onProgress |
||
68 | * @throws \UnexpectedValueException |
||
69 | */ |
||
70 | public function getOwncloud(Feed $feed, callable $onProgress){ |
||
71 | if ($feed->isValid()){ |
||
72 | $downloadPath = $this->getBaseDownloadPath($feed); |
||
73 | if (!is_writable(dirname($downloadPath))){ |
||
74 | throw new \Exception(dirname($downloadPath) . ' is not writable.'); |
||
75 | } |
||
76 | $url = $feed->getUrl(); |
||
77 | $request = $this->httpClient->createRequest( |
||
78 | 'GET', |
||
79 | $url, |
||
80 | [ |
||
81 | 'save_to' => $downloadPath, |
||
82 | 'timeout' => 600 |
||
83 | ] |
||
84 | ); |
||
85 | $request->getEmitter()->on('progress', $onProgress); |
||
86 | $response = $this->httpClient->send($request); |
||
87 | View Code Duplication | if ($response->getStatusCode() !== 200){ |
|
|
|||
88 | throw new \UnexpectedValueException('Failed to download ' . $url . '. Server responded with ' . $response->getStatusCode() . ' instead of 200.'); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Produce a local path to save the package to |
||
95 | * @param Feed $feed |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getBaseDownloadPath(Feed $feed){ |
||
102 | |||
103 | /** |
||
104 | * Get md5 sum for the package |
||
105 | * @param Feed $feed |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getMd5(Feed $feed){ |
||
114 | |||
115 | /** |
||
116 | * Read update feed for new releases |
||
117 | * @return Feed |
||
118 | */ |
||
119 | 3 | public function getFeed(){ |
|
120 | 3 | $url = $this->getFeedUrl(); |
|
121 | 3 | $xml = $this->download($url); |
|
122 | 3 | $tmp = []; |
|
123 | 3 | if ($xml){ |
|
124 | 2 | $loadEntities = libxml_disable_entity_loader(true); |
|
125 | 2 | $data = @simplexml_load_string($xml); |
|
126 | 2 | libxml_disable_entity_loader($loadEntities); |
|
127 | 2 | if ($data !== false){ |
|
128 | 1 | $tmp['version'] = (string) $data->version; |
|
129 | 1 | $tmp['versionstring'] = (string) $data->versionstring; |
|
130 | 1 | $tmp['url'] = (string) $data->url; |
|
131 | 1 | $tmp['web'] = (string) $data->web; |
|
132 | 1 | } |
|
133 | 2 | } |
|
134 | |||
135 | 3 | return new Feed($tmp); |
|
136 | } |
||
137 | |||
138 | 3 | public function getUpdateChannel(){ |
|
141 | |||
142 | /** |
||
143 | * Produce complete feed URL |
||
144 | * @return string |
||
145 | */ |
||
146 | 3 | protected function getFeedUrl(){ |
|
158 | |||
159 | /** |
||
160 | * Get URL content |
||
161 | * @param string $url |
||
162 | * @return string |
||
163 | * @throws \UnexpectedValueException |
||
164 | */ |
||
165 | 3 | protected function download($url){ |
|
172 | |||
173 | } |
||
174 |
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.