| Conditions | 32 | 
| Paths | > 20000 | 
| Total Lines | 208 | 
| Code Lines | 124 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 2 | ||
| 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 | ||
| 76 | public function createUpdateDbTableByAnnotations(string $modelClassName): bool | ||
| 77 |     { | ||
| 78 | $result = true; | ||
| 79 | if ( | ||
| 80 | ! class_exists($modelClassName) | ||
| 81 |             || count(get_class_vars($modelClassName)) === 0) { | ||
| 82 | return true; | ||
| 83 | } | ||
| 84 | // Test is abstract | ||
| 85 |         try { | ||
| 86 | $reflection = new ReflectionClass($modelClassName); | ||
| 87 |             if ($reflection->isAbstract()) { | ||
| 88 | return true; | ||
| 89 | } | ||
| 90 |         } catch (\ReflectionException $exception) { | ||
| 91 | return false; | ||
| 92 | } | ||
| 93 | $model = new $modelClassName(); | ||
| 94 | $connectionServiceName = $model->getReadConnectionService(); | ||
| 95 |         if (empty($connectionServiceName)) { | ||
| 96 | return false; | ||
| 97 | } | ||
| 98 | |||
| 99 | $connectionService = $this->di->getShared($connectionServiceName); | ||
| 100 |         $metaData          = $this->di->get('modelsMetadata'); | ||
| 101 | |||
| 102 | //https://docs.phalcon.io/4.0/ru-ru/annotations | ||
| 103 |         $modelAnnotation = $this->di->get('annotations')->get($model); | ||
| 104 | |||
| 105 | $tableName = $model->getSource(); | ||
| 106 | $table_structure = []; | ||
| 107 | $indexes = []; | ||
| 108 | |||
| 109 | // Create columns list by code annotations | ||
| 110 | $newColNames = $metaData->getAttributes($model); | ||
| 111 | $previousAttribute = ''; | ||
| 112 |         foreach ($newColNames as $attribute) { | ||
| 113 | $table_structure[$attribute] = [ | ||
| 114 | 'type' => Column::TYPE_VARCHAR, | ||
| 115 | 'after' => $previousAttribute, | ||
| 116 | 'notNull' => false, | ||
| 117 | 'isNumeric' => false, | ||
| 118 | 'primary' => false, | ||
| 119 | ]; | ||
| 120 | $previousAttribute = $attribute; | ||
| 121 | } | ||
| 122 | |||
| 123 | // Set data types | ||
| 124 | $propertiesAnnotations = $modelAnnotation->getPropertiesAnnotations(); | ||
| 125 |         if ($propertiesAnnotations !== false) { | ||
| 126 | $attributeTypes = $metaData->getDataTypes($model); | ||
| 127 |             foreach ($attributeTypes as $attribute => $type) { | ||
| 128 | $table_structure[$attribute]['type'] = $type; | ||
| 129 | // Try to find size of field | ||
| 130 |                 if (array_key_exists($attribute, $propertiesAnnotations)) { | ||
| 131 | $propertyDescription = $propertiesAnnotations[$attribute]; | ||
| 132 |                     if ($propertyDescription->has('Column') | ||
| 133 |                         && $propertyDescription->get('Column')->hasArgument('length') | ||
| 134 |                     ) { | ||
| 135 |                         $table_structure[$attribute]['size'] = $propertyDescription->get('Column')->getArgument( | ||
| 136 | 'length' | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | // For each numeric column change type | ||
| 144 | $numericAttributes = $metaData->getDataTypesNumeric($model); | ||
| 145 |         foreach ($numericAttributes as $attribute => $value) { | ||
| 146 | $table_structure[$attribute]['type'] = Column::TYPE_INTEGER; | ||
| 147 | $table_structure[$attribute]['isNumeric'] = true; | ||
| 148 | } | ||
| 149 | |||
| 150 | // For each not nullable column change type | ||
| 151 | $notNull = $metaData->getNotNullAttributes($model); | ||
| 152 |         foreach ($notNull as $attribute) { | ||
| 153 | $table_structure[$attribute]['notNull'] = true; | ||
| 154 | } | ||
| 155 | |||
| 156 | // Set default values for initial save, later it fill at Models\ModelBase\beforeValidationOnCreate | ||
| 157 | $defaultValues = $metaData->getDefaultValues($model); | ||
| 158 |         foreach ($defaultValues as $key => $value) { | ||
| 159 |             if ($value !== null) { | ||
| 160 | $table_structure[$key]['default'] = $value; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | // Set primary keys | ||
| 165 | // $primaryKeys = $metaData->getPrimaryKeyAttributes($model); | ||
| 166 |         // foreach ($primaryKeys as $attribute) { | ||
| 167 | // $indexes[$attribute] = new Index($attribute, [$attribute], 'UNIQUE'); | ||
| 168 | // } | ||
| 169 | |||
| 170 | // Set bind types | ||
| 171 | $bindTypes = $metaData->getBindTypes($model); | ||
| 172 |         foreach ($bindTypes as $attribute => $value) { | ||
| 173 | $table_structure[$attribute]['bindType'] = $value; | ||
| 174 | } | ||
| 175 | |||
| 176 | // Find auto incremental column, usually it is ID column | ||
| 177 | $keyFiled = $metaData->getIdentityField($model); | ||
| 178 |         if ($keyFiled) { | ||
| 179 | unset($indexes[$keyFiled]); | ||
| 180 | $table_structure[$keyFiled] = [ | ||
| 181 | 'type' => Column::TYPE_INTEGER, | ||
| 182 | 'notNull' => true, | ||
| 183 | 'autoIncrement' => true, | ||
| 184 | 'primary' => true, | ||
| 185 | 'isNumeric' => true, | ||
| 186 | 'first' => true, | ||
| 187 | ]; | ||
| 188 | } | ||
| 189 | |||
| 190 | // Some exceptions | ||
| 191 |         if ($modelClassName === PbxSettings::class) { | ||
| 192 | $keyFiled = 'key'; | ||
| 193 | unset($indexes[$keyFiled]); | ||
| 194 | $table_structure[$keyFiled] = [ | ||
| 195 | 'type' => Column::TYPE_VARCHAR, | ||
| 196 | 'notNull' => true, | ||
| 197 | 'autoIncrement' => false, | ||
| 198 | 'primary' => true, | ||
| 199 | 'isNumeric' => false, | ||
| 200 | 'first' => true, | ||
| 201 | ]; | ||
| 202 | } | ||
| 203 | |||
| 204 | // Create additional indexes | ||
| 205 | $modelClassAnnotation = $modelAnnotation->getClassAnnotations(); | ||
| 206 | if ($modelClassAnnotation !== false | ||
| 207 |             && $modelClassAnnotation->has('Indexes')) { | ||
| 208 |             $additionalIndexes = $modelClassAnnotation->get('Indexes')->getArguments(); | ||
| 209 |             foreach ($additionalIndexes as $index) { | ||
| 210 |                 $indexName           = "i_{$tableName}_{$index['name']}"; | ||
| 211 | $indexes[$indexName] = new Index($indexName, $index['columns'], $index['type']); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | // Create new table structure | ||
| 216 | $columns = []; | ||
| 217 |         foreach ($table_structure as $colName => $colType) { | ||
| 218 | $columns[] = new Column($colName, $colType); | ||
| 219 | } | ||
| 220 | |||
| 221 | $columnsNew = [ | ||
| 222 | 'columns' => $columns, | ||
| 223 | 'indexes' => $indexes, | ||
| 224 | ]; | ||
| 225 | |||
| 226 | $connectionService->begin(); | ||
| 227 | |||
| 228 |         if ( ! $connectionService->tableExists($tableName)) { | ||
| 229 |             Util::echoWithSyslog(' - UpdateDatabase: Create new table: ' . $tableName . ' '); | ||
| 230 | $result = $connectionService->createTable($tableName, '', $columnsNew); | ||
| 231 | Util::echoGreenDone(); | ||
| 232 |         } else { | ||
| 233 | // Table exists, we have to check/upgrade its structure | ||
| 234 | $currentColumnsArr = $connectionService->describeColumns($tableName, ''); | ||
| 235 | |||
| 236 |             if ($this->isTableStructureNotEqual($currentColumnsArr, $columns)) { | ||
| 237 |                 Util::echoWithSyslog(' - UpdateDatabase: Upgrade table: ' . $tableName . ' '); | ||
| 238 | // Create new table and copy all data | ||
| 239 | $currentStateColumnList = []; | ||
| 240 | $oldColNames = []; // Старые названия колонок | ||
| 241 | $countColumnsTemp = count($currentColumnsArr); | ||
| 242 |                 for ($k = 0; $k < $countColumnsTemp; $k++) { | ||
| 243 | $currentStateColumnList[$k] = $currentColumnsArr[$k]->getName(); | ||
| 244 | $oldColNames[] = $currentColumnsArr[$k]->getName(); | ||
| 245 | } | ||
| 246 | |||
| 247 | // Create temporary clone on current table with all columns and date | ||
| 248 | // Delete original table | ||
| 249 |                 $gluedColumns = implode(',', $currentStateColumnList); | ||
| 250 |                 $query        = "CREATE TEMPORARY TABLE {$tableName}_backup({$gluedColumns});  | ||
| 251 | INSERT INTO {$tableName}_backup SELECT {$gluedColumns} FROM {$tableName};  | ||
| 252 | DROP TABLE  {$tableName}"; | ||
| 253 | $result = $result && $connectionService->execute($query); | ||
| 254 | |||
| 255 | // Create new table with new columns structure | ||
| 256 | $result = $result && $connectionService->createTable($tableName, '', $columnsNew); | ||
| 257 | |||
| 258 | // Copy data from temporary table to newly created | ||
| 259 | $newColumnNames = array_intersect($newColNames, $oldColNames); | ||
| 260 |                 $gluedNewColumns = implode(',', $newColumnNames); | ||
| 261 | $result = $result && $connectionService->execute( | ||
| 262 |                         "INSERT INTO {$tableName} ( {$gluedNewColumns}) SELECT {$gluedNewColumns}  FROM {$tableName}_backup;" | ||
| 263 | ); | ||
| 264 | |||
| 265 | // Drop temporary table | ||
| 266 |                 $result = $result && $connectionService->execute("DROP TABLE {$tableName}_backup;"); | ||
| 267 | Util::echoGreenDone(); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 |         if ($result) { | ||
| 273 | $this->updateIndexes($tableName, $connectionService, $indexes); | ||
| 274 | } | ||
| 275 | |||
| 276 |         if ($result) { | ||
| 277 | $connectionService->commit(); | ||
| 278 |         } else { | ||
| 279 |             Util::sysLogMsg('createUpdateDbTableByAnnotations', "Error: Failed on create/update table {$tableName}"); | ||
| 280 | $connectionService->rollback(); | ||
| 281 | } | ||
| 282 | |||
| 283 | return $result; | ||
| 284 | } | ||
| 388 | } |