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 |
||
| 17 | class Validator extends \GUMP |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Function to create and return previously created instance |
||
| 21 | * Renamed from get_instance() to follow $f3 design pattern |
||
| 22 | * as calling $this->get_instance() will ignore this class |
||
| 23 | * and get a GUMP instance instead if this method did not exist |
||
| 24 | * |
||
| 25 | * @return Validator |
||
| 26 | */ |
||
| 27 | public static function instance() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Perform data filtering against the provided ruleset. |
||
| 38 | * |
||
| 39 | * @param mixed $input |
||
| 40 | * @param array optinal $ruleset ot use class ruleset |
||
| 41 | * @return bool|array |
||
| 42 | */ |
||
| 43 | public function filter(array $input, array $ruleset = []) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Perform data validation against the provided ruleset. |
||
| 50 | * |
||
| 51 | * @param array $input |
||
| 52 | * @param array optinal $ruleset ot use class ruleset |
||
| 53 | * @return bool|array |
||
| 54 | */ |
||
| 55 | public function validate(array $input, array $ruleset = []) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A custom filter named "lower". |
||
| 62 | * |
||
| 63 | * The callback function receives two arguments: |
||
| 64 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 65 | * |
||
| 66 | * @param $value |
||
| 67 | * @param array $param |
||
|
|
|||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function filter_lower(string $value, array $param = null): string |
||
| 75 | |||
| 76 | /** |
||
| 77 | * A custom filter named "upper". |
||
| 78 | * |
||
| 79 | * The callback function receives two arguments: |
||
| 80 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 81 | * |
||
| 82 | * @param $value |
||
| 83 | * @param array $param |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function filter_upper(string $value, array $param = null): string |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Strip whitespaces from the beginning of a string |
||
| 94 | * |
||
| 95 | * The callback function receives two arguments: |
||
| 96 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 97 | * |
||
| 98 | * @param $value |
||
| 99 | * @param array $param |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | * @link https://fatfreeframework.com/utf-unicode-string-manager#ltrim |
||
| 103 | */ |
||
| 104 | public function filter_ltrim(string $value, array $param = null): string |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Strip whitespaces from the end of a string |
||
| 111 | * |
||
| 112 | * The callback function receives two arguments: |
||
| 113 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 114 | * |
||
| 115 | * @param $value |
||
| 116 | * @param array $param |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | * @link https://fatfreeframework.com/utf-unicode-string-manager#rtrim |
||
| 120 | */ |
||
| 121 | public function filter_rtrim(string $value, array $param = null): string |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Strip whitespaces from the beginning and end of a string |
||
| 128 | * |
||
| 129 | * The callback function receives two arguments: |
||
| 130 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 131 | * |
||
| 132 | * @param $value |
||
| 133 | * @param array $param |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | * @link https://fatfreeframework.com/utf-unicode-string-manager#trim |
||
| 137 | */ |
||
| 138 | public function filter_trim(string $value, array $param = null): string |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Convert code points to Unicode symbols |
||
| 145 | * |
||
| 146 | * The callback function receives two arguments: |
||
| 147 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 148 | * |
||
| 149 | * @param $value |
||
| 150 | * @param array $param |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | * @link https://fatfreeframework.com/utf-unicode-string-manager#translate |
||
| 154 | */ |
||
| 155 | public function filter_translate(string $value, array $param = null): string |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Translate emoji tokens to Unicode font-supported symbols |
||
| 162 | * |
||
| 163 | * The callback function receives two arguments: |
||
| 164 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 165 | * |
||
| 166 | * @param $value |
||
| 167 | * @param array $param |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify |
||
| 171 | */ |
||
| 172 | public function filter_emojify(string $value, array $param = null): string |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Convert input to a slug |
||
| 179 | * |
||
| 180 | * The callback function receives two arguments: |
||
| 181 | * The value to filter, and any parameters used in the filter rule. It should returned the filtered value. |
||
| 182 | * |
||
| 183 | * @param $value |
||
| 184 | * @param array $param |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | * @link https://fatfreeframework.com/utf-unicode-string-manager#emojify |
||
| 188 | */ |
||
| 189 | public function filter_slug(string $value, array $param = null): string |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Check whether the IP Address is Public |
||
| 196 | * |
||
| 197 | * Usage: '<index>' => 'valid_ip_public' |
||
| 198 | * |
||
| 199 | * @param string $field |
||
| 200 | * @param array $input |
||
| 201 | * @param null $param |
||
| 202 | * |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function validate_valid_ip_public(string $field, array $input, $param = null) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Check whether the IP Address is NOT Public |
||
| 222 | * |
||
| 223 | * Usage: '<index>' => 'valid_ip_not_public' |
||
| 224 | * |
||
| 225 | * @param string $field |
||
| 226 | * @param array $input |
||
| 227 | * @param null $param |
||
| 228 | * |
||
| 229 | * @return mixed |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function validate_valid_ip_not_public(string $field, array $input, $param = null) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Check whether the IP Address is Reserved |
||
| 248 | * |
||
| 249 | * Usage: '<index>' => 'valid_ip_reserved' |
||
| 250 | * |
||
| 251 | * @param string $field |
||
| 252 | * @param array $input |
||
| 253 | * @param null $param |
||
| 254 | * |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function validate_valid_ip_reserved(string $field, array $input, $param = null) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Check whether the IP Address is Private or Reserved |
||
| 274 | * |
||
| 275 | * Usage: '<index>' => 'valid_ip_private' |
||
| 276 | * |
||
| 277 | * @param string $field |
||
| 278 | * @param array $input |
||
| 279 | * @param null $param |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function validate_valid_ip_private(string $field, array $input, $param = null) |
|
| 297 | |||
| 298 | |||
| 299 | } |
||
| 300 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.