Conditions | 18 |
Paths | 528 |
Total Lines | 99 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
54 | function generateValidationRule($column, $js = false) |
||
55 | { |
||
56 | $jRules = []; |
||
57 | $assert = []; |
||
58 | $label = getPrettyColumnName($column); |
||
59 | $errorName = str_replace('?', '', $label); |
||
60 | $nullable = $column->is_nullable === 'NO' ? false : true; |
||
61 | $numeric = in_array($column->data_type, ['int', 'tinyint', 'decimal']); |
||
62 | if ( ! $nullable ) |
||
63 | { |
||
64 | $assert['not_empty'] = $numeric ? "numeric('{$errorName} must not be empty.')" : "notBlank('{$errorName} must not be empty.')"; |
||
65 | $jRules['required'] = true; |
||
66 | $jRules['data-bv-notempty-message'] = "{$errorName} must not be empty."; |
||
67 | } |
||
68 | |||
69 | switch ($column->data_type) |
||
70 | { |
||
71 | case 'text': |
||
72 | |||
73 | $jRules['maxlength'] = '10000'; |
||
74 | $jRules['data-bv-stringlength-message'] = "{$errorName} cannot exceed 10000 characters, (%s) submitted."; |
||
75 | $assert['utf-8'] = "utf8('{$errorName} has to be UTF-8 characters only, (%s) submitted.')"; |
||
76 | $assert['maxlength'] = "maxLength(10000, '{$errorName} cannot exceed 10000 characters, (%s) submitted.')"; |
||
77 | break; |
||
78 | |||
79 | case 'varchar': |
||
80 | |||
81 | $jRules['maxlength'] = $column->character_maximum_length; |
||
82 | $jRules['data-bv-stringlength-message'] = "{$errorName} cannot exceed {$column->character_maximum_length} characters."; |
||
83 | $assert['utf-8'] = "utf8('The value has to be UTF-8 characters only, (%s) submitted.')"; |
||
84 | $assert['maxlength'] = "maxLength($column->character_maximum_length, '{$errorName} cannot exceed {$column->character_maximum_length} characters.')"; |
||
85 | break; |
||
86 | |||
87 | case 'int': |
||
88 | |||
89 | $assert['integer'] = "integer('{$errorName} must be a integer, (%s) submitted.')"; |
||
90 | |||
91 | $jRules['data-bv-lessthan-value'] = ($column->numeric_precision + 1); |
||
92 | $jRules['data-bv-lessthan-message'] = "{$errorName} cannot exceed {$jRules['data-bv-lessthan-value']}."; |
||
93 | $jRules['data-bv-integer-message'] = "{$errorName} must be a integer, (%s) submitted."; |
||
94 | if ( $column->columnName === 'id' || preg_match('/_id$/', $column->columnName) ) |
||
95 | { |
||
96 | $jRules['data-bv-greaterthan-value'] = '0'; |
||
97 | $jRules['data-bv-greaterthan-message'] = "{$errorName} must be greater than 0."; |
||
98 | $assert['range'] = "range(1, 4294967295, '{$errorName} must be between 1 and 4294967295, (%s) submitted.')"; |
||
99 | } |
||
100 | break; |
||
101 | |||
102 | case 'decimal': |
||
103 | |||
104 | $jRules['data-bv-lessthan-value'] = ($column->numeric_precision + 1); |
||
105 | $jRules['data-bv-lessthan-message'] = "{$errorName} cannot exceed {$jRules['data-bv-lessthan-value']}."; |
||
106 | $jRules['data-bv-numeric-message'] = "{$errorName} must be a number."; |
||
107 | $jRules['data-bv-numeric-separator'] = "."; |
||
108 | $assert['range'] = "range(-4294967295, 4294967295, '{$errorName} must be between -4294967295 and 4294967295.')"; |
||
109 | |||
110 | break; |
||
111 | |||
112 | case 'tinyint': |
||
113 | |||
114 | $assert['boolean'] = "range(0, 1, '{$errorName} must be between 0 and 1.')"; |
||
115 | $jRules['pattern'] = '^(1|0)$|^$'; |
||
116 | $jRules['data-bv-between-message'] = "{$errorName} must selected."; |
||
117 | unset($jRules['required'], $jRules['data-bv-notempty-message']); |
||
118 | break; |
||
119 | |||
120 | case 'enum': |
||
121 | |||
122 | $values = str_replace("'", '', str_replace(',', '|', str_replace(['enum(', ')'], '', $column->column_type))); |
||
123 | $list = str_replace(',', ', ', str_replace("'", '', str_replace(['enum(', ')'], '', $column->column_type))); |
||
124 | $assertList = str_replace(['enum(', ')'], '', $column->column_type); |
||
125 | $jRules['pattern'] = '^(' . $values . ')$|^$'; |
||
126 | $jRules['data-bv-regexp-message'] = "{$errorName} must be one of the following: {$list}."; |
||
127 | $assert['list'] = "inArray([$assertList], '{$errorName} must be one of the following: {$list}.')"; |
||
128 | break; |
||
129 | |||
130 | case 'date': |
||
131 | |||
132 | $assert['date'] = "date('{$errorName} must be a valid date, (%s) submitted.')"; |
||
133 | break; |
||
134 | |||
135 | case 'datetime': |
||
136 | case 'timestamp': |
||
137 | |||
138 | $assert['date'] = "date('{$errorName} must be a valid timestamp, (%s) submitted.')"; |
||
139 | break; |
||
140 | |||
141 | default: |
||
142 | |||
143 | } |
||
144 | if ( preg_match('/email$/', $column->columnName) ) |
||
145 | { |
||
146 | $jRules['data-bv-emailaddress-message'] = "{$errorName} must be a valid email address."; |
||
147 | $assert['email'] = "email('{$errorName} must be a valid email address.')"; |
||
148 | } |
||
149 | $nullOr = $nullable ? "->nullOr()\n " : ''; |
||
150 | $assert = " Assert(\$value)\n {$nullOr}->" . implode("\n ->", $assert); |
||
151 | return $js ? $jRules : $assert; |
||
152 | } |
||
153 | |||
274 |