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 ConstraintControllerHandlesMethod extends BaseConstraintBecausePhpunitSucksAtBackwardsCompatibility |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Controller The Controller instance. |
||
| 27 | */ |
||
| 28 | protected $controllerInstance; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool Whether generic 'execute' methods should be accepted. |
||
| 32 | */ |
||
| 33 | protected $acceptGeneric; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Class constructor. |
||
| 37 | * |
||
| 38 | * @param Controller $controllerInstance Instance of the Controller to test. |
||
| 39 | * @param bool $acceptGeneric Whether generic execute methods should be accepted. |
||
| 40 | * |
||
| 41 | * @author Felix Gilcher <[email protected]> |
||
| 42 | * @since 1.0.0 |
||
| 43 | */ |
||
| 44 | public function __construct(Controller $controllerInstance, $acceptGeneric = true) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Evaluates the constraint for parameter $other. Returns TRUE if the |
||
| 52 | * constraint is met, FALSE otherwise. |
||
| 53 | * |
||
| 54 | * @param mixed $other Value or object to evaluate. |
||
| 55 | * |
||
| 56 | * @return bool The result of the evaluation. |
||
| 57 | * |
||
| 58 | * @author Felix Gilcher <[email protected]> |
||
| 59 | * @since 1.0.7 |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function matches($other) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Returns a string representation of the constraint. |
||
| 73 | * |
||
| 74 | * @return string The string representation. |
||
| 75 | * |
||
| 76 | * @author Felix Gilcher <[email protected]> |
||
| 77 | * @since 1.0.0 |
||
| 78 | */ |
||
| 79 | public function toString() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a custom error description. |
||
| 89 | * |
||
| 90 | * @param mixed $other Value or object to evaluate. |
||
| 91 | * @param string $description The original description. |
||
| 92 | * @param bool $not true if the constraint was negated. |
||
| 93 | * |
||
| 94 | * @return string The error description. |
||
| 95 | * |
||
| 96 | * @author Felix Gilcher <[email protected]> |
||
| 97 | * @since 1.0.0 |
||
| 98 | */ |
||
| 99 | View Code Duplication | protected function customFailureDescription($other, $description, $not) |
|
| 115 | } |
||
| 116 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.