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 declare(strict_types=1); defined('BASEPATH') or exit('No direct script access allowed'); |
||
| 3 | class MY_Form_validation extends CI_Form_validation { |
||
| 4 | private $user_tables; |
||
| 5 | |||
| 6 | 64 | public function __construct() { |
|
| 18 | |||
| 19 | public function run($group = '') : bool { |
||
| 27 | |||
| 28 | /*** User Validation ***/ |
||
| 29 | /** |
||
| 30 | 3 | * @param string $username |
|
| 31 | 3 | * @return bool |
|
| 32 | 1 | */ |
|
| 33 | public function valid_username(string $username) : bool { |
||
| 34 | 2 | if(!($isValid = (bool) preg_match('/^[a-zA-Z0-9_-]{4,15}$/', $username))) { |
|
| 35 | 1 | $this->set_message('valid_username', 'Username is invalid format.'); |
|
| 36 | } |
||
| 37 | 3 | return $isValid; |
|
| 38 | } |
||
| 39 | /** |
||
| 40 | * @param string $password |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | 2 | public function valid_password(string $password) : bool { |
|
| 44 | 2 | if(!($isValid = $this->min_length($password, $this->CI->config->item('min_password_length', 'ion_auth')))) { |
|
| 45 | 1 | $this->set_message('valid_password', 'The password is too short!'); |
|
| 46 | } |
||
| 47 | 2 | elseif(!($isValid = $this->max_length($password, $this->CI->config->item('max_password_length', 'ion_auth')))) { |
|
| 48 | $this->set_message('valid_password', 'The password is too long!'); |
||
| 49 | } |
||
| 50 | return $isValid; |
||
| 51 | } |
||
| 52 | /** |
||
| 53 | 2 | * @param string $username |
|
| 54 | 2 | * @return bool |
|
| 55 | 1 | */ |
|
| 56 | public function is_unique_username(string $username) : bool { |
||
| 57 | 2 | View Code Duplication | if(!($isValid = $this->is_unique($username, "{$this->user_tables['users']}.username"))) { |
| 58 | $this->set_message('is_unique_username', 'The username already exists in our database.'); |
||
| 59 | } |
||
| 60 | 3 | return $isValid; |
|
| 61 | 3 | } |
|
| 62 | 3 | /** |
|
| 63 | 1 | * @param string $email |
|
| 64 | * @return bool |
||
| 65 | 3 | */ |
|
| 66 | public function is_unique_email(string $email) : bool { |
||
| 67 | View Code Duplication | if(!($isValid = $this->is_unique($email, "{$this->user_tables['users']}.email"))) { |
|
| 68 | 5 | $this->set_message('is_unique_email', 'The email already exists in our database.'); |
|
| 69 | 5 | } |
|
| 70 | return $isValid; |
||
| 71 | } |
||
| 72 | 2 | ||
| 73 | 2 | public function is_valid_json(string $json_string) : bool { |
|
| 80 | |||
| 81 | public function is_valid_tag_string(string $tag_string) : bool { |
||
| 84 | 2 | ||
| 85 | 2 | public function is_valid_category(string $category) : bool { |
|
| 88 | 2 | ||
| 89 | public function not_contains(string $haystack, string $needle) : bool { |
||
| 92 | |||
| 93 | public function not_equals(string $originalString, string $matchingString) : bool { |
||
| 96 | 3 | ||
| 97 | 3 | public function is_valid_option_value(string $value, string $option) : bool { |
|
| 103 | |||
| 104 | /** MISC FUNCTIONS **/ |
||
| 105 | /** |
||
| 106 | * @param string $ruleName |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function isRuleValid(string $ruleName) : bool { |
||
| 110 | $isValid = FALSE; |
||
| 111 | if(is_string($ruleName) && $this->has_rule($ruleName)){ |
||
| 112 | $isValid = !array_key_exists($ruleName, $this->error_array()); |
||
| 113 | } |
||
| 114 | return $isValid; |
||
| 115 | } |
||
| 116 | } |
||
| 117 |