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 | trait DateValidatorTrait | ||
| 11 | { | ||
| 12 | /** @var UIValidationEngine */ | ||
| 13 | protected $validationEngine; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Exceptions are caught in order to be processed later | ||
| 17 | * @param mixed $value Date Y-m-d ? | ||
| 18 | * | ||
| 19 | * @return \DateTime | ||
| 20 | */ | ||
| 21 | public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Exceptions are caught in order to be processed later | ||
| 52 | * @param mixed $value null|DateTime Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 or null ? | ||
| 53 | * | ||
| 54 | * @return \DateTime|null | ||
|  | |||
| 55 | */ | ||
| 56 | View Code Duplication | public function mustBeDateTimeOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) | |
| 64 | |||
| 65 | /** | ||
| 66 | * Exceptions are caught in order to be processed later | ||
| 67 | * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ? | ||
| 68 | * | ||
| 69 | * @return \DateTime|false | ||
| 70 | */ | ||
| 71 | public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) | ||
| 101 | |||
| 102 | private function createDefaultDateTime(): \DateTime | ||
| 108 | } | ||
| 109 | 
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.