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 |
|
| 62 | { |
||
| 63 | 3 | parent::init(); |
|
| 64 | 3 | $this->docker = Docker::create(); |
|
| 65 | 3 | $this->push = $this->module->push; |
|
| 66 | 3 | $this->image = $this->module->image; |
|
| 67 | 3 | $this->tag = $this->module->tag; |
|
| 68 | } |
||
| 69 | |||
| 70 | 2 | public function actionBuild(): void |
|
| 71 | { |
||
| 72 | 2 | if ($this->push && !isset($this->image, $this->user, $this->password)) { |
|
| 73 | 1 | throw new InvalidConfigException("When using the push option, you must configure or provide user, password and image"); |
|
| 74 | } |
||
| 75 | |||
| 76 | 1 | $params = []; |
|
| 77 | |||
| 78 | |||
| 79 | 1 | if (isset($this->image)) { |
|
| 80 | 1 | $params['t'] = "{$this->image}:{$this->tag}"; |
|
| 81 | } |
||
| 82 | 1 | $buildStream = $this->createBuildStream($params); |
|
| 83 | 1 | $this->color = true; |
|
| 84 | 1 | $buildStream->onFrame(\Closure::fromCallable([$this, 'logBuildInfo'])); |
|
| 85 | 1 | $buildStream->wait(); |
|
| 86 | 1 | $this->stdout("Wait finished\n"); |
|
| 87 | 1 | $buildStream->wait(); |
|
| 88 | |||
| 89 | 1 | if ($this->push) { |
|
| 90 | 1 | $this->pushImage(); |
|
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | 1 | private function logBuildInfo(BuildInfo $buildInfo): void |
|
| 95 | { |
||
| 96 | 1 | $this->stdout($buildInfo->getStream(), Console::FG_CYAN); |
|
| 97 | 1 | $this->stdout($buildInfo->getProgress(), Console::FG_YELLOW); |
|
| 98 | 1 | $this->stdout($buildInfo->getStatus(), Console::FG_RED); |
|
| 99 | 1 | if (!empty($buildInfo->getProgressDetail())) { |
|
| 100 | $this->stdout($buildInfo->getProgressDetail()->getMessage(), Console::FG_YELLOW); |
||
| 101 | } |
||
| 102 | 1 | if (!empty($buildInfo->getErrorDetail())) { |
|
| 103 | $this->stdout($buildInfo->getErrorDetail()->getCode() . ':' . $buildInfo->getErrorDetail()->getMessage(), Console::FG_YELLOW); |
||
| 104 | } |
||
| 105 | 1 | if (!\is_null($buildInfo->getError())) { |
|
| 106 | throw new \Exception($buildInfo->getError() . ':' . $buildInfo->getErrorDetail()->getMessage()); |
||
| 107 | } |
||
| 108 | } |
||
| 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 |
||
| 146 | { |
||
| 147 | $this->stdout("It seems the console client works!\n", Console::FG_GREEN); |
||
| 148 | } |
||
| 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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.