Conditions | 25 |
Paths | 2880 |
Total Lines | 71 |
Code Lines | 44 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
118 | public function fieldspec(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null):string { |
||
119 | $type = strtoupper(trim($type)); |
||
120 | $defaultType = strtoupper($defaultType); |
||
121 | |||
122 | |||
123 | $field = [$this->quote(trim($name))]; |
||
124 | |||
125 | // @todo: whitelist types? |
||
126 | $nolengthtypes = ['DATE', 'TINYBLOB', 'TINYTEXT', 'BLOB', 'TEXT', 'MEDIUMBLOB', |
||
127 | 'MEDIUMTEXT', 'LONGBLOB', 'LONGTEXT', 'SERIAL', 'BOOLEAN', 'UUID']; |
||
128 | |||
129 | $field[] = (is_int($length) || (is_string($length) && count(explode(',', $length)) === 2)) && !in_array($type, $nolengthtypes, true) |
||
130 | ? $type.'('. $length . ')' |
||
131 | : $type; |
||
132 | |||
133 | if($attribute){ |
||
134 | $field[] = strtoupper($attribute); |
||
135 | } |
||
136 | |||
137 | $collationtypes = ['TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'VARCHAR', 'CHAR', 'ENUM', 'SET']; |
||
138 | if($collation && in_array($type, $collationtypes, true)){ |
||
139 | [$charset] = explode('_', $collation); |
||
140 | |||
141 | $field[] = 'CHARACTER SET '.$charset; |
||
142 | |||
143 | if($charset !== $collation){ |
||
144 | $field[] = 'COLLATE '.$collation; |
||
145 | } |
||
146 | |||
147 | } |
||
148 | |||
149 | if($isNull !== null){ |
||
150 | $field[] = $isNull ? 'NULL' : 'NOT NULL'; |
||
151 | } |
||
152 | |||
153 | if($defaultType === 'USER_DEFINED'){ |
||
154 | |||
155 | switch(true){ |
||
156 | case $type === 'TIMESTAMP' && intval($defaultValue) === 0: |
||
157 | $field[] = 'DEFAULT 0'; |
||
158 | break; |
||
159 | case $type === 'BIT': |
||
160 | $field[] = 'DEFAULT b\''.preg_replace('/[^01]/', '0', $defaultValue).'\''; |
||
161 | break; |
||
162 | case $type === 'BOOLEAN': |
||
163 | $field[] = 'DEFAULT '.(preg_match('/^1|T|TRUE|YES$/i', $defaultValue) ? 'TRUE' : 'FALSE'); |
||
164 | break; |
||
165 | case $type === 'BINARY' || $type === 'VARBINARY': |
||
166 | $field[] = 'DEFAULT 0x'.$defaultValue; |
||
167 | break; |
||
168 | case strtoupper($defaultValue) === 'NULL' && $isNull === true: |
||
169 | $field[] = 'DEFAULT NULL'; |
||
170 | break; |
||
171 | default: |
||
172 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
173 | } |
||
174 | |||
175 | } |
||
176 | else if($defaultType === 'CURRENT_TIMESTAMP'){ |
||
177 | $field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
||
178 | } |
||
179 | else if($defaultType === 'NULL' && $isNull === true){ |
||
180 | $field[] = 'DEFAULT NULL'; |
||
181 | } |
||
182 | |||
183 | |||
184 | if($extra){ |
||
185 | $field[] = $extra; |
||
186 | } |
||
187 | |||
188 | return implode(' ', $field); |
||
189 | } |
||
210 |