Conditions | 23 |
Paths | 1081 |
Total Lines | 103 |
Code Lines | 65 |
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 | // read remaining lines line by line and create new schema |
||
83 | foreach ($lines as $key => $value) { |
||
84 | $line = \trim($value); |
||
85 | if ('CREATE TABLE' === \mb_strtoupper(\substr($line, 0, 12))) { |
||
86 | // start table definition |
||
87 | $tableName = $this->getTableName ($line); |
||
88 | $tables[$tableName] = []; |
||
89 | $tables[$tableName]['options'] = ''; |
||
90 | $tables[$tableName]['columns'] = []; |
||
91 | $tables[$tableName]['keys'] = []; |
||
92 | } else { |
||
93 | if (')' === \mb_strtoupper(\substr($line, 0, 1))) { |
||
94 | // end of table definition |
||
95 | // get options |
||
96 | $tables[$tableName]['options'] = $this->getOptions($line); |
||
97 | } else { |
||
98 | // get keys and fields |
||
99 | switch (\mb_strtoupper(\substr($line, 0, 3))) { |
||
100 | case 'KEY': |
||
101 | case 'PRI': |
||
102 | case 'UNI': |
||
103 | $tables[$tableName]['keys'][] = $this->getKey($line); |
||
104 | break; |
||
105 | case 'else': |
||
106 | default: |
||
107 | $columns = $this->getColumns($line); |
||
108 | $tables[$tableName]['columns'][] = $columns; |
||
109 | break; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // create array for new schema |
||
116 | $level1 = \str_repeat(' ', 4); |
||
117 | $level2 = \str_repeat(' ', 8); |
||
118 | $level3 = \str_repeat(' ', 12); |
||
119 | |||
120 | foreach ($tables as $tkey => $table) { |
||
121 | $schema[] = "{$tkey}:\n"; |
||
122 | foreach ($table as $lkey => $line) { |
||
123 | if ('keys' == $lkey) { |
||
124 | $schema[] = $level1 . "keys:\n"; |
||
125 | foreach ($line as $kkey => $kvalue) { |
||
126 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
127 | $schema[] = $level2 . $kkey2 . ":\n"; |
||
128 | $schema[] = $level3 . 'columns: ' . $kvalue2['columns'] . "\n"; |
||
129 | $schema[] = $level3 . 'unique: ' . $kvalue2['unique'] . "\n"; |
||
130 | } |
||
131 | } |
||
132 | } elseif ('options' == $lkey) { |
||
133 | $schema[] = $level1 . 'options: ' . $line . "\n"; |
||
134 | } else { |
||
135 | $schema[] = $level1 . 'columns: ' . "\n"; |
||
136 | foreach ($line as $kkey => $kvalue) { |
||
137 | $schema[] = $level2 . '-' . "\n"; |
||
138 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
139 | $schema[] = $level3 . $kkey2 . ": " . $kvalue2 . "\n"; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // create new file and write schema array into this file |
||
147 | $myfile = \fopen($this->fileYaml, "w"); |
||
148 | if (false == $myfile || \is_null($myfile)) { |
||
149 | \xoops_error('Error: Unable to open sql file!'); |
||
150 | return false; |
||
151 | } |
||
152 | foreach ($schema as $line) { |
||
153 | \fwrite($myfile, $line); |
||
154 | } |
||
155 | \fclose($myfile); |
||
156 | |||
157 | return true; |
||
158 | |||
271 |