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 namespace Limoncello\Validation\Rules\Comparisons; |
||
| 30 | abstract class BaseTwoValueComparision extends BaseRule implements ComparisionInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Property key. |
||
| 34 | */ |
||
| 35 | const PROPERTY_LOWER_VALUE = self::PROPERTY_LAST + 1; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Property key. |
||
| 39 | */ |
||
| 40 | const PROPERTY_UPPER_VALUE = self::PROPERTY_LOWER_VALUE + 1; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | private $lowerValue; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var mixed |
||
| 49 | */ |
||
| 50 | private $upperValue; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | private $errorCode; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var mixed |
||
| 59 | */ |
||
| 60 | private $errorContext; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param mixed $lowerValue |
||
| 64 | * @param mixed $upperValue |
||
| 65 | * @param int $errorCode |
||
| 66 | * @param mixed $errorContext |
||
| 67 | */ |
||
| 68 | public function __construct($lowerValue, $upperValue, int $errorCode, $errorContext = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritdoc |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public function getLowerValue() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | public function getUpperValue() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | protected function getErrorCode(): int |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getErrorContext() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param ContextInterface $context |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | protected static function readLowerValue(ContextInterface $context) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param ContextInterface $context |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | protected static function readUpperValue(ContextInterface $context) |
||
| 154 | } |
||
| 155 |
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.