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 namespace Comodojo\RpcServer; |
||
| 25 | class RpcMethod { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Generic-to-RPC values map |
||
| 29 | * |
||
| 30 | * @var array $rpcvalues |
||
| 31 | */ |
||
| 32 | public static $rpcvalues = array( |
||
| 33 | "i4" => "int", |
||
| 34 | "int" => "int", |
||
| 35 | "double" => "double", |
||
| 36 | "float" => "double", |
||
| 37 | "boolean" => "boolean", |
||
| 38 | "bool" => "boolean", |
||
| 39 | "base64" => "base64", |
||
| 40 | "dateTime.iso8601" => "dateTime.iso8601", |
||
| 41 | "datetime" => "dateTime.iso8601", |
||
| 42 | "string" => "string", |
||
| 43 | "array" => "array", |
||
| 44 | "struct" => "struct", |
||
| 45 | "nil" => "null", |
||
| 46 | "ex:nil" => "null", |
||
| 47 | "null" => "null", |
||
| 48 | "undefined" => "undefined" |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Name of method |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $name = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Callback class|function |
||
| 60 | * |
||
| 61 | * @var string|function |
||
| 62 | */ |
||
| 63 | private $callback = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Description of method |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $description = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Array of supported signatures |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $signatures = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Internal pointer to current signature |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | private $current_signature = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Placeholder for additional arguments |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $arguments = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Class constructor |
||
| 95 | * |
||
| 96 | * @param string $name |
||
| 97 | * @param mixed $callback |
||
| 98 | * @param string $method |
||
|
|
|||
| 99 | * |
||
| 100 | * @throws Exception |
||
| 101 | */ |
||
| 102 | public function __construct($name, callable $callback, ...$arguments) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the method's name |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getName() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the method's callback |
||
| 131 | * |
||
| 132 | * @return callable |
||
| 133 | */ |
||
| 134 | public function getCallback() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the method's description |
||
| 142 | * |
||
| 143 | * @param string $description |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function setDescription($description = null) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the method's method |
||
| 161 | * |
||
| 162 | * @return string|null |
||
| 163 | */ |
||
| 164 | public function getDescription() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get additional arguments to forward to callback |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getArguments() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Add a signature and switch internal pointer |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function addSignature() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the method's signatures |
||
| 203 | * |
||
| 204 | * @param bool $compact (default) Compact signatures in a format compatible with system.methodSignature |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function getSignatures($compact = true) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the current method's signature |
||
| 232 | * |
||
| 233 | * @param bool $compact (default) Compact signatures in a format compatible with system.methodSignature |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function getSignature($compact = true) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Delete a signature |
||
| 253 | * |
||
| 254 | * @param integer $signature The signature's ID |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function deleteSignature($signature) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Select a signature |
||
| 275 | * |
||
| 276 | * @param integer $signature The signature's ID |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | * @throws Exception |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function selectSignature($signature) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Set the current signature's return type |
||
| 297 | * |
||
| 298 | * @param string $type |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | * @throws Exception |
||
| 302 | */ |
||
| 303 | public function setReturnType($type) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the current signature's return type |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getReturnType() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a parameter to current signature |
||
| 326 | * |
||
| 327 | * @param string $type |
||
| 328 | * @param string $name |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | * @throws Exception |
||
| 332 | */ |
||
| 333 | public function addParameter($type, $name) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Delete a parameter from current signature |
||
| 347 | * |
||
| 348 | * @param string $name |
||
| 349 | * |
||
| 350 | * @return $this |
||
| 351 | * @throws Exception |
||
| 352 | */ |
||
| 353 | public function deleteParameter($name) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get current signature's parameters |
||
| 365 | * |
||
| 366 | * @param string $format The output array format (ASSOC|NUMERIC) |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function getParameters($format = 'ASSOC') { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Static class constructor - create an RpcMethod object |
||
| 380 | * |
||
| 381 | * @param string $name |
||
| 382 | * @param string|function $callback |
||
| 383 | * @param string|null $method |
||
| 384 | * |
||
| 385 | * @return RpcMethod |
||
| 386 | * @throws Exception |
||
| 387 | */ |
||
| 388 | public static function create($name, $callback, ...$attributes) { |
||
| 403 | |||
| 404 | } |
||
| 405 |
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.