| Conditions | 14 |
| Paths | 25 |
| Total Lines | 48 |
| Code Lines | 37 |
| 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 |
||
| 78 | public function parsePreparedParameters($parameters) |
||
| 79 | { |
||
| 80 | $values = array(); |
||
| 81 | foreach ($parameters as $value) { |
||
| 82 | $phpType = gettype($value); |
||
| 83 | $sqlType = null; |
||
| 84 | |||
| 85 | // Allow overriding of parameter type using an associative array |
||
| 86 | if ($phpType === 'array') { |
||
| 87 | $phpType = $value['type']; |
||
| 88 | $value = $value['value']; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Convert php variable type to one that makes mysqli_stmt_bind_param happy |
||
| 92 | // @see http://www.php.net/manual/en/mysqli-stmt.bind-param.php |
||
| 93 | switch ($phpType) { |
||
| 94 | case 'boolean': |
||
| 95 | case 'integer': |
||
| 96 | $sqlType = SQLITE3_INTEGER; |
||
| 97 | break; |
||
| 98 | case 'float': // Not actually returnable from gettype |
||
| 99 | case 'double': |
||
| 100 | $sqlType = SQLITE3_FLOAT; |
||
| 101 | break; |
||
| 102 | case 'object': // Allowed if the object or resource has a __toString method |
||
| 103 | case 'resource': |
||
| 104 | case 'string': |
||
| 105 | $sqlType = SQLITE3_TEXT; |
||
| 106 | break; |
||
| 107 | case 'NULL': |
||
| 108 | $sqlType = SQLITE3_NULL; |
||
| 109 | break; |
||
| 110 | case 'blob': |
||
| 111 | $sqlType = SQLITE3_BLOB; |
||
| 112 | break; |
||
| 113 | case 'array': |
||
| 114 | case 'unknown type': |
||
| 115 | default: |
||
| 116 | user_error("Cannot bind parameter \"$value\" as it is an unsupported type ($phpType)", E_USER_ERROR); |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | $values[] = array( |
||
| 120 | 'type' => $sqlType, |
||
| 121 | 'value' => $value |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | return $values; |
||
| 125 | } |
||
| 126 | |||
| 192 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: