| Conditions | 18 |
| Paths | 1515 |
| Total Lines | 73 |
| Code Lines | 39 |
| Lines | 6 |
| Ratio | 8.22 % |
| 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 |
||
| 32 | public function analyse($appId) { |
||
| 33 | $appPath = \OC_App::getAppPath($appId); |
||
| 34 | if ($appPath === false) { |
||
| 35 | throw new \RuntimeException("No app with given id <$appId> known."); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!file_exists($appPath . '/appinfo/database.xml')) { |
||
| 39 | return ['errors' => [], 'warnings' => []]; |
||
| 40 | } |
||
| 41 | |||
| 42 | libxml_use_internal_errors(true); |
||
| 43 | $loadEntities = libxml_disable_entity_loader(false); |
||
| 44 | $xml = simplexml_load_file($appPath . '/appinfo/database.xml'); |
||
| 45 | libxml_disable_entity_loader($loadEntities); |
||
| 46 | |||
| 47 | |||
| 48 | $errors = $warnings = []; |
||
| 49 | |||
| 50 | foreach ($xml->table as $table) { |
||
| 51 | // Table names |
||
| 52 | if (strpos($table->name, '*dbprefix*') !== 0) { |
||
| 53 | $errors[] = 'Database schema error: name of table ' . $table->name . ' does not start with *dbprefix*'; |
||
| 54 | } |
||
| 55 | $tableName = substr($table->name, strlen('*dbprefix*')); |
||
| 56 | if (strpos($tableName, '*dbprefix*') !== false) { |
||
| 57 | $warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . $table->name; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (strlen($tableName) > 27) { |
||
| 61 | $errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; |
||
| 62 | } |
||
| 63 | |||
| 64 | $hasAutoIncrement = false; |
||
| 65 | |||
| 66 | // Column names |
||
| 67 | foreach ($table->declaration->field as $column) { |
||
| 68 | if (strpos($column->name, '*dbprefix*') !== false) { |
||
| 69 | $warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . $column->name . ' on table ' . $table->name; |
||
| 70 | } |
||
| 71 | |||
| 72 | View Code Duplication | if (strlen($column->name) > 30) { |
|
| 73 | $errors[] = 'Database schema error: Name of column ' . $column->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed'; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($column->autoincrement) { |
||
| 77 | if ($hasAutoIncrement) { |
||
| 78 | $errors[] = 'Database schema error: Table ' . $table->name . ' has multiple autoincrement columns'; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (strlen($tableName) > 21) { |
||
| 82 | $errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; |
||
| 83 | } |
||
| 84 | |||
| 85 | $hasAutoIncrement = true; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // Index names |
||
| 90 | foreach ($table->declaration->index as $index) { |
||
| 91 | $hasPrefix = strpos($index->name, '*dbprefix*'); |
||
| 92 | if ($hasPrefix !== false && $hasPrefix !== 0) { |
||
| 93 | $warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . $index->name . ' on table ' . $table->name; |
||
| 94 | } |
||
| 95 | |||
| 96 | $indexName = $hasPrefix === 0 ? substr($index->name, strlen('*dbprefix*')) : $index->name; |
||
| 97 | View Code Duplication | if (strlen($indexName) > 27) { |
|
| 98 | $errors[] = 'Database schema error: Name of index ' . $index->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed'; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return ['errors' => $errors, 'warnings' => $warnings]; |
||
| 104 | } |
||
| 105 | } |
||
| 106 |