| Conditions | 28 |
| Paths | 4186 |
| Total Lines | 122 |
| Code Lines | 81 |
| 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 |
||
| 55 | public function createSchemaFromSqlfile(): bool |
||
| 56 | { |
||
| 57 | if (!\file_exists($this->fileSql)) { |
||
| 58 | \xoops_error('Error: Sql file not found!'); |
||
|
|
|||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | $tables = []; |
||
| 63 | $schema = []; |
||
| 64 | $tableName = ''; |
||
| 65 | |||
| 66 | // read sql file |
||
| 67 | $lines = \file($this->fileSql); |
||
| 68 | |||
| 69 | // remove unnecessary lines |
||
| 70 | foreach ($lines as $key => $value) { |
||
| 71 | $line = \trim($value); |
||
| 72 | // remove blank lines |
||
| 73 | if ('' === $line) { |
||
| 74 | unset($lines[$key]); |
||
| 75 | } |
||
| 76 | // remove comment lines |
||
| 77 | if ('#' === \substr($line, 0, 1)) { |
||
| 78 | unset($lines[$key]); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $skip = true; |
||
| 83 | $skipWords = ['CREATE DATABASE ', 'CREATE VIEW ', 'INSERT INTO ', 'SELECT ', 'DELETE ', 'UPDATE ', 'ALTER ', 'DROP ']; |
||
| 84 | $options = ''; |
||
| 85 | // read remaining lines line by line and create new schema |
||
| 86 | foreach ($lines as $key => $value) { |
||
| 87 | $line = \trim($value); |
||
| 88 | foreach ($skipWords as $skipWord) { |
||
| 89 | if ($skipWord === \mb_strtoupper(\substr($line, 0, \strlen($skipWord)))) { |
||
| 90 | $skip = true; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | if ('CREATE TABLE' === \mb_strtoupper(\substr($line, 0, 12))) { |
||
| 94 | $skip = false; |
||
| 95 | $options = ''; |
||
| 96 | // start table definition |
||
| 97 | $tableName = $this->getTableName ($line); |
||
| 98 | $tables[$tableName] = []; |
||
| 99 | $tables[$tableName]['options'] = ''; |
||
| 100 | $tables[$tableName]['columns'] = []; |
||
| 101 | $tables[$tableName]['keys'] = []; |
||
| 102 | } else { |
||
| 103 | if (false == $skip) { |
||
| 104 | if (')' === \mb_strtoupper(\substr($line, 0, 1))) { |
||
| 105 | // end of table definition |
||
| 106 | // get options |
||
| 107 | $this->getOptions($line, $options); |
||
| 108 | $tables[$tableName]['options'] = $options; |
||
| 109 | } elseif ('ENGINE' === \mb_strtoupper(\substr($line, 0, 6))) { |
||
| 110 | $this->getOptions($line, $options); |
||
| 111 | $tables[$tableName]['options'] = $options; |
||
| 112 | } elseif ('DEFAULT CHARSET ' === \mb_strtoupper(\substr($line, 0, 16))) { |
||
| 113 | $this->getOptions($line, $options); |
||
| 114 | $tables[$tableName]['options'] = $options; |
||
| 115 | } else { |
||
| 116 | // get keys and fields |
||
| 117 | switch (\mb_strtoupper(\substr($line, 0, 3))) { |
||
| 118 | case 'KEY': |
||
| 119 | case 'PRI': |
||
| 120 | case 'UNI': |
||
| 121 | $tables[$tableName]['keys'][] = $this->getKey($line); |
||
| 122 | break; |
||
| 123 | case 'else': |
||
| 124 | default: |
||
| 125 | $columns = $this->getColumns($line); |
||
| 126 | $tables[$tableName]['columns'][] = $columns; |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // create array for new schema |
||
| 135 | $level1 = \str_repeat(' ', 4); |
||
| 136 | $level2 = \str_repeat(' ', 8); |
||
| 137 | $level3 = \str_repeat(' ', 12); |
||
| 138 | |||
| 139 | foreach ($tables as $tkey => $table) { |
||
| 140 | $schema[] = "{$tkey}:\n"; |
||
| 141 | foreach ($table as $lkey => $line) { |
||
| 142 | if ('keys' == $lkey) { |
||
| 143 | $schema[] = $level1 . "keys:\n"; |
||
| 144 | foreach ($line as $kkey => $kvalue) { |
||
| 145 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
| 146 | $schema[] = $level2 . $kkey2 . ":\n"; |
||
| 147 | $schema[] = $level3 . 'columns: ' . $kvalue2['columns'] . "\n"; |
||
| 148 | $schema[] = $level3 . 'unique: ' . $kvalue2['unique'] . "\n"; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } elseif ('options' == $lkey) { |
||
| 152 | $schema[] = $level1 . 'options: ' . $line . "\n"; |
||
| 153 | } else { |
||
| 154 | $schema[] = $level1 . 'columns: ' . "\n"; |
||
| 155 | foreach ($line as $kkey => $kvalue) { |
||
| 156 | $schema[] = $level2 . '-' . "\n"; |
||
| 157 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
| 158 | $schema[] = $level3 . $kkey2 . ": " . $kvalue2 . "\n"; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // create new file and write schema array into this file |
||
| 166 | $myfile = \fopen($this->fileYaml, "w"); |
||
| 167 | if (false == $myfile || \is_null($myfile)) { |
||
| 168 | \xoops_error('Error: Unable to open sql file!'); |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | foreach ($schema as $line) { |
||
| 172 | \fwrite($myfile, $line); |
||
| 173 | } |
||
| 174 | \fclose($myfile); |
||
| 175 | |||
| 176 | return true; |
||
| 177 | |||
| 296 |