Complex classes like Validator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Validator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Validator |
||
| 20 | { |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | private $errors = []; |
||
| 24 | /** @var array */ |
||
| 25 | private $namings = []; |
||
| 26 | /** @var array */ |
||
| 27 | private $customErrorsWithInputName = []; |
||
| 28 | /** @var array */ |
||
| 29 | private $customErrors = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor is not allowed because Validoo uses its own |
||
| 33 | * static method to instantiate the validation |
||
| 34 | * @param $errors |
||
| 35 | * @param $namings |
||
| 36 | */ |
||
| 37 | 48 | private function __construct($errors, $namings) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @param $inputs |
||
| 45 | * @param array $rules |
||
| 46 | * @param array|null $naming |
||
| 47 | * @return Validator |
||
| 48 | * @throws ValidooException |
||
| 49 | */ |
||
| 50 | 48 | public static function validate($inputs, array $rules, array $naming = null): self |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param $closure |
||
| 94 | * @param $params |
||
| 95 | * @param $rule |
||
| 96 | * @return mixed |
||
| 97 | * @throws ValidooException |
||
| 98 | */ |
||
| 99 | 48 | private static function doValidation($closure, $params, $rule) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the parameter names of a rule |
||
| 120 | * @param $rule |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | 48 | private static function getParams($rule) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Handle parameter with input name |
||
| 139 | * eg: equals(:name) |
||
| 140 | * @param mixed $params |
||
| 141 | * @param $inputs |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | 48 | private static function getParamValues($params, $inputs) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param null $input |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 9 | protected static function required($input = null): bool |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param $input |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | protected static function numeric($input): bool |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $input |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 4 | protected static function email($input): bool |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param $input |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 2 | protected static function isdir($input): bool |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param $input |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | protected static function isarray($input): bool |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param $input |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 11 | protected static function integer($input): bool |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param $input |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | protected static function float($input): bool |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $input |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | 4 | protected static function alpha($input): bool |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param $input |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | protected static function alpha_numeric($input): bool |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param $input |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 4 | protected static function ip($input): bool |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param $input |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | 9 | protected static function url($input): bool |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param $input |
||
| 260 | * @param $length |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected static function max_length($input, $length): bool |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param $input |
||
| 270 | * @param $length |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | protected static function min_length($input, $length): bool |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $input |
||
| 280 | * @param $length |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | protected static function exact_length($input, $length): bool |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $input |
||
| 290 | * @param $param |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 4 | protected static function equals($input, $param): bool |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param $input |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | protected static function is_filename($input): bool |
||
| 306 | |||
| 307 | protected static function exists_file($input): bool |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | 46 | public function isSuccess() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param array $errors_array |
||
| 322 | */ |
||
| 323 | 1 | public function customErrors(array $errors_array) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string|null $lang |
||
| 338 | * @return array |
||
| 339 | * @throws ValidooException |
||
| 340 | */ |
||
| 341 | 3 | public function getErrors(string $lang = null): array |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | 3 | protected function getDefaultLang(): string |
|
| 394 | |||
| 395 | /* |
||
| 396 | * TODO: need improvements for tel and urn urls. |
||
| 397 | * check out url.test.php for the test result |
||
| 398 | * urn syntax: http://www.faqs.org/rfcs/rfc2141.html |
||
| 399 | * |
||
| 400 | */ |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string|null $lang |
||
| 404 | * @return array|mixed |
||
| 405 | */ |
||
| 406 | 3 | protected function getDefaultErrorTexts(string $lang = null) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $input_name |
||
| 424 | * @return mixed|string |
||
| 425 | */ |
||
| 426 | 3 | protected function handleNaming(string $input_name) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param array $params |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | 3 | protected function handleParameterNaming(array $params) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @param string $input_name |
||
| 458 | * @param string|null $rule_name |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function has(string $input_name, string $rule_name = null): bool |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | final public function getResults(): array |
||
| 477 | } |
||
| 478 |