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 |
||
| 23 | class Server implements ServerInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The filesystem path where the web server is installed. |
||
| 27 | * |
||
| 28 | * It has to be an absolute path. |
||
| 29 | * |
||
| 30 | * Apache httpd server does not accept a relative prefix path at compilation. |
||
| 31 | * Nginx does, but this is a very risky practice... |
||
| 32 | * So relative prefix path in nginx configuration will not be considered |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $prefix = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The string to match as a starting multi-line directive. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $startMultiLine = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The string to match as an ending multi-line directive. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $endMultiLine = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The string to match a simple directive. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $simpleDirective = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Confirms if the server instance has valid parameters. |
||
| 61 | * |
||
| 62 | * @return bool true if all parameters are initialized, false otherwise |
||
| 63 | */ |
||
| 64 | 2 | public function isValid() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Getter for the prefix. |
||
| 76 | * |
||
| 77 | * @return string the filesystem path where the web server is installed |
||
| 78 | */ |
||
| 79 | 2 | public function getPrefix() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Sets the prefix of a server isntance. |
||
| 86 | * |
||
| 87 | * @throws ServerException if the prefix is invalid |
||
| 88 | * |
||
| 89 | * @param string $prefix the filesystem path where the web server is installed |
||
| 90 | */ |
||
| 91 | 11 | public function setPrefix($prefix) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Gets the regexp that will match the starting block directives. |
||
| 117 | * |
||
| 118 | * @return string the regexp that will match the starting block directives |
||
| 119 | */ |
||
| 120 | 1 | public function getStartMultiLine() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Sets the regexp that will match the starting block directives. |
||
| 127 | * |
||
| 128 | * @param string $simpleDirective the regexp that will match the starting block directives |
||
|
|
|||
| 129 | */ |
||
| 130 | 6 | View Code Duplication | public function setStartMultiLine($startMultiLine) |
| 153 | |||
| 154 | /** |
||
| 155 | * Gets the regexp that will match the ending block directives. |
||
| 156 | * |
||
| 157 | * @return string the regexp that will match the ending block directives |
||
| 158 | */ |
||
| 159 | 1 | public function getEndMultiLine() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Sets the regexp that will match the ending block directives. |
||
| 166 | * |
||
| 167 | * @param string $endMultiLine the regexp that will match the ending block directives |
||
| 168 | */ |
||
| 169 | 3 | View Code Duplication | public function setEndMultiLine($endMultiLine) |
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the regexp that will match the simple directives. |
||
| 194 | * |
||
| 195 | * @return string the regexp that will match the simple directives |
||
| 196 | */ |
||
| 197 | 1 | public function getSimpleDirective() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Sets the regexp that will match the simple directives. |
||
| 204 | * |
||
| 205 | * @param string $simpleDirective the regexp that will match the simple directives |
||
| 206 | */ |
||
| 207 | 6 | View Code Duplication | public function setSimpleDirective($simpleDirective) |
| 230 | |||
| 231 | /** |
||
| 232 | * Confirms if a matcher is a valid reguler expression. |
||
| 233 | * |
||
| 234 | * A directive matcher MUST contain a key and a value named subpattern. |
||
| 235 | * |
||
| 236 | * @param string $matcher the matcher to validate |
||
| 237 | * @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
||
| 238 | * |
||
| 239 | * @return bool true if the matcher is valid, false otherwise |
||
| 240 | */ |
||
| 241 | 12 | private function isValidRegex($matcher, $subpattern = true) |
|
| 253 | } |
||
| 254 |
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.