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 |
||
9 | class Format implements PropertyConstraint |
||
10 | { |
||
11 | // @codingStandardsIgnoreStart |
||
12 | const DATE_TIME_PATTERN = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})([Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(\\.[0-9]+)?)?(([Zz]|([+-])([0-9]{2}):([0-9]{2})))?/'; |
||
13 | // @codingStandardsIgnoreEnd |
||
14 | |||
15 | const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i'; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | 2 | public static function validate($value, $parameter, $pointer = null) |
|
77 | |||
78 | /** |
||
79 | * @param string $format |
||
80 | * @param mixed $value |
||
81 | * @param string $pattern |
||
82 | * @param int $errorCode |
||
83 | * @param string $pointer |
||
84 | * |
||
85 | * @return \League\JsonGuard\ValidationError|null |
||
86 | */ |
||
87 | 2 | View Code Duplication | private static function validateRegex($format, $value, $pattern, $errorCode, $pointer) |
101 | |||
102 | /** |
||
103 | * @param string $format |
||
104 | * @param mixed $value |
||
105 | * @param int $filter |
||
106 | 2 | * @param mixed $options |
|
107 | * @param int $errorCode |
||
108 | 2 | * @param string $pointer |
|
109 | 2 | * |
|
110 | * @return \League\JsonGuard\ValidationError|null |
||
111 | */ |
||
112 | private static function validateFilter($format, $value, $filter, $options, $errorCode, $pointer) |
||
134 | } |
||
135 |
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.