Conditions | 15 |
Paths | 329 |
Total Lines | 116 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
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 |
||
88 | protected function schemaToMigration(Schema $schema) { |
||
89 | $content = <<<'EOT' |
||
90 | /** @var ISchemaWrapper $schema */ |
||
91 | $schema = $schemaClosure(); |
||
92 | |||
93 | EOT; |
||
94 | |||
95 | foreach ($schema->getTables() as $table) { |
||
96 | $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
||
97 | |||
98 | if (!$schema->hasTable('{{table-name}}')) { |
||
99 | $table = $schema->createTable('{{table-name}}'); |
||
100 | |||
101 | EOT |
||
102 | ); |
||
103 | |||
104 | foreach ($table->getColumns() as $column) { |
||
105 | $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
||
106 | $table->addColumn('{{name}}', '{{type}}', [ |
||
107 | |||
108 | EOT |
||
109 | ); |
||
110 | if ($column->getAutoincrement()) { |
||
111 | $content .= <<<'EOT' |
||
112 | 'autoincrement' => true, |
||
113 | |||
114 | EOT; |
||
115 | } |
||
116 | $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
||
117 | 'notnull' => {{notnull}}, |
||
118 | |||
119 | EOT |
||
120 | ); |
||
121 | if ($column->getLength() !== null) { |
||
122 | $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
||
123 | 'length' => {{length}}, |
||
124 | |||
125 | EOT |
||
126 | ); |
||
127 | } |
||
128 | $default = $column->getDefault(); |
||
129 | if ($default !== null) { |
||
130 | if (is_string($default)) { |
||
131 | $default = "'$default'"; |
||
132 | } else if (is_bool($default)) { |
||
133 | $default = ($default === true) ? 'true' : 'false'; |
||
134 | } |
||
135 | $content .= str_replace('{{default}}', $default, <<<'EOT' |
||
136 | 'default' => {{default}}, |
||
137 | |||
138 | EOT |
||
139 | ); |
||
140 | } |
||
141 | if ($column->getUnsigned()) { |
||
142 | $content .= <<<'EOT' |
||
143 | 'unsigned' => true, |
||
144 | |||
145 | EOT; |
||
146 | } |
||
147 | |||
148 | $content .= <<<'EOT' |
||
149 | ]); |
||
150 | |||
151 | EOT; |
||
152 | } |
||
153 | |||
154 | $content .= <<<'EOT' |
||
155 | |||
156 | EOT; |
||
157 | |||
158 | $primaryKey = $table->getPrimaryKey(); |
||
159 | if ($primaryKey !== null) { |
||
160 | $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
||
161 | $table->setPrimaryKey(['{{columns}}']); |
||
162 | |||
163 | EOT |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | foreach ($table->getIndexes() as $index) { |
||
168 | if ($index->isPrimary()) { |
||
169 | continue; |
||
170 | } |
||
171 | |||
172 | if ($index->isUnique()) { |
||
173 | $content .= str_replace( |
||
174 | ['{{columns}}', '{{name}}'], |
||
175 | [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
||
176 | <<<'EOT' |
||
177 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
||
178 | |||
179 | EOT |
||
180 | ); |
||
181 | } else { |
||
182 | $content .= str_replace( |
||
183 | ['{{columns}}', '{{name}}'], |
||
184 | [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
||
185 | <<<'EOT' |
||
186 | $table->addIndex(['{{columns}}'], '{{name}}'); |
||
187 | |||
188 | EOT |
||
189 | ); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $content .= <<<'EOT' |
||
194 | } |
||
195 | |||
196 | EOT; |
||
197 | } |
||
198 | |||
199 | $content .= <<<'EOT' |
||
200 | return $schema; |
||
201 | EOT; |
||
202 | |||
203 | return $content; |
||
204 | } |
||
206 |