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 |
||
| 4 | class DeployPlanDispatcher extends Dispatcher { |
||
| 5 | |||
| 6 | const ACTION_PLAN = 'plan'; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @var array |
||
| 10 | */ |
||
| 11 | private static $action_types = [ |
||
| 12 | self::ACTION_PLAN |
||
| 13 | ]; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public static $allowed_actions = [ |
||
| 19 | 'gitupdate', |
||
| 20 | 'gitrefs', |
||
| 21 | 'deploysummary' |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \DNProject |
||
| 26 | */ |
||
| 27 | protected $project = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \DNEnvironment |
||
| 31 | */ |
||
| 32 | protected $environment = null; |
||
| 33 | |||
| 34 | public function init() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function Link() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | * @param \SS_HTTPRequest $request |
||
| 60 | * |
||
| 61 | * @return \HTMLText|\SS_HTTPResponse |
||
| 62 | */ |
||
| 63 | public function index(\SS_HTTPRequest $request) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param SS_HTTPRequest $request |
||
| 72 | * @return SS_HTTPResponse |
||
| 73 | */ |
||
| 74 | public function gitupdate(SS_HTTPRequest $request) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param SS_HTTPRequest $request |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function gitrefs(\SS_HTTPRequest $request) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Generate the data structure used by the frontend component. |
||
| 131 | * |
||
| 132 | * @param string $name of the component |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function getModel($name) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param int $ID |
||
| 144 | * @return SS_HTTPResponse |
||
| 145 | */ |
||
| 146 | protected function getFetch($ID) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return SS_HTTPResponse |
||
| 162 | */ |
||
| 163 | View Code Duplication | protected function createFetch() { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param SS_HTTPRequest $request |
||
| 183 | * |
||
| 184 | * @return SS_HTTPResponse |
||
| 185 | */ |
||
| 186 | public function deploysummary(SS_HTTPRequest $request) { |
||
| 187 | if(!trim($request->getBody())) { |
||
| 188 | return $this->getAPIResponse('no body was sent in the request', 400); |
||
| 189 | } |
||
| 190 | |||
| 191 | $jsonBody = json_decode($request->getBody(), true); |
||
| 192 | if(empty($jsonBody)) { |
||
| 193 | return $this->getAPIResponse('request did not contain a parsable JSON payload', 400); |
||
| 194 | } |
||
| 195 | |||
| 196 | $options = [ |
||
| 197 | 'sha' => $jsonBody['sha'] |
||
| 198 | ]; |
||
| 199 | |||
| 200 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
||
| 201 | $data = $strategy->toArray(); |
||
| 202 | |||
| 203 | $interface = $this->project->getRepositoryInterface(); |
||
| 204 | if($this->canCompareCodeVersions($interface, $data['changes']['Code version'])) { |
||
| 205 | $compareurl = sprintf( |
||
| 206 | '%s/compare/%s...%s', |
||
| 207 | $interface->URL, |
||
| 208 | $data['changes']['Code version']['from'], |
||
| 209 | $data['changes']['Code version']['to'] |
||
| 210 | ); |
||
| 211 | $data['changes']['Code version']['compareUrl'] = $compareurl; |
||
| 212 | } |
||
| 213 | |||
| 214 | // Append json to response |
||
| 215 | $token = SecurityToken::inst(); |
||
| 216 | $data['SecurityID'] = $token->getValue(); |
||
| 217 | |||
| 218 | $this->extend('updateDeploySummary', $data); |
||
| 219 | |||
| 220 | return json_encode($data); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $project |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function getGitBranches($project) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $project |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | protected function getGitTags($project) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $project |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | protected function getGitPrevDeploys($project) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Return a simple response with a message |
||
| 290 | * |
||
| 291 | * @param string $message |
||
| 292 | * @param int $statusCode |
||
| 293 | * @return SS_HTTPResponse |
||
| 294 | */ |
||
| 295 | protected function getAPIResponse($message, $statusCode) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param ArrayData $interface |
||
| 310 | * @param array $codeVersion |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | protected function canCompareCodeVersions(\ArrayData $interface, $codeVersion) { |
||
| 329 | } |
||
| 330 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: