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 |
||
15 | class ImageManager |
||
16 | { |
||
17 | /** |
||
18 | * @var \GuzzleHttp\Client |
||
19 | */ |
||
20 | private $client; |
||
21 | |||
22 | /** |
||
23 | * @param \GuzzleHttp\Client |
||
24 | */ |
||
25 | public function __construct(HttpClient $client) |
||
29 | |||
30 | /** |
||
31 | * Get all images from docker daemon |
||
32 | * |
||
33 | * @param boolean $dangling Filter dangling images |
||
34 | * @param boolean $all List all images including untagged |
||
35 | * |
||
36 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
37 | * |
||
38 | * @return Image[] |
||
39 | */ |
||
40 | public function findAll($dangling = false, $all = false) |
||
84 | |||
85 | /** |
||
86 | * Get an image from docker daemon |
||
87 | * |
||
88 | * @param string $repository Name of image to get |
||
89 | * @param string $tag Tag of the image to get (default "latest") |
||
90 | * |
||
91 | * @return Image |
||
92 | */ |
||
93 | public function find($repository, $tag = 'latest') |
||
103 | |||
104 | /** |
||
105 | * Inspect an image |
||
106 | * |
||
107 | * @param \Docker\Image $image |
||
108 | * |
||
109 | * @throws \Docker\Exception\ImageNotFoundException |
||
110 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
111 | * @throws \GuzzleHttp\Exception\RequestException |
||
112 | * |
||
113 | * @return array json data from docker inspect |
||
114 | */ |
||
115 | public function inspect(Image $image) |
||
142 | |||
143 | /** |
||
144 | * Pull an image from registry |
||
145 | * |
||
146 | * @param string $name Name of image to pull |
||
147 | * @param string $tag Tag of image |
||
148 | * @param callable $callback Callback to retrieve log of pull |
||
149 | * |
||
150 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
151 | * |
||
152 | * @return Image |
||
153 | */ |
||
154 | public function pull($name, $tag = 'latest', callable $callback = null) |
||
179 | |||
180 | /** |
||
181 | * Push an image. |
||
182 | * |
||
183 | * @param string $name |
||
184 | * @param string $tag |
||
185 | * @param string $registryAuth |
||
186 | * @param callable $callback |
||
187 | * |
||
188 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
189 | * |
||
190 | * @return Image |
||
191 | */ |
||
192 | public function push($name, $tag, $registryAuth, callable $callback = null) |
||
212 | |||
213 | /** |
||
214 | * Build docker image (Docker API v1.20) |
||
215 | * |
||
216 | * POST /build request returns empty body with different headers so it's not needed to build something |
||
217 | * like UnexpectedStatusCodeException. We can get response success or fail only. |
||
218 | * |
||
219 | * @param array $options |
||
220 | * dockerfile - Path within the build context to the Dockerfile. This is ignored if remote is specified and points to an individual filename. |
||
221 | * t – A repository name (and optionally a tag) to apply to the resulting image in case of success. |
||
222 | * remote – A Git repository URI or HTTP/HTTPS URI build source. If the URI specifies a filename, the file’s contents are placed into a file called Dockerfile. |
||
223 | * q – Suppress verbose build output. |
||
224 | * nocache – Do not use the cache when building the image. |
||
225 | * pull - Attempt to pull the image even if an older image exists locally. |
||
226 | * rm - Remove intermediate containers after a successful build (default behavior). |
||
227 | * forcerm - Always remove intermediate containers (includes rm). |
||
228 | * memory - Set memory limit for build. |
||
229 | * memswap - Total memory (memory + swap), -1 to disable swap. |
||
230 | * cpushares - CPU shares (relative weight). |
||
231 | * cpusetcpus - CPUs in which to allow execution (e.g., 0-3, 0,1). |
||
232 | * cpuperiod - The length of a CPU period in microseconds. |
||
233 | * cpuquota - Microseconds of CPU time that the container can get in a CPU period |
||
234 | * |
||
235 | * @param $dockerfileString - Dockerfile contents |
||
236 | * @param callable|null $callback |
||
237 | * @return bool |
||
238 | * @throws \GuzzleHttp\Exception\RequestException |
||
239 | */ |
||
240 | public function build($dockerfileString, callable $callback = null, array $options = []) |
||
286 | |||
287 | /** |
||
288 | * Remove an image from docker daemon |
||
289 | * |
||
290 | * @param Image $image Image to remove |
||
291 | * @param boolean $force Force removal of image (default false) |
||
292 | * @param boolean $noprune Do not remove parent images (default false) |
||
293 | * |
||
294 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
295 | * |
||
296 | * @return ImageManager |
||
297 | */ |
||
298 | View Code Duplication | public function remove(Image $image, $force = false, $noprune = false) |
|
313 | |||
314 | /** |
||
315 | * Remove multiple images from docker daemon |
||
316 | * |
||
317 | * @param Image[]|array $images Images to remove |
||
318 | * @param boolean $force Force removal of image (default false) |
||
319 | * @param boolean $noprune Do not remove parent images (default false) |
||
320 | * |
||
321 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
322 | * |
||
323 | * @return ImageManager |
||
324 | */ |
||
325 | public function removeImages(array $images, $force = false, $noprune = false) |
||
340 | |||
341 | /** |
||
342 | * Search for an image on Docker Hub. |
||
343 | * |
||
344 | * @param string $term term to search |
||
345 | * |
||
346 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function search($term) |
||
367 | |||
368 | /** |
||
369 | * Tag an image |
||
370 | * |
||
371 | * @param Image $image image to tag |
||
372 | * @param $repository Repository name to use |
||
373 | * @param string $tag Tag to use |
||
374 | * @param bool $force Force to set tag even if an image with the same name already exists ? |
||
375 | * |
||
376 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
377 | * |
||
378 | * @return ImageManager |
||
379 | */ |
||
380 | public function tag(Image $image, $repository, $tag = 'latest', $force = false) |
||
400 | |||
401 | /** |
||
402 | * Get history of an image |
||
403 | * |
||
404 | * @param Image $image |
||
405 | * |
||
406 | * @throws \Docker\Exception\UnexpectedStatusCodeException |
||
407 | * |
||
408 | * @return array |
||
409 | */ |
||
410 | View Code Duplication | public function history(Image $image) |
|
422 | } |
||
423 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.