Builder::createTable()   F
last analyzed

Complexity

Conditions 16
Paths 3073

Size

Total Lines 66
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
cc 16
eloc 36
c 5
b 2
f 1
nc 3073
nop 1
dl 0
loc 66
rs 1.4

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Framy Framework
4
 *
5
 * @copyright Copyright Framy
6
 * @Author  Marco Bier <[email protected]>
7
 */
8
9
namespace app\framework\Component\Database\Schema;
10
11
/**
12
 * Class Builder
13
 * Parse the sql query.
14
 *
15
 * @package app\framework\Component\Database\Schema
16
 */
17
class Builder
18
{
19
    /**
20
     * Builds based on thr Blueprint the sql query.
21
     *
22
     * @param Blueprint $table
23
     * @return string
24
     */
25
    public static function createTable(Blueprint $table): string
26
    {
27
        $query = "CREATE TABLE `".$table->getTable()."`";
28
29
        // go through columns
30
        $query .= " (";
31
        foreach ($table->getColumns() as $key => $Column)
32
        {
33
            $query .= "`".$Column->name."` ";
34
35
            $query .= $Column->type;
36
37
            if(isset($Column->length)) {
38
                $query .= "(".$Column->length;
39
            }
40
41
            if(isset($Column->scale) && isset($Column->length)) {
42
                $query .= ", ".$Column->scale.")";
43
            } else {
44
                if(isset($Column->length)) {
45
                    $query .= ")";
46
                }
47
            }
48
49
            if($Column->isUnsigned) {
50
                $query .= " UNSIGNED";
51
            }
52
53
            if($Column->notNull) {
54
                $query .= " NOT NULL";
55
            }
56
57
            if($Column->isAutoIncrement) {
58
                $query .= " AUTO_INCREMENT";
59
            }
60
61
            if($Column->primaryKey) {
62
                $query .= " PRIMARY KEY";
63
            }
64
65
            if($Column->foreignKey) {
66
                $query .= " FOREIGN KEY";
67
            }
68
69
            if($Column->unique) {
70
                $query .= " UNIQUE";
71
            }
72
73
            if($Column->default != null) {
74
                $default = $Column->default;
75
                if (is_string($default)) {
76
                    if (str($default)->startsWith("%"))
77
                        $default = str($default)->explode("%")->last();
78
                    else
79
                        $default = "'".$default."'";
80
                }
81
82
                $query .= " DEFAULT ".$default;
83
            }
84
85
            if(count($table->getColumns())-1 != $key)
86
                $query .= ", ";
87
        }
88
        $query .= ");";
89
90
        return $query;
91
    }
92
93
    /**
94
     * @param string $name
95
     * @return string
96
     */
97
    public static function dropTable(string $name): string
98
    {
99
        return "DROP TABLE `".$name."`;";
100
    }
101
}
102