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 |
||
24 | class BuildController extends Controller |
||
25 | { |
||
26 | public $defaultAction = 'build'; |
||
27 | |||
28 | /** |
||
29 | * @var string The name of the created image |
||
30 | * If not explicitly set will take its default from module config. |
||
31 | */ |
||
32 | public $image; |
||
33 | |||
34 | /** |
||
35 | * @var string The tag of the created image |
||
36 | * If not explicitly set will take its default from module config. |
||
37 | */ |
||
38 | public $tag; |
||
39 | |||
40 | /** |
||
41 | * @var bool whether to push the image after a successful build. |
||
42 | * If not explicitly set will take its default from module config. |
||
43 | */ |
||
44 | public $push; |
||
45 | |||
46 | /** |
||
47 | * @var Docker |
||
48 | */ |
||
49 | protected $docker; |
||
50 | |||
51 | /** |
||
52 | * @var string the user to authenticate against the repository |
||
53 | */ |
||
54 | public $user; |
||
55 | |||
56 | /** |
||
57 | * @var string the password to authenticate against the repository |
||
58 | */ |
||
59 | public $password; |
||
60 | |||
61 | 3 | public function init(): void |
|
69 | |||
70 | 2 | public function actionBuild(): void |
|
93 | |||
94 | 1 | private function logBuildInfo(BuildInfo $buildInfo): void |
|
109 | |||
110 | /** |
||
111 | * Push a docker container image |
||
112 | * @param string $name |
||
|
|||
113 | * @throws \Exception |
||
114 | */ |
||
115 | 1 | private function pushImage(): void |
|
116 | { |
||
117 | 1 | if (!isset($this->image)) { |
|
118 | throw new \InvalidArgumentException("Image name must be configured when pushing images"); |
||
119 | } |
||
120 | 1 | $name = "{$this->image}:{$this->tag}"; |
|
121 | |||
122 | 1 | $authConfig = new AuthConfig(); |
|
123 | 1 | $authConfig->setUsername($this->user); |
|
124 | 1 | $authConfig->setPassword($this->password); |
|
125 | $params = [ |
||
126 | 1 | 'X-Registry-Auth' => $authConfig |
|
127 | ]; |
||
128 | /** @var PushStream $pushStream */ |
||
129 | 1 | $pushStream = $this->docker->imagePush($name, [], $params ?? [], Docker::FETCH_OBJECT); |
|
130 | |||
131 | 1 | if ($pushStream instanceof ResponseInterface) { |
|
132 | throw new \Exception($pushStream->getReasonPhrase() . ':' . $pushStream->getBody()->getContents(), $pushStream->getStatusCode()); |
||
133 | } |
||
134 | |||
135 | $pushStream->onFrame(function(PushImageInfo $pushImageInfo): void { |
||
136 | 1 | if (!empty($pushImageInfo->getError())) { |
|
137 | 1 | throw new \Exception($pushImageInfo->getError()); |
|
138 | } |
||
139 | 1 | $this->stdout($pushImageInfo->getProgress(), Console::FG_YELLOW); |
|
140 | 1 | $this->stdout($pushImageInfo->getStatus(), Console::FG_RED); |
|
141 | 1 | }); |
|
142 | 1 | $pushStream->wait(); |
|
143 | } |
||
144 | |||
145 | public function actionTestClient(): void |
||
149 | |||
150 | 2 | public function createBuildStream(array $params = []): BuildStream |
|
156 | |||
157 | 1 | public function options($actionID) |
|
173 | |||
174 | public function optionAliases() |
||
184 | |||
185 | 1 | View Code Duplication | public function stdout($string) |
196 | |||
197 | |||
198 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.