Conditions | 40 |
Paths | 13329 |
Total Lines | 111 |
Code Lines | 91 |
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 |
||
136 | private function loadField($table, $xml) { |
||
137 | $options = array( 'notnull' => false ); |
||
138 | foreach ($xml->children() as $child) { |
||
139 | /** |
||
140 | * @var \SimpleXMLElement $child |
||
141 | */ |
||
142 | switch ($child->getName()) { |
||
143 | case 'name': |
||
144 | $name = (string)$child; |
||
145 | $name = $this->platform->quoteIdentifier($name); |
||
146 | break; |
||
147 | case 'type': |
||
148 | $type = (string)$child; |
||
149 | switch ($type) { |
||
150 | case 'text': |
||
151 | $type = 'string'; |
||
152 | break; |
||
153 | case 'clob': |
||
154 | $type = 'text'; |
||
155 | break; |
||
156 | case 'timestamp': |
||
157 | $type = 'datetime'; |
||
158 | break; |
||
159 | case 'numeric': |
||
160 | $type = 'decimal'; |
||
161 | break; |
||
162 | } |
||
163 | break; |
||
164 | case 'length': |
||
165 | $length = (string)$child; |
||
166 | $options['length'] = $length; |
||
167 | break; |
||
168 | case 'unsigned': |
||
169 | $unsigned = $this->asBool($child); |
||
170 | $options['unsigned'] = $unsigned; |
||
171 | break; |
||
172 | case 'notnull': |
||
173 | $notnull = $this->asBool($child); |
||
174 | $options['notnull'] = $notnull; |
||
175 | break; |
||
176 | case 'autoincrement': |
||
177 | $autoincrement = $this->asBool($child); |
||
178 | $options['autoincrement'] = $autoincrement; |
||
179 | break; |
||
180 | case 'default': |
||
181 | $default = (string)$child; |
||
182 | $options['default'] = $default; |
||
183 | break; |
||
184 | case 'comments': |
||
185 | $comment = (string)$child; |
||
186 | $options['comment'] = $comment; |
||
187 | break; |
||
188 | case 'primary': |
||
189 | $primary = $this->asBool($child); |
||
190 | $options['primary'] = $primary; |
||
191 | break; |
||
192 | case 'precision': |
||
193 | $precision = (string)$child; |
||
194 | $options['precision'] = $precision; |
||
195 | break; |
||
196 | case 'scale': |
||
197 | $scale = (string)$child; |
||
198 | $options['scale'] = $scale; |
||
199 | break; |
||
200 | default: |
||
201 | throw new \DomainException('Unknown element: ' . $child->getName()); |
||
202 | |||
203 | } |
||
204 | } |
||
205 | if (isset($name) && isset($type)) { |
||
206 | if (isset($options['default']) && empty($options['default'])) { |
||
207 | if (empty($options['notnull']) || !$options['notnull']) { |
||
208 | unset($options['default']); |
||
209 | $options['notnull'] = false; |
||
210 | } else { |
||
211 | $options['default'] = ''; |
||
212 | } |
||
213 | if ($type == 'integer' || $type == 'decimal') { |
||
214 | $options['default'] = 0; |
||
215 | } elseif ($type == 'boolean') { |
||
216 | $options['default'] = false; |
||
217 | } |
||
218 | if (!empty($options['autoincrement']) && $options['autoincrement']) { |
||
219 | unset($options['default']); |
||
220 | } |
||
221 | } |
||
222 | if ($type === 'integer' && isset($options['default'])) { |
||
223 | $options['default'] = (int)$options['default']; |
||
224 | } |
||
225 | if ($type === 'integer' && isset($options['length'])) { |
||
226 | $length = $options['length']; |
||
227 | if ($length < 4) { |
||
228 | $type = 'smallint'; |
||
229 | } else if ($length > 4) { |
||
230 | $type = 'bigint'; |
||
231 | } |
||
232 | } |
||
233 | if ($type === 'boolean' && isset($options['default'])) { |
||
234 | $options['default'] = $this->asBool($options['default']); |
||
235 | } |
||
236 | if (!empty($options['autoincrement']) |
||
237 | && !empty($options['notnull']) |
||
238 | ) { |
||
239 | $options['primary'] = true; |
||
240 | } |
||
241 | $table->addColumn($name, $type, $options); |
||
242 | if (!empty($options['primary']) && $options['primary']) { |
||
243 | $table->setPrimaryKey(array($name)); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
327 |