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 | 61 | public function __construct() { |
|
| 14 | |||
| 15 | /*** User Validation ***/ |
||
| 16 | /** |
||
| 17 | * @param string $username |
||
| 18 | * @return bool |
||
| 19 | */ |
||
| 20 | 4 | public function valid_username(string $username) : bool { |
|
| 21 | 4 | if(!($isValid = (bool) preg_match('/^[a-zA-Z0-9_-]{4,15}$/', $username))) { |
|
| 22 | 3 | $this->set_message('valid_username', 'Username is invalid format.'); |
|
| 23 | } |
||
| 24 | 4 | return $isValid; |
|
| 25 | } |
||
| 26 | /** |
||
| 27 | * @param string $password |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | 3 | public function valid_password(string $password) : bool { |
|
| 31 | 3 | if(!($isValid = $this->min_length($password, $this->CI->config->item('min_password_length', 'ion_auth')))) { |
|
| 32 | 1 | $this->set_message('valid_password', 'The password is too short!'); |
|
| 33 | } |
||
| 34 | 2 | elseif(!($isValid = $this->max_length($password, $this->CI->config->item('max_password_length', 'ion_auth')))) { |
|
| 35 | 1 | $this->set_message('valid_password', 'The password is too long!'); |
|
| 36 | } |
||
| 37 | 3 | return $isValid; |
|
| 38 | } |
||
| 39 | /** |
||
| 40 | * @param string $username |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | 2 | public function is_unique_username(string $username) : bool { |
|
| 44 | 2 | View Code Duplication | if(!($isValid = $this->is_unique($username, "{$this->user_tables['users']}.username"))) { |
| 45 | 1 | $this->set_message('is_unique_username', 'The username already exists in our database.'); |
|
| 46 | } |
||
| 47 | 2 | return $isValid; |
|
| 48 | } |
||
| 49 | /** |
||
| 50 | * @param string $email |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | 2 | public function is_unique_email(string $email) : bool { |
|
| 54 | 2 | View Code Duplication | if(!($isValid = $this->is_unique($email, "{$this->user_tables['users']}.email"))) { |
| 55 | 1 | $this->set_message('is_unique_email', 'The email already exists in our database.'); |
|
| 56 | } |
||
| 57 | 2 | return $isValid; |
|
| 58 | } |
||
| 59 | |||
| 60 | 3 | public function is_valid_json(string $json_string) : bool { |
|
| 67 | |||
| 68 | 5 | public function is_valid_tag_string(string $tag_string) : bool { |
|
| 69 | 5 | return (bool) preg_match('/^[a-z0-9\\-_,:]{0,255}$/', $tag_string) && (count(preg_grep('/^mal:(?:[0-9]+|none)$/', explode(',', $tag_string))) <= 1); |
|
| 70 | } |
||
| 71 | |||
| 72 | 2 | public function is_valid_category(string $category) : bool { |
|
| 75 | |||
| 76 | 2 | public function not_contains(string $haystack, string $needle) { |
|
| 79 | |||
| 80 | 2 | public function not_equals(string $originalString, string $matchingString) { |
|
| 83 | |||
| 84 | 2 | public function is_valid_option_value(string $value, string $option) : bool { |
|
| 90 | |||
| 91 | /** MISC FUNCTIONS **/ |
||
| 92 | 3 | /** |
|
| 93 | 3 | * @param string $ruleName |
|
| 94 | 3 | * @return bool |
|
| 95 | 2 | */ |
|
| 96 | public function isRuleValid(string $ruleName) : bool { |
||
| 97 | 3 | $isValid = FALSE; |
|
| 98 | if(is_string($ruleName) && $this->has_rule($ruleName)){ |
||
| 99 | $isValid = !in_array($ruleName, array_keys($this->error_array()), TRUE); |
||
| 100 | } |
||
| 101 | return $isValid; |
||
| 102 | } |
||
| 103 | } |
||
| 104 |