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 ContainerResource extends Resource |
||
9 | { |
||
10 | /** |
||
11 | * List containers. |
||
12 | * |
||
13 | * @param array $parameters List of parameters |
||
14 | * |
||
15 | * (bool)all: Show all containers. Only running containers are shown by default (i.e., this defaults to false) |
||
16 | * (int)limit: Show <limit> last created containers, include non-running ones. |
||
17 | * (string)since: Show only containers created since Id, include non-running ones. |
||
18 | * (string)before: Show only containers created before Id, include non-running ones. |
||
19 | * (bool)size: 1/True/true or 0/False/false, Show the containers sizes. |
||
20 | * (array)filters: A JSON encoded value of the filters (a map[string][]string) to process on the containers list |
||
21 | * @param string $fetch Fetch mode (object or response) |
||
22 | * |
||
23 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\ContainerConfig[] |
||
24 | */ |
||
25 | public function findAll($parameters = [], $fetch = self::FETCH_OBJECT) |
||
48 | |||
49 | /** |
||
50 | * Create a container. |
||
51 | * |
||
52 | * @param \Docker\API\Model\ContainerConfig $container Container to create |
||
53 | * @param array $parameters List of parameters |
||
54 | * |
||
55 | * (string)name: Assign the specified name to the container. Must match /?[a-zA-Z0-9_-]+. |
||
56 | * (string)Content-Type: Content Type of input |
||
57 | * @param string $fetch Fetch mode (object or response) |
||
58 | * |
||
59 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\ContainerCreateResult |
||
60 | */ |
||
61 | View Code Duplication | public function create(\Docker\API\Model\ContainerConfig $container, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
|
|||
62 | { |
||
63 | $queryParam = new QueryParam(); |
||
64 | $queryParam->setDefault('name', null); |
||
65 | $queryParam->setDefault('Content-Type', 'application/json'); |
||
66 | $queryParam->setHeaderParameters(['Content-Type']); |
||
67 | $url = '/containers/create'; |
||
68 | $url = $url . ('?' . $queryParam->buildQueryString($parameters)); |
||
69 | $headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); |
||
70 | $body = $this->serializer->serialize($container, 'json'); |
||
71 | $request = $this->messageFactory->createRequest('POST', $url, $headers, $body); |
||
72 | $response = $this->httpClient->sendRequest($request); |
||
73 | if (self::FETCH_OBJECT == $fetch) { |
||
74 | if ('201' == $response->getStatusCode()) { |
||
75 | return $this->serializer->deserialize($response->getBody()->getContents(), 'Docker\\API\\Model\\ContainerCreateResult', 'json'); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return $response; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return low-level information on the container id. |
||
84 | * |
||
85 | * @param string $id The container id or name |
||
86 | * @param array $parameters List of parameters |
||
87 | * @param string $fetch Fetch mode (object or response) |
||
88 | * |
||
89 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\Container |
||
90 | */ |
||
91 | public function find($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
109 | |||
110 | /** |
||
111 | * List processes running inside the container id. |
||
112 | * |
||
113 | * @param string $id The container id or name |
||
114 | * @param array $parameters List of parameters |
||
115 | * |
||
116 | * (string)ps_args: ps arguments to use (e.g., aux) |
||
117 | * @param string $fetch Fetch mode (object or response) |
||
118 | * |
||
119 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\ContainerTop |
||
120 | */ |
||
121 | public function listProcesses($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
140 | |||
141 | /** |
||
142 | * Get stdout and stderr logs from the container id. Note: This endpoint works only for containers with json-file logging driver. |
||
143 | * |
||
144 | * @param string $id The container id or name |
||
145 | * @param array $parameters List of parameters |
||
146 | * |
||
147 | * (bool)follow: 1/True/true or 0/False/false, return stream. Default false. |
||
148 | * (bool)stdout: 1/True/true or 0/False/false, show stdout log. Default false. |
||
149 | * (bool)stderr: 1/True/true or 0/False/false, show stderr log. Default false. |
||
150 | * (int)since: UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp. Default: 0 (unfiltered) |
||
151 | * (bool)timestamps: 1/True/true or 0/False/false, print timestamps for every log line. |
||
152 | * (string)tail: Output specified number of lines at the end of logs: all or <number>. Default all. |
||
153 | * @param string $fetch Fetch mode (object or response) |
||
154 | * |
||
155 | * @return \Psr\Http\Message\ResponseInterface |
||
156 | */ |
||
157 | View Code Duplication | public function logs($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
176 | |||
177 | /** |
||
178 | * Inspect changes on a container’s filesystem. |
||
179 | * |
||
180 | * @param string $id The container id or name |
||
181 | * @param array $parameters List of parameters |
||
182 | * |
||
183 | * (int)kind: Kind of changes |
||
184 | * @param string $fetch Fetch mode (object or response) |
||
185 | * |
||
186 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\ContainerChange[] |
||
187 | */ |
||
188 | public function changes($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
207 | |||
208 | /** |
||
209 | * Export the contents of container id. |
||
210 | * |
||
211 | * @param string $id The container id or name |
||
212 | * @param array $parameters List of parameters |
||
213 | * @param string $fetch Fetch mode (object or response) |
||
214 | * |
||
215 | * @return \Psr\Http\Message\ResponseInterface |
||
216 | */ |
||
217 | public function export($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
230 | |||
231 | /** |
||
232 | * This endpoint returns a live stream of a container’s resource usage statistics. |
||
233 | * |
||
234 | * @param string $id The container id or name |
||
235 | * @param array $parameters List of parameters |
||
236 | * |
||
237 | * (bool)stream: Stream stats |
||
238 | * @param string $fetch Fetch mode (object or response) |
||
239 | * |
||
240 | * @return \Psr\Http\Message\ResponseInterface |
||
241 | */ |
||
242 | public function stats($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
256 | |||
257 | /** |
||
258 | * Resize the TTY for container with id. The unit is number of characters. You must restart the container for the resize to take effect. |
||
259 | * |
||
260 | * @param string $id The container id or name |
||
261 | * @param array $parameters List of parameters |
||
262 | * |
||
263 | * (int)h: Height of the tty session |
||
264 | * (int)w: Width of the tty session |
||
265 | * @param string $fetch Fetch mode (object or response) |
||
266 | * |
||
267 | * @return \Psr\Http\Message\ResponseInterface |
||
268 | */ |
||
269 | public function resize($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
270 | { |
||
271 | $queryParam = new QueryParam(); |
||
272 | $queryParam->setDefault('h', null); |
||
273 | $queryParam->setDefault('w', null); |
||
274 | $url = '/containers/{id}/resize'; |
||
275 | $url = str_replace('{id}', $id, $url); |
||
276 | $url = $url . ('?' . $queryParam->buildQueryString($parameters)); |
||
277 | $headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); |
||
278 | $body = $queryParam->buildFormDataString($parameters); |
||
279 | $request = $this->messageFactory->createRequest('POST', $url, $headers, $body); |
||
280 | $response = $this->httpClient->sendRequest($request); |
||
281 | |||
282 | return $response; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Start the container id. |
||
287 | * |
||
288 | * @param string $id The container id or name |
||
289 | * @param array $parameters List of parameters |
||
290 | * @param string $fetch Fetch mode (object or response) |
||
291 | * |
||
292 | * @return \Psr\Http\Message\ResponseInterface |
||
293 | */ |
||
294 | public function start($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
307 | |||
308 | /** |
||
309 | * Stop the container id. |
||
310 | * |
||
311 | * @param string $id The container id or name |
||
312 | * @param array $parameters List of parameters |
||
313 | * |
||
314 | * (int)t: number of seconds to wait before killing the container |
||
315 | * @param string $fetch Fetch mode (object or response) |
||
316 | * |
||
317 | * @return \Psr\Http\Message\ResponseInterface |
||
318 | */ |
||
319 | public function stop($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
333 | |||
334 | /** |
||
335 | * Restart the container id. |
||
336 | * |
||
337 | * @param string $id The container id or name |
||
338 | * @param array $parameters List of parameters |
||
339 | * |
||
340 | * (int)t: number of seconds to wait before killing the container |
||
341 | * @param string $fetch Fetch mode (object or response) |
||
342 | * |
||
343 | * @return \Psr\Http\Message\ResponseInterface |
||
344 | */ |
||
345 | public function restart($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
359 | |||
360 | /** |
||
361 | * Send a posix signal to a container. |
||
362 | * |
||
363 | * @param string $id The container id or name |
||
364 | * @param array $parameters List of parameters |
||
365 | * |
||
366 | * (string)signal: Signal to send to the container, integer or string like SIGINT, defaults to SIGKILL |
||
367 | * @param string $fetch Fetch mode (object or response) |
||
368 | * |
||
369 | * @return \Psr\Http\Message\ResponseInterface |
||
370 | */ |
||
371 | public function kill($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
385 | |||
386 | /** |
||
387 | * Rename the container id to a new_name. |
||
388 | * |
||
389 | * @param string $id The container id or name |
||
390 | * @param array $parameters List of parameters |
||
391 | * |
||
392 | * (string)name: New name for the container |
||
393 | * @param string $fetch Fetch mode (object or response) |
||
394 | * |
||
395 | * @return \Psr\Http\Message\ResponseInterface |
||
396 | */ |
||
397 | public function rename($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
411 | |||
412 | /** |
||
413 | * Pause the container id. |
||
414 | * |
||
415 | * @param string $id The container id or name |
||
416 | * @param array $parameters List of parameters |
||
417 | * @param string $fetch Fetch mode (object or response) |
||
418 | * |
||
419 | * @return \Psr\Http\Message\ResponseInterface |
||
420 | */ |
||
421 | public function pause($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
434 | |||
435 | /** |
||
436 | * Unpause the container id. |
||
437 | * |
||
438 | * @param string $id The container id or name |
||
439 | * @param array $parameters List of parameters |
||
440 | * @param string $fetch Fetch mode (object or response) |
||
441 | * |
||
442 | * @return \Psr\Http\Message\ResponseInterface |
||
443 | */ |
||
444 | public function unpause($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
457 | |||
458 | /** |
||
459 | * Attach to the container id. |
||
460 | * |
||
461 | * @param string $id The container id or name |
||
462 | * @param array $parameters List of parameters |
||
463 | * |
||
464 | * (string)logs: 1/True/true or 0/False/false, return logs. Default false |
||
465 | * (string)stream: 1/True/true or 0/False/false, return stream. Default false |
||
466 | * (string)stdin: 1/True/true or 0/False/false, if stream=true, attach to stdin. Default false. |
||
467 | * (string)stdout: 1/True/true or 0/False/false, if logs=true, return stdout log, if stream=true, attach to stdout. Default false. |
||
468 | * (string)stderr: 1/True/true or 0/False/false, if logs=true, return stderr log, if stream=true, attach to stderr. Default false. |
||
469 | * @param string $fetch Fetch mode (object or response) |
||
470 | * |
||
471 | * @return \Psr\Http\Message\ResponseInterface |
||
472 | */ |
||
473 | View Code Duplication | public function attach($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
491 | |||
492 | /** |
||
493 | * Attach to the container id with a websocket. |
||
494 | * |
||
495 | * @param string $id The container id or name |
||
496 | * @param array $parameters List of parameters |
||
497 | * |
||
498 | * (string)logs: 1/True/true or 0/False/false, return logs. Default false |
||
499 | * (string)stream: 1/True/true or 0/False/false, return stream. Default false |
||
500 | * (string)stdin: 1/True/true or 0/False/false, if stream=true, attach to stdin. Default false. |
||
501 | * (string)stdout: 1/True/true or 0/False/false, if logs=true, return stdout log, if stream=true, attach to stdout. Default false. |
||
502 | * (string)stderr: 1/True/true or 0/False/false, if logs=true, return stderr log, if stream=true, attach to stderr. Default false. |
||
503 | * @param string $fetch Fetch mode (object or response) |
||
504 | * |
||
505 | * @return \Psr\Http\Message\ResponseInterface |
||
506 | */ |
||
507 | View Code Duplication | public function attachWebsocket($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
|
525 | |||
526 | /** |
||
527 | * Block until container id stops, then returns the exit code. |
||
528 | * |
||
529 | * @param string $id The container id or name |
||
530 | * @param array $parameters List of parameters |
||
531 | * @param string $fetch Fetch mode (object or response) |
||
532 | * |
||
533 | * @return \Psr\Http\Message\ResponseInterface|\Docker\API\Model\ContainerWait |
||
534 | */ |
||
535 | public function wait($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
553 | |||
554 | /** |
||
555 | * Remove the container id from the filesystem. |
||
556 | * |
||
557 | * @param string $id The container id or name |
||
558 | * @param array $parameters List of parameters |
||
559 | * |
||
560 | * (string)v: 1/True/true or 0/False/false, Remove the volumes associated to the container. Default false. |
||
561 | * (string)force: 1/True/true or 0/False/false, Kill then remove the container. Default false. |
||
562 | * @param string $fetch Fetch mode (object or response) |
||
563 | * |
||
564 | * @return \Psr\Http\Message\ResponseInterface |
||
565 | */ |
||
566 | public function remove($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
581 | |||
582 | /** |
||
583 | * Get an tar archive of a resource in the filesystem of container id. |
||
584 | * |
||
585 | * @param string $id The container id or name |
||
586 | * @param array $parameters List of parameters |
||
587 | * |
||
588 | * (string)path: Resource in the container’s filesystem to archive. |
||
589 | * @param string $fetch Fetch mode (object or response) |
||
590 | * |
||
591 | * @return \Psr\Http\Message\ResponseInterface |
||
592 | */ |
||
593 | public function getArchive($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
607 | |||
608 | /** |
||
609 | * Retrieving information about files and folders in a container. |
||
610 | * |
||
611 | * @param string $id The container id or name |
||
612 | * @param array $parameters List of parameters |
||
613 | * |
||
614 | * (string)path: Resource in the container’s filesystem to archive. |
||
615 | * @param string $fetch Fetch mode (object or response) |
||
616 | * |
||
617 | * @return \Psr\Http\Message\ResponseInterface |
||
618 | */ |
||
619 | public function getArchiveInformation($id, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
633 | |||
634 | /** |
||
635 | * Upload a tar archive to be extracted to a path in the filesystem of container id. |
||
636 | * |
||
637 | * @param string $id The container id or name |
||
638 | * @param string $inputStream The input stream must be a tar archive compressed with one of the following algorithms: identity (no compression), gzip, bzip2, xz. |
||
639 | * @param array $parameters List of parameters |
||
640 | * |
||
641 | * (string)path: Path to a directory in the container to extract the archive’s contents into. |
||
642 | * (string)noOverwriteDirNonDir: If “1”, “true”, or “True” then it will be an error if unpacking the given content would cause an existing directory to be replaced with a non-directory and vice versa. |
||
643 | * @param string $fetch Fetch mode (object or response) |
||
644 | * |
||
645 | * @return \Psr\Http\Message\ResponseInterface |
||
646 | */ |
||
647 | public function putArchive($id, $inputStream, $parameters = [], $fetch = self::FETCH_OBJECT) |
||
662 | } |
||
663 |
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.