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 |
||
| 10 | class UUID extends StringLiteral |
||
| 11 | { |
||
| 12 | /** @var BaseUuid */ |
||
| 13 | protected $value; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string $uuid |
||
|
|
|||
| 17 | * @return UUID |
||
| 18 | * @throws \ValueObjects\Exception\InvalidNativeArgumentException |
||
| 19 | */ |
||
| 20 | 1 | public static function fromNative() |
|
| 27 | |||
| 28 | /** |
||
| 29 | * Generate a new UUID string |
||
| 30 | * |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | 1 | public static function generateAsString() |
|
| 40 | |||
| 41 | 28 | public function __construct($value = null) |
|
| 42 | { |
||
| 43 | 28 | $uuid_str = BaseUuid::uuid4(); |
|
| 44 | |||
| 45 | 28 | if (null !== $value) { |
|
| 46 | 2 | $pattern = '/'.BaseUuid::VALID_PATTERN.'/'; |
|
| 47 | |||
| 48 | 2 | if (! \preg_match($pattern, $value)) { |
|
| 49 | 1 | throw new InvalidNativeArgumentException($value, array('UUID string')); |
|
| 50 | } |
||
| 51 | |||
| 52 | 1 | $uuid_str = $value; |
|
| 53 | } |
||
| 54 | |||
| 55 | 27 | $this->value = \strval($uuid_str); |
|
| 56 | 27 | } |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Tells whether two UUID are equal by comparing their values |
||
| 60 | * |
||
| 61 | * @param UUID $uuid |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | 2 | View Code Duplication | public function sameValueAs(ValueObjectInterface $uuid) |
| 72 | } |
||
| 73 |
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.