| Conditions | 6 |
| Paths | 6 |
| Total Lines | 55 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| CRAP Score | 6.105 |
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 |
||
| 79 | 1 | public function getSQL() |
|
| 80 | { |
||
| 81 | 1 | if (count($this->records) === 0) { |
|
| 82 | throw new RuntimeException("no records added to this query"); |
||
| 83 | } |
||
| 84 | |||
| 85 | 1 | $table = "{$this->table}"; |
|
| 86 | |||
| 87 | /** @var Column[] $columns */ |
||
| 88 | |||
| 89 | 1 | $columns = array_filter( |
|
| 90 | 1 | $this->table->listColumns(), |
|
| 91 | function (Column $column) { |
||
| 92 | 1 | return $column->isAuto() === false; |
|
| 93 | 1 | } |
|
| 94 | ); |
||
| 95 | |||
| 96 | 1 | $quoted_column_names = implode( |
|
| 97 | 1 | ", ", |
|
| 98 | array_map( |
||
| 99 | 1 | function (Column $column) { |
|
| 100 | 1 | return $this->driver->quoteName($column->getName()); |
|
| 101 | 1 | }, |
|
| 102 | $columns |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | |||
| 106 | 1 | $tuples = []; |
|
| 107 | |||
| 108 | 1 | foreach ($this->records as $tuple_num => $record) { |
|
| 109 | 1 | $placeholders = []; |
|
| 110 | |||
| 111 | 1 | foreach ($columns as $col_index => $column) { |
|
| 112 | 1 | $name = $column->getName(); |
|
| 113 | |||
| 114 | 1 | if (array_key_exists($name, $record)) { |
|
| 115 | 1 | $value = $record[$name]; |
|
| 116 | } elseif ($column->isRequired() === false) { |
||
| 117 | $value = $column->getDefault(); |
||
| 118 | } else { |
||
| 119 | throw new RuntimeException("required value '{$name}' missing from tuple # {$tuple_num}"); |
||
| 120 | } |
||
| 121 | |||
| 122 | 1 | $placeholder = "c{$tuple_num}_{$col_index}"; |
|
| 123 | |||
| 124 | 1 | $placeholders[] = ":{$placeholder}"; |
|
| 125 | |||
| 126 | 1 | $this->bind($placeholder, $value, $column->getType()); |
|
| 127 | } |
||
| 128 | |||
| 129 | 1 | $tuples[] = "(" . implode(", ", $placeholders) . ")"; |
|
| 130 | } |
||
| 131 | |||
| 132 | 1 | return "INSERT INTO {$table} ({$quoted_column_names}) VALUES\n" . implode(",\n", $tuples); |
|
| 133 | } |
||
| 134 | } |
||
| 135 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: