Conditions | 49 |
Paths | > 20000 |
Total Lines | 149 |
Lines | 74 |
Ratio | 49.66 % |
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 |
||
196 | function parse_create_table( $sql ) { |
||
197 | $table = new stdClass(); |
||
198 | |||
199 | $table->raw = $sql; |
||
200 | $table->columns = new stdClass(); |
||
201 | $table->primary = null; |
||
202 | $table->uniques = new stdClass(); |
||
203 | $table->keys = new stdClass(); |
||
204 | $sql = explode( "\n", trim( $sql ) ); |
||
205 | $table->engine = preg_replace( '/^.+ ENGINE=(\S+) .+$/i', "$1", $sql[(count($sql)-1)] ); |
||
206 | $table->charset = preg_replace( '/^.+ DEFAULT CHARSET=(\S+)( .+)?$/i', "$1", $sql[(count($sql)-1)] ); |
||
207 | $table->single_int_paging_column = null; |
||
208 | |||
209 | foreach ( $sql as $idx => $val ) |
||
210 | $sql[$idx] = trim($val); |
||
211 | $columns = preg_grep( '/^\s*`[^`]+`\s*\S*/', $sql ); |
||
212 | if ( !$columns ) |
||
213 | return false; |
||
214 | |||
215 | $table->name = preg_replace( '/(^[^`]+`|`[^`]+$)/', '', array_shift( preg_grep( '/^CREATE\s+TABLE\s+/', $sql ) ) ); |
||
216 | |||
217 | foreach ( $columns as $line ) { |
||
218 | preg_match( '/^`([^`]+)`\s+([a-z]+)(\(\d+\))?\s*/', $line, $m ); |
||
219 | $name = $m[1]; |
||
220 | $table->columns->$name = new stdClass(); |
||
221 | $table->columns->$name->null = (bool)stripos( $line, ' NOT NULL ' ); |
||
222 | $table->columns->$name->type = $m[2]; |
||
223 | View Code Duplication | if ( isset($m[3]) ) { |
|
224 | if ( substr( $m[3], 0, 1 ) == '(' ) |
||
225 | $table->columns->$name->length = substr( $m[3], 1, -1 ); |
||
226 | else |
||
227 | $table->columns->$name->length = $m[3]; |
||
228 | } else { |
||
229 | $table->columns->$name->length = null; |
||
230 | } |
||
231 | View Code Duplication | if ( preg_match( '/ character set (\S+)/i', $line, $m ) ) { |
|
232 | $table->columns->$name->charset = $m[1]; |
||
233 | } else { |
||
234 | $table->columns->$name->charset = ''; |
||
235 | } |
||
236 | View Code Duplication | if ( preg_match( '/ collate (\S+)/i', $line, $m ) ) { |
|
237 | $table->columns->$name->collate = $m[1]; |
||
238 | } else { |
||
239 | $table->columns->$name->collate = ''; |
||
240 | } |
||
241 | View Code Duplication | if ( preg_match( '/ DEFAULT (.+),$/i', $line, $m ) ) { |
|
242 | if ( substr( $m[1], 0, 1 ) == "'" ) |
||
243 | $table->columns->$name->default = substr( $m[1], 1, -1 ); |
||
244 | else |
||
245 | $table->columns->$name->default = $m[1]; |
||
246 | } else { |
||
247 | $table->columns->$name->default = null; |
||
248 | } |
||
249 | $table->columns->$name->line = $line; |
||
250 | } |
||
251 | $pk = preg_grep( '/^PRIMARY\s+KEY\s+/i', $sql ); |
||
252 | if ( count( $pk ) ) { |
||
253 | $pk = array_pop( $pk ); |
||
254 | $pk = preg_replace( '/(^[^\(]+\(`|`\),?$)/', '', $pk ); |
||
255 | $pk = preg_replace( '/\([0-9]+\)/', '', $pk ); |
||
256 | $pk = explode( '`,`', $pk ); |
||
257 | $table->primary = $pk; |
||
258 | } |
||
259 | View Code Duplication | if ( is_array( $table->primary ) && count( $table->primary ) == 1 ) { |
|
260 | $pk_column_name = $table->primary[0]; |
||
261 | switch( strtolower( $table->columns->$pk_column_name->type ) ) { |
||
262 | // Integers, exact value |
||
263 | case 'tinyint': |
||
264 | case 'smallint': |
||
265 | case 'int': |
||
266 | case 'integer': |
||
267 | case 'bigint': |
||
268 | // Fixed point, exact value |
||
269 | case 'decimal': |
||
270 | case 'numeric': |
||
271 | // Floating point, approximate value |
||
272 | case 'float': |
||
273 | case 'double': |
||
274 | case 'real': |
||
275 | // Date and Time |
||
276 | case 'date': |
||
277 | case 'datetime': |
||
278 | case 'timestamp': |
||
279 | $table->single_int_paging_column = $pk_column_name; |
||
280 | break; |
||
281 | } |
||
282 | } |
||
283 | $keys = preg_grep( '/^((?:UNIQUE )?INDEX|(?:UNIQUE )?KEY)\s+/i', $sql ); |
||
284 | if ( !count( $keys ) ) |
||
285 | return $table; |
||
286 | foreach ( $keys as $idx => $key ) { |
||
287 | if ( 0 === strpos( $key, 'UNIQUE' ) ) |
||
288 | $is_unique = false; |
||
289 | else |
||
290 | $is_unique = true; |
||
291 | |||
292 | // for KEY `refresh` (`ip`,`time_last`) USING BTREE, |
||
293 | $key = preg_replace( '/ USING \S+ ?(,?)$/', '$1', $key ); |
||
294 | |||
295 | // for KEY `id` USING BTREE (`id`), |
||
296 | $key = preg_replace( '/` USING \S+ \(/i', '` (', $key ); |
||
297 | |||
298 | $key = preg_replace( '/^((?:UNIQUE )?INDEX|(?:UNIQUE )?KEY)\s+/i', '', $key ); |
||
299 | $key = preg_replace( '/\([0-9]+\)/', '', $key ); |
||
300 | preg_match( '/^`([^`]+)`\s+\(`(.+)`\),?$/', $key, $m ); |
||
301 | $key = $m[1]; //preg_replace( '/\([^)]+\)/', '', $m[1]); |
||
302 | if ( !$key ) |
||
303 | continue; |
||
304 | if ( $is_unique ) |
||
305 | $table->keys->$key = explode( '`,`', $m[2] ); |
||
306 | else |
||
307 | $table->uniques->$key = explode( '`,`', $m[2] ); |
||
308 | } |
||
309 | |||
310 | $uniques = get_object_vars( $table->uniques ); |
||
311 | foreach( $uniques as $idx => $val ) { |
||
312 | View Code Duplication | if ( is_array( $val ) && count( $val ) == 1 ) { |
|
313 | $pk_column_name = $val[0]; |
||
314 | switch( strtolower( $table->columns->$pk_column_name->type ) ) { |
||
315 | // Integers, exact value |
||
316 | case 'tinyint': |
||
317 | case 'smallint': |
||
318 | case 'int': |
||
319 | case 'integer': |
||
320 | case 'bigint': |
||
321 | // Fixed point, exact value |
||
322 | case 'decimal': |
||
323 | case 'numeric': |
||
324 | // Floating point, approximate value |
||
325 | case 'float': |
||
326 | case 'double': |
||
327 | case 'real': |
||
328 | // Date and Time |
||
329 | case 'date': |
||
330 | case 'datetime': |
||
331 | case 'timestamp': |
||
332 | $table->single_int_paging_column = $pk_column_name; |
||
333 | break; |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | |||
338 | if ( empty( $table->primary ) ) { |
||
339 | if ( !empty( $uniques ) ) |
||
340 | $table->primary = array_shift( $uniques ); |
||
341 | } |
||
342 | |||
343 | return $table; |
||
344 | } |
||
345 | |||
385 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: