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 RestValidatorHelper { |
||
10 | const DefaultMaxLength = 1600; |
||
11 | const PHP_INT_MIN = -2147483648; |
||
12 | |||
13 | /** |
||
14 | * @param array $data the data from the request |
||
15 | * @param string $field the field, that should be checked |
||
16 | * @param array $options |
||
17 | * @return string |
||
|
|||
18 | * @throws \ValidationException |
||
19 | */ |
||
20 | public static function validate_string($data, $field, $options=[]) { |
||
44 | |||
45 | /** |
||
46 | * @param mixed $data |
||
47 | * @param string $field |
||
48 | * @param array $options |
||
49 | * @return int |
||
50 | * @throws \ValidationException |
||
51 | */ |
||
52 | public static function validate_int($data, $field, $options=[]) { |
||
72 | |||
73 | /** |
||
74 | * @param mixed $data |
||
75 | * @param string $field |
||
76 | * @param array $options |
||
77 | * @return string |
||
78 | * @throws \ValidationException |
||
79 | */ |
||
80 | public static function validate_datetime($data, $field, $options=[]) { |
||
97 | |||
98 | /** |
||
99 | * @param mixed $data |
||
100 | * @param string $field |
||
101 | * @param array $options |
||
102 | * @return string |
||
103 | * @throws \ValidationException |
||
104 | */ |
||
105 | View Code Duplication | public static function validate_url($data, $field, $options=[]) { |
|
120 | |||
121 | /** |
||
122 | * Validates an URL (defined in RFC 3986). |
||
123 | * |
||
124 | * @param string $url the url, that should be validated |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public static function is_url($url) { |
||
134 | |||
135 | /** |
||
136 | * @param mixed $data |
||
137 | * @param string $field |
||
138 | * @param array $options |
||
139 | * @return string |
||
140 | * @throws \ValidationException |
||
141 | */ |
||
142 | public static function validate_country_code($data, $field, $options=[]) { |
||
158 | |||
159 | /** |
||
160 | * @param mixed $data |
||
161 | * @param string $field |
||
162 | * @param array $options |
||
163 | * @return string |
||
164 | * @throws \ValidationException |
||
165 | */ |
||
166 | View Code Duplication | public static function validate_email($data, $field, $options=[]) { |
|
181 | } |
||
182 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.