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 ImageResource extends Resource |
||
9 | { |
||
10 | /** |
||
11 | * List Images. |
||
12 | * |
||
13 | * @param array $parameters List of parameters |
||
14 | * @param string $fetch Fetch mode (object or response) |
||
15 | * |
||
16 | * @return \Psr\Http\Message\ResponseInterface |
||
17 | */ |
||
18 | public function findAll($parameters = [], $fetch = self::FETCH_OBJECT) |
||
19 | { |
||
20 | $queryParam = new QueryParam(); |
||
21 | $queryParam->setDefault('all', false); |
||
22 | $queryParam->setDefault('filters', null); |
||
23 | $queryParam->setDefault('filter', null); |
||
24 | $queryParam->setDefault('digests', null); |
||
25 | $url = sprintf('/v1.21/images/json?%s', $queryParam->buildQueryString($parameters)); |
||
26 | $request = $this->messageFactory->createRequest('GET', $url, $queryParam->buildHeaders($parameters), null); |
||
27 | $request = $request->withHeader('Host', 'localhost'); |
||
28 | $response = $this->httpClient->sendRequest($request); |
||
29 | if (self::FETCH_RESPONSE == $fetch) { |
||
30 | return $response; |
||
31 | } |
||
32 | if ('200' == $response->getStatusCode()) { |
||
33 | return $this->serializer->deserialize($response->getBody()->getContents(), 'Docker\\API\\Model\\ImageItem[]', 'json'); |
||
34 | } |
||
35 | |||
36 | return $response; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Build an image from Dockerfile via stdin. |
||
41 | * |
||
42 | * @param mixed $inputStream The input stream must be a tar archive compressed with one of the following algorithms: identity (no compression), gzip, bzip2, xz. |
||
43 | * @param array $parameters List of parameters |
||
44 | * @param string $fetch Fetch mode (object or response) |
||
45 | * |
||
46 | * @return \Psr\Http\Message\ResponseInterface |
||
47 | */ |
||
48 | public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
71 | |||
72 | /** |
||
73 | * Create an image either by pulling it from the registry or by importing it. |
||
74 | * |
||
75 | * @param array $parameters List of parameters |
||
76 | * @param string $fetch Fetch mode (object or response) |
||
77 | * |
||
78 | * @return \Psr\Http\Message\ResponseInterface |
||
79 | */ |
||
80 | View Code Duplication | public function create($parameters = [], $fetch = self::FETCH_OBJECT) |
|
98 | |||
99 | /** |
||
100 | * Return low-level information on the image name. |
||
101 | * |
||
102 | * @param array $parameters List of parameters |
||
103 | * @param string $fetch Fetch mode (object or response) |
||
104 | * |
||
105 | * @return \Psr\Http\Message\ResponseInterface |
||
106 | */ |
||
107 | public function find($parameters = [], $fetch = self::FETCH_OBJECT) |
||
108 | { |
||
109 | $queryParam = new QueryParam(); |
||
110 | $url = sprintf('/v1.21/images/{name}/json?%s', $queryParam->buildQueryString($parameters)); |
||
111 | $request = $this->messageFactory->createRequest('GET', $url, $queryParam->buildHeaders($parameters), null); |
||
112 | $request = $request->withHeader('Host', 'localhost'); |
||
113 | $response = $this->httpClient->sendRequest($request); |
||
114 | if (self::FETCH_RESPONSE == $fetch) { |
||
115 | return $response; |
||
116 | } |
||
117 | if ('200' == $response->getStatusCode()) { |
||
118 | return $this->serializer->deserialize($response->getBody()->getContents(), 'Docker\\API\\Model\\Image', 'json'); |
||
119 | } |
||
120 | |||
121 | return $response; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return the history of the image name. |
||
126 | * |
||
127 | * @param array $parameters List of parameters |
||
128 | * @param string $fetch Fetch mode (object or response) |
||
129 | * |
||
130 | * @return \Psr\Http\Message\ResponseInterface |
||
131 | */ |
||
132 | public function history($parameters = [], $fetch = self::FETCH_OBJECT) |
||
148 | |||
149 | /** |
||
150 | * Push the image name on the registry. |
||
151 | * |
||
152 | * @param array $parameters List of parameters |
||
153 | * @param string $fetch Fetch mode (object or response) |
||
154 | * |
||
155 | * @return \Psr\Http\Message\ResponseInterface |
||
156 | */ |
||
157 | View Code Duplication | public function push($parameters = [], $fetch = self::FETCH_OBJECT) |
|
172 | |||
173 | /** |
||
174 | * Tag the image name into a repository. |
||
175 | * |
||
176 | * @param array $parameters List of parameters |
||
177 | * @param string $fetch Fetch mode (object or response) |
||
178 | * |
||
179 | * @return \Psr\Http\Message\ResponseInterface |
||
180 | */ |
||
181 | View Code Duplication | public function tag($parameters = [], $fetch = self::FETCH_OBJECT) |
|
197 | |||
198 | /** |
||
199 | * Remove the image name from the filesystem. |
||
200 | * |
||
201 | * @param array $parameters List of parameters |
||
202 | * @param string $fetch Fetch mode (object or response) |
||
203 | * |
||
204 | * @return \Psr\Http\Message\ResponseInterface |
||
205 | */ |
||
206 | View Code Duplication | public function remove($parameters = [], $fetch = self::FETCH_OBJECT) |
|
221 | |||
222 | /** |
||
223 | * Search for an image on Docker Hub. |
||
224 | * |
||
225 | * @param array $parameters List of parameters |
||
226 | * @param string $fetch Fetch mode (object or response) |
||
227 | * |
||
228 | * @return \Psr\Http\Message\ResponseInterface |
||
229 | */ |
||
230 | public function search($parameters = [], $fetch = self::FETCH_OBJECT) |
||
247 | |||
248 | /** |
||
249 | * Create a new image from a container’s changes. |
||
250 | * |
||
251 | * @param mixed $containerConfig The container configuration |
||
252 | * @param array $parameters List of parameters |
||
253 | * @param string $fetch Fetch mode (object or response) |
||
254 | * |
||
255 | * @return \Psr\Http\Message\ResponseInterface |
||
256 | */ |
||
257 | View Code Duplication | public function commit($containerConfig, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
280 | |||
281 | /** |
||
282 | * Get a tarball containing all images and metadata for the repository specified by name. |
||
283 | * |
||
284 | * @param array $parameters List of parameters |
||
285 | * @param string $fetch Fetch mode (object or response) |
||
286 | * |
||
287 | * @return \Psr\Http\Message\ResponseInterface |
||
288 | */ |
||
289 | public function save($parameters = [], $fetch = self::FETCH_OBJECT) |
||
302 | |||
303 | /** |
||
304 | * Get a tarball containing all images and metadata for one or more repositories. |
||
305 | * |
||
306 | * @param array $parameters List of parameters |
||
307 | * @param string $fetch Fetch mode (object or response) |
||
308 | * |
||
309 | * @return \Psr\Http\Message\ResponseInterface |
||
310 | */ |
||
311 | View Code Duplication | public function saveAll($parameters = [], $fetch = self::FETCH_OBJECT) |
|
325 | |||
326 | /** |
||
327 | * Load a set of images and tags into a Docker repository. See the image tarball format for more details. |
||
328 | * |
||
329 | * @param mixed $imagesTarball Tar archive containing images |
||
330 | * @param array $parameters List of parameters |
||
331 | * @param string $fetch Fetch mode (object or response) |
||
332 | * |
||
333 | * @return \Psr\Http\Message\ResponseInterface |
||
334 | */ |
||
335 | public function load($imagesTarball, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
348 | } |
||
349 |
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.