Schema   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 0 Features 2
Metric Value
wmc 21
c 9
b 0
f 2
lcom 1
cbo 3
dl 0
loc 192
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A increments() 0 4 1
A strings() 0 4 1
A text() 0 4 1
A integer() 0 4 1
A primaryKey() 0 4 1
A unique() 0 4 1
A field() 0 10 2
A buildQuery() 0 23 2
A sanitizeQuery() 0 6 1
A createTable() 0 15 3
A foreignKey() 0 6 1
B dateTime() 0 25 6
1
<?php
2
/**
3
 * SugarORM\Schema manages the creation of database table.
4
 *
5
 * @package Ibonly\SugarORM\Schema
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use PDOException;
13
use Ibonly\PotatoORM\DatabaseQuery;
14
use Ibonly\PotatoORM\SchemaInterface;
15
16
class Schema extends DatabaseQuery implements SchemaInterface
17
{
18
    //Inject the inflector trait
19
    use Inflector;
20
21
    protected $fieldDescription = [];
22
23
    /**
24
     * field(arguments) contains the sql field statement
25
     *
26
     * @return array
27
     */
28
    public function field($type, $fieldName, $length=NULL)
29
    {
30
        if($length === null){
31
             $this->fieldDescription[] = $type ." ".$fieldName;
32
        }else
33
        {
34
         $this->fieldDescription[] = $type ." ".$fieldName." ".$length;
35
        }
36
37
    }
38
39
    /**
40
     * buildQuery(argument): Builds the CREATE query
41
     *
42
     * @return string
43
     */
44
    public function buildQuery($tablename)
45
    {
46
        $pluralTableName = self::pluralize($tablename);
47
        $query = "CREATE TABLE IF NOT EXISTS {$pluralTableName} (".PHP_EOL;
48
49
        $callback = function($fieldName) use (&$query)
50
        {
51
            $fields = explode(" ", $fieldName);
52
         
53
            $constrain = $fields[0];
54
            if(count($fields) == 2)
55
            {
56
                $query .= $this->$constrain($fields[1], 20) .", ".PHP_EOL;
57
            }else
58
            {
59
                    $query .= $this->$constrain($fields[1], $fields[2]) .", ".PHP_EOL;
60
            }
61
        };
62
        array_walk($this->fieldDescription, $callback);
63
        $query .= ');)';
64
65
        return $query;
66
    }
67
68
    /**
69
     * SanitizeQuery(argument) Removes the unwanted character in the build
70
     * and completes the statement
71
     *
72
     * @return string
73
     */
74
    public function sanitizeQuery($query)
75
    {
76
        $q = substr_replace($this->buildQuery($query), "", -6);
77
        $q .= ");";
78
        return $q;
79
    }
80
81
    /**
82
     * createTable(argument) Execute the CREATE query
83
     *
84
     * @return bool
85
     */
86
    public function createTable($tablename, $connection = NULL)
87
    {
88
        $connection = DatabaseQuery::connect();
89
        try
90
        {
91
            $sqlQuery = self::sanitizeQuery($tablename);
92
            $query = $connection->prepare($sqlQuery);
93
            if($query->execute())
94
            {
95
                return true;
96
            }
97
        }catch(PDOException $e){
98
            return $e->getMessage();
99
        }
100
    }
101
102
    /**
103
     * increments(argument)
104
     *
105
     * @return string
106
     */
107
    public function increments($value)
108
    {
109
        return $value." int(11) NOT NULL AUTO_INCREMENT";
110
    }
111
112
    /**
113
     * strings(arguments)
114
     *
115
     * @return string
116
     */
117
    public function strings($value, $length)
118
    {
119
        return $value ." varchar (".$length.") NOT NULL";
120
    }
121
122
    /**
123
     * text(argument)
124
     *
125
     * @return string
126
     */
127
    public function text($value)
128
    {
129
        return $value." text NOT NULL";
130
    }
131
132
    /**
133
     * increments(argument)
134
     *
135
     *
136
     * @return string
137
     */
138
    public function integer($value, $length)
139
    {
140
        return $value." int(".$length.") NOT NULL";
141
    }
142
143
    /**
144
     * increments(argument)
145
     *
146
     * @return string
147
     */
148
    public function primaryKey($value)
149
    {
150
        return "PRIMARY KEY ({$value})";
151
    }
152
153
    /**
154
     * unique(argument)
155
     *
156
     * @return string
157
     */
158
    public function unique($value)
159
    {
160
        return "UNIQUE KEY {$value} ({$value})";
161
    }
162
163
    /**
164
     * foreignKey(argument)
165
     *
166
     * @return string
167
     */
168
    public function foreignKey($value, $length)
169
    {
170
        $r = explode("-", $length);
171
        
172
        return "FOREIGN KEY ({$value}) REFERENCES ".$r[0]."(".$r[1].")";
173
    }
174
175
    /**
176
     * dateTime description]
177
     *
178
     * @param  [type] $value [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
179
     * @param  [type] $type  [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
180
     * @return [type]        [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
181
     */
182
    public function dateTime($value, $type = NULL)
183
    {
184
        $apend = "";
0 ignored issues
show
Unused Code introduced by
$apend is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
        switch ($type) {
186
            case 'time':
187
                    $apend = 'time';
188
                break;
189
            case 'timestamp':
190
                    $apend = 'timestamp';
191
                break;
192
            case 'date':
193
                    $apend = 'date';
194
                break;
195
            case 'datetime':
196
                    $apend = 'datetime';
197
                break;
198
            case 'year':
199
                    $apend = 'year(4)';
200
                break;
201
            default:
202
                    $apend = 'timestamp';
203
                break;
204
        }
205
        return $value . " " . $apend . " NOT NULL";
206
    }
207
}