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\Execution; |
||
| 24 | final class BlockReplies |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Rule reply key. |
||
| 28 | */ |
||
| 29 | const REPLY_SUCCESS_VALUE = 0; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Rule reply key. |
||
| 33 | */ |
||
| 34 | const REPLY_ERRORS_INFO = self::REPLY_SUCCESS_VALUE + 1; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Error info key. |
||
| 38 | */ |
||
| 39 | const ERROR_INFO_BLOCK_INDEX = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Error info key. |
||
| 43 | */ |
||
| 44 | const ERROR_INFO_VALUE = self::ERROR_INFO_BLOCK_INDEX + 1; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Error info key. |
||
| 48 | */ |
||
| 49 | const ERROR_INFO_CODE = self::ERROR_INFO_VALUE + 1; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Error info key. |
||
| 53 | */ |
||
| 54 | const ERROR_INFO_CONTEXT = self::ERROR_INFO_CODE + 1; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param mixed $result |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public static function createSuccessReply($result): array |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public static function createStartSuccessReply(): array |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public static function createEndSuccessReply(): array |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param ContextInterface $context |
||
| 87 | * @param mixed $errorValue |
||
| 88 | * @param int $errorCode |
||
| 89 | * @param mixed|null $errorContext |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public static function createErrorReply( |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param ContextInterface $context |
||
| 114 | * @param int $errorCode |
||
| 115 | * @param mixed|null $errorContext |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | View Code Duplication | public static function createStartErrorReply(ContextInterface $context, int $errorCode, $errorContext = null): array |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param ContextInterface $context |
||
| 135 | * @param int $errorCode |
||
| 136 | * @param mixed|null $errorContext |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | View Code Duplication | public static function createEndErrorReply(ContextInterface $context, int $errorCode, $errorContext = null): array |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param int $blockId |
||
| 156 | * @param mixed $value |
||
| 157 | * @param int $code |
||
| 158 | * @param null|mixed $context |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | protected static function createErrorInfoEntry(int $blockId, $value, int $code, $context = null): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param array $result |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public static function isResultSuccessful(array $result): bool |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $result |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public static function extractResultOutput(array $result) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param array $result |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public static function extractResultErrorsInfo(array $result): array |
||
| 223 | } |
||
| 224 |
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.