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 |
||
| 19 | View Code Duplication | final class AndPredicate implements PredicateDecorator |
|
| 20 | { |
||
| 21 | protected $predicate1; |
||
| 22 | protected $predicate2; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * AndPredicate constructor. |
||
| 26 | * |
||
| 27 | * @param Predicate $predicate1 The first predicate to check. |
||
| 28 | * @param Predicate $predicate2 The second predicate to check. |
||
| 29 | */ |
||
| 30 | 5 | public function __construct(Predicate $predicate1, Predicate $predicate2) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Gets the two predicates being decorated as an array. |
||
| 38 | * |
||
| 39 | * @return array The predicates. |
||
| 40 | */ |
||
| 41 | 1 | public function getPredicates() : array |
|
| 42 | { |
||
| 43 | 1 | return [$this->predicate1, $this->predicate2]; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Evaluates the predicate returning true if both predicates return true. |
||
| 48 | * |
||
| 49 | * @param mixed $value The input value. |
||
| 50 | * @return bool true if both decorated predicates return true |
||
| 51 | */ |
||
| 52 | 4 | public function __invoke($value) : bool |
|
| 56 | } |
||
| 57 |