Conditions | 40 |
Paths | 13329 |
Total Lines | 111 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Tests | 93 |
CRAP Score | 41.8947 |
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 |
||
157 | 25 | private function loadField($table, $xml) { |
|
158 | 25 | $options = array( 'notnull' => false ); |
|
159 | 25 | foreach ($xml->children() as $child) { |
|
160 | /** |
||
161 | * @var \SimpleXMLElement $child |
||
162 | */ |
||
163 | 25 | switch ($child->getName()) { |
|
164 | 25 | case 'name': |
|
165 | 25 | $name = (string)$child; |
|
166 | 25 | $name = $this->platform->quoteIdentifier($name); |
|
167 | 25 | break; |
|
168 | 25 | case 'type': |
|
169 | 25 | $type = (string)$child; |
|
170 | switch ($type) { |
||
171 | 25 | case 'text': |
|
172 | 25 | $type = 'string'; |
|
173 | 25 | break; |
|
174 | 24 | case 'clob': |
|
175 | 23 | $type = 'text'; |
|
176 | 23 | break; |
|
177 | 24 | case 'timestamp': |
|
178 | 20 | $type = 'datetime'; |
|
179 | 20 | break; |
|
180 | 24 | case 'numeric': |
|
181 | $type = 'decimal'; |
||
182 | break; |
||
183 | } |
||
184 | 25 | break; |
|
185 | 25 | case 'length': |
|
186 | 25 | $length = (string)$child; |
|
187 | 25 | $options['length'] = $length; |
|
188 | 25 | break; |
|
189 | 25 | case 'unsigned': |
|
190 | 20 | $unsigned = $this->asBool($child); |
|
191 | 20 | $options['unsigned'] = $unsigned; |
|
192 | 20 | break; |
|
193 | 25 | case 'notnull': |
|
194 | 25 | $notnull = $this->asBool($child); |
|
195 | 25 | $options['notnull'] = $notnull; |
|
196 | 25 | break; |
|
197 | 25 | case 'autoincrement': |
|
198 | 24 | $autoincrement = $this->asBool($child); |
|
199 | 24 | $options['autoincrement'] = $autoincrement; |
|
200 | 24 | break; |
|
201 | 25 | case 'default': |
|
202 | 25 | $default = (string)$child; |
|
203 | 25 | $options['default'] = $default; |
|
204 | 25 | break; |
|
205 | 23 | case 'comments': |
|
206 | 20 | $comment = (string)$child; |
|
207 | 20 | $options['comment'] = $comment; |
|
208 | 20 | break; |
|
209 | 23 | case 'primary': |
|
210 | $primary = $this->asBool($child); |
||
211 | $options['primary'] = $primary; |
||
212 | break; |
||
213 | 23 | case 'precision': |
|
214 | 23 | $precision = (string)$child; |
|
215 | 23 | $options['precision'] = $precision; |
|
216 | 23 | break; |
|
217 | 23 | case 'scale': |
|
218 | 23 | $scale = (string)$child; |
|
219 | 23 | $options['scale'] = $scale; |
|
220 | 23 | break; |
|
221 | default: |
||
222 | throw new \DomainException('Unknown element: ' . $child->getName()); |
||
223 | |||
224 | 25 | } |
|
225 | 25 | } |
|
226 | 25 | if (isset($name) && isset($type)) { |
|
227 | 25 | if (isset($options['default']) && empty($options['default'])) { |
|
228 | 24 | if (empty($options['notnull']) || !$options['notnull']) { |
|
229 | 20 | unset($options['default']); |
|
230 | 20 | $options['notnull'] = false; |
|
231 | 20 | } else { |
|
232 | 24 | $options['default'] = ''; |
|
233 | } |
||
234 | 24 | if ($type == 'integer' || $type == 'decimal') { |
|
235 | 24 | $options['default'] = 0; |
|
236 | 24 | } elseif ($type == 'boolean') { |
|
237 | $options['default'] = false; |
||
238 | } |
||
239 | 24 | if (!empty($options['autoincrement']) && $options['autoincrement']) { |
|
240 | 24 | unset($options['default']); |
|
241 | 24 | } |
|
242 | 24 | } |
|
243 | 25 | if ($type === 'integer' && isset($options['default'])) { |
|
244 | 23 | $options['default'] = (int)$options['default']; |
|
245 | 23 | } |
|
246 | 25 | if ($type === 'integer' && isset($options['length'])) { |
|
247 | 24 | $length = $options['length']; |
|
248 | 24 | if ($length < 4) { |
|
249 | 1 | $type = 'smallint'; |
|
250 | 24 | } else if ($length > 4) { |
|
251 | $type = 'bigint'; |
||
252 | } |
||
253 | 24 | } |
|
254 | 25 | if ($type === 'boolean' && isset($options['default'])) { |
|
255 | 3 | $options['default'] = $this->asBool($options['default']); |
|
256 | 3 | } |
|
257 | 25 | if (!empty($options['autoincrement']) |
|
258 | 25 | && !empty($options['notnull']) |
|
259 | 25 | ) { |
|
260 | 24 | $options['primary'] = true; |
|
261 | 24 | } |
|
262 | 25 | $table->addColumn($name, $type, $options); |
|
263 | 25 | if (!empty($options['primary']) && $options['primary']) { |
|
264 | 24 | $table->setPrimaryKey(array($name)); |
|
265 | 24 | } |
|
266 | 25 | } |
|
267 | 25 | } |
|
268 | |||
348 |