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; |
||
| 32 | abstract class BaseOneValueComparision extends BaseRule implements ComparisionInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Property key. |
||
| 36 | */ |
||
| 37 | const PROPERTY_VALUE = self::PROPERTY_LAST + 1; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var mixed |
||
| 41 | */ |
||
| 42 | private $value; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $errorCode; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var mixed |
||
| 51 | */ |
||
| 52 | private $errorContext; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param mixed $value |
||
| 56 | * @param int $errorCode |
||
| 57 | * @param mixed $errorContext |
||
| 58 | */ |
||
| 59 | public function __construct($value, int $errorCode, $errorContext = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @inheritdoc |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | public function getValue() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | protected function getErrorCode(): int |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function getErrorContext() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param ContextInterface $context |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | protected static function readValue(ContextInterface $context) |
||
| 121 | } |
||
| 122 |
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.