| Conditions | 10 |
| Paths | 65 |
| Total Lines | 46 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 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 |
||
| 47 | public static function checkSqlSyntax( $files ) { |
||
| 48 | if ( !Sqlite::isPresent() ) { |
||
| 49 | throw new MWException( "Can't check SQL syntax: SQLite not found" ); |
||
| 50 | } |
||
| 51 | if ( !is_array( $files ) ) { |
||
| 52 | $files = [ $files ]; |
||
| 53 | } |
||
| 54 | |||
| 55 | $allowedTypes = array_flip( [ |
||
| 56 | 'integer', |
||
| 57 | 'real', |
||
| 58 | 'text', |
||
| 59 | 'blob', // NULL type is omitted intentionally |
||
| 60 | ] ); |
||
| 61 | |||
| 62 | $db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
||
| 63 | try { |
||
| 64 | foreach ( $files as $file ) { |
||
| 65 | $err = $db->sourceFile( $file ); |
||
| 66 | if ( $err != true ) { |
||
| 67 | return $err; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ ); |
||
| 72 | foreach ( $tables as $table ) { |
||
| 73 | if ( strpos( $table->name, 'sqlite_' ) === 0 ) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ ); |
||
| 78 | foreach ( $columns as $col ) { |
||
| 79 | if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) { |
||
| 80 | $db->close(); |
||
| 81 | |||
| 82 | return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'"; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } catch ( DBError $e ) { |
||
| 87 | return $e->getMessage(); |
||
| 88 | } |
||
| 89 | $db->close(); |
||
| 90 | |||
| 91 | return true; |
||
| 92 | } |
||
| 93 | } |
||
| 94 |