Completed
Push — master ( 98a82f...7bd861 )
by Márcio Lucas R.
11s
created

TableMySql::alter()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 156
Code Lines 98

Duplication

Lines 55
Ratio 35.26 %

Importance

Changes 0
Metric Value
dl 55
loc 156
rs 2
c 0
b 0
f 0
cc 34
eloc 98
nc 4152000
nop 1

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
/**
4
 * Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT
5
 */
6
namespace Phiber\ORM\Persistence;
7
8
use Phiber\ORM\factories\TableFactory;
9
use Phiber\Util\Annotations;
10
use Phiber\Util\FuncoesReflections;
11
use Phiber\Util\FuncoesString;
12
use Phiber\Util\JsonReader;
13
14
/**
15
 * Classe responsável por criar as tabelas do banco
16
 * 
17
 * @package bin
18
 */
19
class TableMySql extends TableFactory
20
{
21
    /**
22
     * Sincroniza o banco com o código em tempo de instanciação.
23
     * 
24
     * @param Object $obj
25
     * @return mixed|void
26
     */
27
    static function sync($obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29
        if (self::exists($obj)) {
30
31
            self::drop($obj);
32
            self::alter($obj);
33
34
        } else {
35
            self::create($obj);
36
        }
37
    }
38
39
    /**
40
     * Verifica se a tabela existe, se caso não existir, a função retornará false.
41
     * 
42
     * @param Object $obj
43
     * @return bool|string
44
     */
45
    static function exists($obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
    {
47
        $tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj));
48
        $schema = JsonReader::read(BASE_DIR . '/phiber_config.json')->phiber->link->database_name;
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
        $sql    = "SELECT * FROM information_schema.tables WHERE table_schema = ? AND table_name = ?";
50
        
51
        if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->execute_querys == 1 ? true : false) {
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
            $pdo = self::getConnection()->prepare($sql);
53
            $pdo->bindValue(1, $schema);
54
            $pdo->bindValue(2, $tabela);
55
            $pdo->execute();
56
            
57
            if ($pdo->rowCount() > 0) {
58
                return true;
59
            }
60
                
61
            return false;
62
63
        } else {
64
            return $sql;
65
        }
66
    }
67
68
    /**
69
     * Deleta a tabela do banco de dados.
70
     * 
71
     * @param  Object $obj
72
     * @return bool|string
73
     */
74
    static function drop($obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
    {
76
        $tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj));
77
        $atributosObjeto = FuncoesReflections::pegaAtributosDoObjeto($obj);
78
        $columnsTabela = self::columns($tabela);
79
        $arrayCamposTabela = [];
80 View Code Duplication
        for ($i = 0; $i < count($columnsTabela); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            array_push($arrayCamposTabela, $columnsTabela[$i]['Field']);
82
        }
83
        
84
        $arrayDiff = array_diff($arrayCamposTabela, $atributosObjeto);
85
        $arrayDiff = array_values($arrayDiff);
86
        $sqlDrop = "ALTER TABLE $tabela \n";
87
        for ($j = 0; $j < count($arrayDiff); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
88 View Code Duplication
            if ($j != count($arrayDiff) - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
                $sqlDrop .= "DROP " . $arrayDiff[$j] . ", ";
90
            } else {
91
                $sqlDrop .= "DROP " . $arrayDiff[$j] . ";";
92
            }
93
        }
94
        
95
        $contentFile = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false;
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
96
        if ($contentFile) {
97
            $pdo = self::getConnection()->prepare($sqlDrop);
98
            if ($pdo->execute()) {
99
                return true;
100
            };
101
102
        } else {
103
            return $sqlDrop;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Mostra as colunas daquela tabela.
111
     * 
112
     * @param string $table
113
     * @return array|bool
114
     */
115
    static function columns($table)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
    {
117
        $sql = "show columns from " . strtolower($table);
118
119
        $contentFile = JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->execute_querys == 1 ? true : false;
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
        if ($contentFile) {
121
            $pdo = self::getConnection()->prepare($sql);
122
            if ($pdo->execute()) {
123
                return $pdo->fetchAll(\PDO::FETCH_ASSOC);
124
            } else {
125
                return false;
126
            }
127
        } else {
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * Altera a tabela do banco
134
     * 
135
     * @param Object $obj
136
     * @return bool|string
137
     */
138
    static function alter($obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
    {
140
        $tabela            = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj));
141
        $atributosTabela   = FuncoesReflections::pegaAtributosDoObjeto($obj);
142
        $annotationsTabela = Annotations::getAnnotation($obj);
143
        $arrFormatado      = [];
144
        $arrFinal          = [];
145
        $stringAlterTable  = "";
146
        for ($i = 0; $i < count($atributosTabela); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
147 View Code Duplication
            for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
                $arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]);
149
                for ($k = 0; $k < count($arrAtual) - 1; $k++) {
150
                    $arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1];
151
                }
152
            }
