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 | 50 | public function __construct() { |
|
7 | 50 | parent::__construct(); |
|
8 | 50 | log_message('debug', 'MY_Form_validation Class Initialized'); |
|
|
|||
9 | |||
10 | 50 | $this->CI->config->load('ion_auth', TRUE); |
|
11 | 50 | $this->user_tables = $this->CI->config->item('tables', 'ion_auth'); |
|
12 | |||
13 | 50 | $this->set_error_delimiters( |
|
14 | 50 | $this->CI->config->item('error_start_delimiter', 'ion_auth'), |
|
15 | 50 | $this->CI->config->item('error_end_delimiter', 'ion_auth') |
|
16 | ); |
||
17 | 50 | } |
|
18 | |||
19 | 6 | public function run($group = '') : bool { |
|
20 | 6 | if(!$this->CI->security->verified) { |
|
21 | $this->_error_array['csrf_token'] = $this->_build_error_msg('Session expired, please resubmit.'); |
||
22 | return FALSE; |
||
23 | } |
||
24 | |||
25 | 6 | return parent::run($group); |
|
26 | } |
||
27 | |||
28 | /*** User Validation ***/ |
||
29 | /** |
||
30 | * @param string $username |
||
31 | * @return bool |
||
32 | */ |
||
33 | 4 | public function valid_username(string $username) : bool { |
|
34 | 4 | if(!($isValid = (bool) preg_match('/^[a-zA-Z0-9_-]{4,15}$/', $username))) { |
|
35 | 3 | $this->set_message('valid_username', 'Username is invalid format.'); |
|
36 | } |
||
37 | 4 | return $isValid; |
|
38 | } |
||
39 | /** |
||
40 | * @param string $password |
||
41 | * @return bool |
||
42 | */ |
||
43 | 3 | public function valid_password(string $password) : bool { |
|
44 | 3 | 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 | 1 | $this->set_message('valid_password', 'The password is too long!'); |
|
49 | } |
||
50 | 3 | return $isValid; |
|
51 | } |
||
52 | /** |
||
53 | * @param string $username |
||
54 | * @return bool |
||
55 | */ |
||
56 | 2 | 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 | 1 | $this->set_message('is_unique_username', 'The username already exists in our database.'); |
|
59 | } |
||
60 | 2 | return $isValid; |
|
61 | } |
||
62 | /** |
||
63 | * @param string $email |
||
64 | * @return bool |
||
65 | */ |
||
66 | 2 | public function is_unique_email(string $email) : bool { |
|
67 | 2 | View Code Duplication | if(!($isValid = $this->is_unique($email, "{$this->user_tables['users']}.email"))) { |
68 | 1 | $this->set_message('is_unique_email', 'The email already exists in our database.'); |
|
69 | } |
||
70 | 2 | return $isValid; |
|
71 | } |
||
72 | |||
73 | 3 | public function is_valid_json(string $json_string) : bool { |
|
74 | 3 | $isValid = FALSE; |
|
75 | 3 | if(json_decode($json_string) && json_last_error() === JSON_ERROR_NONE) { |
|
76 | 1 | $isValid = TRUE; |
|
77 | } |
||
78 | 3 | return $isValid; |
|
79 | } |
||
80 | |||
81 | 5 | public function is_valid_tag_string(string $tag_string) : bool { |
|
82 | 5 | return (bool) preg_match('/^[a-z0-9\\-_,:]{0,255}$/', $tag_string) && (count(preg_grep('/^mal:(?:[0-9]+|none)$/', explode(',', $tag_string))) <= 1); |
|
83 | } |
||
84 | |||
85 | 2 | public function is_valid_category(string $category) : bool { |
|
88 | |||
89 | 2 | public function not_contains(string $haystack, string $needle) : bool { |
|
90 | 2 | return strpos($haystack, $needle) === FALSE; |
|
91 | } |
||
92 | |||
93 | public function not_equals(string $originalString, string $matchingString) : bool { |
||
94 | return $originalString !== $matchingString; |
||
95 | } |
||
96 | |||
97 | 2 | 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 | 3 | public function isRuleValid(string $ruleName) : bool { |
|
110 | 3 | $isValid = FALSE; |
|
111 | 3 | if(is_string($ruleName) && $this->has_rule($ruleName)){ |
|
112 | 2 | $isValid = !array_key_exists($ruleName, $this->error_array()); |
|
113 | } |
||
114 | 3 | return $isValid; |
|
115 | } |
||
116 | } |
||
117 |