Conditions | 34 |
Paths | > 20000 |
Total Lines | 156 |
Code Lines | 98 |
Lines | 55 |
Ratio | 35.26 % |
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:
1 | <?php |
||
138 | static function alter($obj) |
||
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++) { |
||
147 | View Code Duplication | for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) { |
|
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++) { |
|
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++) { |
||
173 | View Code Duplication | if ($j != count($arrayDiff) - 1) { |
|
174 | $stringSql .= "ADD " . $arrayDiff[$j]; |
||
175 | } else { |
||
176 | $stringSql .= "ADD " . $arrayDiff[$j]; |
||
177 | } |
||
178 | View Code Duplication | if (array_key_exists('type', $arrFormatado)) { |
|
179 | |||
180 | $stringSql .= " " . $arrFormatado['type'] . ""; |
||
181 | |||
182 | } else { |
||
183 | $stringSql .= ""; |
||
184 | } |
||
185 | |||
186 | View Code Duplication | if (array_key_exists('size', $arrFormatado)) { |
|
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)) { |
|
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)) { |
|
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)) { |
|
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; |
||
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++) { |
||
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) { |
||
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; |
||
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 | |||
390 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.