Conditions | 25 |
Paths | > 20000 |
Total Lines | 158 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
146 | $physicalMaxLength = 2 ** $bits; |
||
147 | |||
148 | /** |
||
149 | * $minDataLength y $maxDataLength contendrán el mínimo y máximo valor que puede contener el campo. |
||
150 | */ |
||
151 | $minDataLength = $unsigned ? 0 : -$physicalMaxLength / 2; |
||
152 | $maxDataLength = ($unsigned ? $physicalMaxLength : $physicalMaxLength / 2) - 1; |
||
153 | |||
154 | /** |
||
155 | * De momento, se asignan los límites máximos por el tipo de dato. |
||
156 | * En $min y $max, iremos arrastrando los límites conforme se vayan comprobando. |
||
157 | * $min nunca podrá ser menor que $minDataLength. |
||
158 | * $max nunca podrá ser mayor que $maxDataLength. |
||
159 | */ |
||
160 | $min = $minDataLength; |
||
161 | $max = $maxDataLength; |
||
162 | |||
163 | return [ |
||
164 | 'dbtype' => $type, |
||
165 | 'min' => $min, |
||
166 | 'max' => $max, |
||
167 | 'size' => $size, |
||
168 | 'unsigned' => $unsigned, |
||
169 | ]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Retorna un array asociativo con la información de cada columna de la tabla. |
||
174 | * El resultado será dependiente del motor de base de datos. |
||
175 | * |
||
176 | * @author Rafael San José Tovar <[email protected]> |
||
177 | * @version 2023.0108 |
||
178 | * |
||
179 | * @param string $tableName |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public static function getColumns(string $tableName): array |
||
184 | { |
||
185 | $query = 'SHOW COLUMNS FROM ' . self::quoteTableName($tableName) . ';'; |
||
186 | $rows = DB::select($query); |
||
187 | $result = []; |
||
188 | foreach ($rows as $row) { |
||
189 | $result[$row['Field']] = $row; |
||
190 | } |
||
191 | return $result; |
||
192 | } |
||
193 | |||
194 | public static function yamlFieldIntegerToDb(array $data): string |
||
195 | { |
||
196 | $type = $data['dbtype']; |
||
197 | // TODO: Aunque lo que está comentado va, igual no hace falta si al comparar |
||
198 | // ignoramos el tamaño a mostrar para los integer |
||
199 | |||
200 | /* |
||
201 | $unsigned = $data['unsigned'] ?? false; |
||
202 | switch ($type) { |
||
203 | case 'tinyint': |
||
204 | return $type . ($unsigned ? '(3) unsigned' : '(4)'); |
||
205 | case 'smallint': |
||
206 | break; |
||
207 | case 'mediumint': |
||
208 | break; |
||
209 | case 'int': |
||
210 | return $type . ($unsigned ? '(10) unsigned' : '(11)'); |
||
211 | case 'bigint': |
||
212 | $type .= '(20)'; |
||
213 | } |
||
214 | */ |
||
215 | return $type . ($unsigned ? ' unsigned' : ''); |
||
|
|||
216 | } |
||
217 | |||
218 | public static function yamlFieldToDb(array $data): array |
||
219 | { |
||
220 | $unsigned = $data['unsigned'] ?? false; |
||
221 | |||
222 | $result = []; |
||
223 | $result['Field'] = $data['name']; |
||
224 | |||
225 | $type = $data['dbtype']; |
||
226 | switch ($data['generictype']) { |
||
227 | case Schema::TYPE_INTEGER: |
||
228 | $type = self::yamlFieldIntegerToDb($data); |
||
229 | break; |
||
230 | case Schema::TYPE_FLOAT: |
||
231 | case Schema::TYPE_DECIMAL: |
||
232 | case Schema::TYPE_STRING: |
||
233 | case Schema::TYPE_TEXT: |
||
234 | case Schema::TYPE_DATE: |
||
235 | case Schema::TYPE_TIME: |
||
236 | case Schema::TYPE_DATETIME: |
||
237 | case Schema::TYPE_BOOLEAN: |
||
238 | $type = 'tinyint(1)'; |
||
239 | break; |
||
240 | } |
||
241 | $result['Type'] = $type; |
||
242 | $result['Null'] = !isset($data['nullable']) || $data['nullable'] ? 'YES' : 'NO'; |
||
243 | $result['Key'] = $data['type'] === 'autoincrement' ? 'PRI' : ''; |
||
244 | $result['Default'] = $data['default'] ?? null; |
||
245 | $result['Extra'] = $data['type'] === 'autoincrement' ? 'auto_increment' : ''; |
||
246 | return $result; |
||
247 | } |
||
248 | |||
249 | public static function getSqlField(array $column): string |
||
250 | { |
||
251 | $field = $column['Field']; |
||
252 | $type = $column['Type']; |
||
253 | $null = $column['Null']; |
||
254 | $key = $column['Key']; |
||
255 | $default = $column['Default']; |
||
256 | $extra = $column['Extra']; |
||
257 | |||
258 | $sql = self::quoteTableName($field) . ' ' . $type; |
||
259 | $nulo = ($null === 'YES'); |
||
260 | if ($extra === 'auto_increment') { |
||
261 | $nulo = false; |
||
262 | $sql .= ' PRIMARY KEY AUTO_INCREMENT'; |
||
263 | } |
||
264 | |||
265 | $sql .= ($nulo ? '' : ' NOT') . ' NULL'; |
||
266 | |||
267 | $defecto = ''; |
||
268 | if (isset($default)) { |
||
269 | if ($default === 'CURRENT_TIMESTAMP') { |
||
270 | $defecto = $default; |
||
271 | } elseif (is_bool($default)) { |
||
272 | $defecto = $default ? 1 : 0; |
||
273 | } else { |
||
274 | $defecto = "'$defecto'"; |
||
275 | } |
||
276 | } else { |
||
277 | if ($nulo) { |
||
278 | $defecto = 'NULL'; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | if (!empty($defecto)) { |
||
283 | $sql .= ' DEFAULT ' . $defecto; |
||
284 | } |
||
285 | return $sql; |
||
286 | } |
||
287 | |||
288 | public static function _dbFieldToSchema(array $data): array |
||
289 | { |
||
290 | return $data; |
||
291 | } |
||
292 | |||
293 | public static function _dbFieldToYaml(array $data): array |
||
294 | { |
||
295 | return $data; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Recibiendo un array con los datos de un campo tal y como lo retorna la base de |
||
300 | * datos, devuelve la información normalizada para ser utilizada por Schema. |
||
301 | * |
||
302 | * @author Rafael San José Tovar <[email protected]> |
||
303 | * @version 2023.0108 |
||
304 | * |
||
469 |