153
154
            $arrFinal[$i] = $arrFormatado;
155
        }
156
157
        $stringAlterTable .= "ALTER TABLE $tabela \n";
158
        $primKey = false;
159
        $columnPrimaryKey = "";
160
161
        $columnsTabela = self::columns($tabela);
162
163
        $arrayCamposTabela = [];
164 View Code Duplication
        for ($i = 0; $i < count($columnsTabela); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
            array_push($arrayCamposTabela, $columnsTabela[$i]['Field']);
166
        }
167
168
        $arrayDiff = array_diff($atributosTabela, $arrayCamposTabela);
169
        $arrayDiff = array_values($arrayDiff);
170
171
        $stringSql = $stringAlterTable;
172
        for ($j = 0; $j < count($arrayDiff); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
173 View Code Duplication
            if ($j != count($arrayDiff) - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
                $stringSql .= "ADD " . $arrayDiff[$j];
175
            } else {
176
                $stringSql .= "ADD " . $arrayDiff[$j];
177
            }
178 View Code Duplication
            if (array_key_exists('type', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
180
                $stringSql .= " " . $arrFormatado['type'] . "";
181
182
            } else {
183
                $stringSql .= "";
184
            }
185
186 View Code Duplication
            if (array_key_exists('size', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
188
                $stringSql .= "(" . $arrFormatado['size'] . ") ";
189
190
            } else {
191
                $stringSql .= "";
192
            }
193
194
            /**
195
             * @todo NOT NULL AQUI
196
            */
197
198 View Code Duplication
            if (array_key_exists('notNull', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
                if ($arrFormatado['notNull'] === "true") {
200
                    $stringSql .= " NOT NULL ";
201
                } else {
202
                    $stringSql .= "";
203
                };
204
            } else {
205
                $stringSql .= "";
206
            }
207
208 View Code Duplication
            if (array_key_exists('primaryKey', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
                if ($arrFormatado['primaryKey'] === "true") {
210
                    $stringSql .= " PRIMARY KEY ";
211
                } else {
212
                    $stringSql .= "";
213
                };
214
            } else {
215
                $stringSql .= "";
216
            }
217
218 View Code Duplication
            if (array_key_exists('autoIncrement', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
                if ($arrFormatado['autoIncrement'] === "true") {
220
                    $stringSql .= " AUTO_INCREMENT ";
221
                } else {
222
                    $stringSql .= "";
223
                };
224
            } else {
225
                $stringSql .= "";
226
            }
227
228
            $fileContent = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false;
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
229
            if ($fileContent) {
230
                $pdo = self::getConnection()->prepare($stringSql);
231
                $pdo->execute();
232
            }
233
        }
234
235
        $columnsTabela = self::columns($tabela);
236
        for ($i = 0; $i < count($arrFinal); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
237
            if ($columnsTabela[$i]['Field'] != $atributosTabela[$i]) {
238
                $stringAlterTable .= "CHANGE `" . $columnsTabela[$i]['Field'] . "` `$atributosTabela[$i]` ";
239
                $stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")\n";
240
241
                if ($arrFinal[$i])
242
243
                    if ($i != count($arrFinal) - 1) {
244
                        $stringAlterTable .= ", \n";
245
                    }
246
            } else {
247
                $stringAlterTable .= "CHANGE  `$atributosTabela[$i]` `$atributosTabela[$i]` ";
248
249
                $stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")";
250
251
                $respIfNotNull = $columnsTabela[$i]['Null'] == 'NO' ? 'false' : 'true';
252
253
                if ($arrFinal[$i]['notNull'] == $respIfNotNull) {
254
                    $stringAlterTable .= " NOT NULL ";
255
                } else {
256
                    $stringAlterTable .= " NULL ";
257
                }
258
259
                if (array_key_exists('default', $arrFinal[$i]) && $arrFinal[$i]['default'] != "none") {
260
                    $stringAlterTable .= "DEFAULT '" . $arrFinal[$i]['default'] . "'";
261
                }
262
263
                if (array_key_exists('autoIncrement', $arrFinal[$i]) && $arrFinal[$i]['autoIncrement'] != "false") {
264
                    $stringAlterTable .= " AUTO_INCREMENT ";
265
                }
266
267
                $stringAlterTable .= ", \n";
268
269
                if ($arrFinal[$i]['primaryKey'] == "true" and $primKey != true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
270
                    $primKey = true;
271
                    $columnPrimaryKey = $atributosTabela[$i];
272
                }
273
                if ($i == count($arrFinal) - 1) {
274
                    if ($primKey) {
275
                        $stringAlterTable .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`$columnPrimaryKey`);";
276
                    } else {
277
                        $stringAlterTable .= " DROP PRIMARY KEY";
278
                    }
279
                }
280
            }
281
        }
282
283
        $jsonFile = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false;
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
284
        if ($jsonFile) {
285
            $pdo = self::getConnection()->prepare($stringAlterTable);
286
            if ($pdo->execute()) {
287
                return true;
288
            };
289
        } else {
290
            return $stringAlterTable;
291
        }
292
        return false;
293
    }
294
295
    /**
296
     * Cria a tabela
297
     * 
298
     * @param Object $obj
299
     * @return bool|string
300
     */
301
    public static function create($obj)
302
    {
303
        $nomeTabela        = FuncoesReflections::pegaNomeClasseObjeto($obj);
304
        $atributosTabela   = FuncoesReflections::pegaAtributosDoObjeto($obj);
305
        $annotationsTabela = Annotations::getAnnotation($obj);
306
        $arrFormatado      = [];
307
        $stringSql = "C" . "REATE TABLE IF NOT EXISTS `" . strtolower($nomeTabela) . "` (";
308
309
        for ($i = 0; $i < count($atributosTabela); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
310
311
            $stringSql .= $atributosTabela[$i] . " ";
312 View Code Duplication
            for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
314
                $arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]);
315
                for ($k = 0; $k < count($arrAtual) - 1; $k++) {
316
                    $arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1];
317
                }
318
            }
319 View Code Duplication
            if (array_key_exists('type', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320
                $stringSql .= " " . $arrFormatado['type'] . "";
321
322
            } else {
323
                $stringSql .= "";
324
            }
325
326 View Code Duplication
            if (array_key_exists('size', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
                if ($arrFormatado['size'] != 'none') {
328
                    $stringSql .= "(" . $arrFormatado['size'] . ") ";
329
                }
330
331
            } else {
332
                $stringSql .= "";
333
            }
334
335
            /**
336
             * @todo NOT NULL AQUI
337
             */
338
339 View Code Duplication
            if (array_key_exists('notNull', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
340
                if ($arrFormatado['notNull'] === "true") {
341
                    $stringSql .= " NOT NULL ";
342
                } else {
343
                    $stringSql .= "";
344
                };
345
            } else {
346
                $stringSql .= "";
347
            }
348
349 View Code Duplication
            if (array_key_exists('primaryKey', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
                if ($arrFormatado['primaryKey'] === "true") {
351
                    $stringSql .= " PRIMARY KEY ";
352
                } else {
353
                    $stringSql .= "";
354
                };
355
            } else {
356
                $stringSql .= "";
357
            }
358
359 View Code Duplication
            if (array_key_exists('autoIncrement', $arrFormatado)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360
                if ($arrFormatado['autoIncrement'] === "true") {
361
                    $stringSql .= " AUTO_INCREMENT ";
362
                } else {
363
                    $stringSql .= "";
364
                };
365
            } else {
366
                $stringSql .= "";
367
            }
368
369
            if ($i != count($atributosTabela) - 1) {
370
371
                $stringSql .= " , ";
372
            }
373
        }
374
375
        $stringSql .= ") ENGINE = InnoDB;";
376
        
377
        echo $stringSql;
378
        if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->code_sync == 1 ? true : false) {
0 ignored issues
show
Unused Code introduced by
The call to JsonReader::read() has too many arguments starting with BASE_DIR . '/phiber_config.json'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
379
            $pdo = self::getConnection()->prepare($stringSql);
380
            if ($pdo->execute()) {
381
                return true;
382
            };
383
        } else {
384
            return $stringSql;
385
        }
386
        
387
        return false;
388
    }
389
}
390