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