| Conditions | 17 |
| Paths | 77 |
| Total Lines | 73 |
| Code Lines | 42 |
| 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 |
||
| 109 | public function exec($querystring, $types = NULL, &...$arguments) { |
||
| 110 | // log exact query to audit log, if it's not a SELECT |
||
| 111 | $isMoreThanSelect = FALSE; |
||
| 112 | if (preg_match("/^SELECT/i", $querystring) == 0 && preg_match("/^DESC/i", $querystring) == 0) { |
||
| 113 | $isMoreThanSelect = TRUE; |
||
| 114 | if ($this->readOnly) { // let's not do this. |
||
| 115 | throw new Exception("This is a read-only DB connection, but this is statement is not a SELECT!"); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | // log exact query to debug log, if log level is at 5 |
||
| 119 | $this->loggerInstance->debug(5, "DB ATTEMPT: " . $querystring . "\n"); |
||
| 120 | if ($types !== NULL) { |
||
| 121 | $this->loggerInstance->debug(5, "Argument type sequence: $types, parameters are: " . print_r($arguments, true)); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($this->connection->connect_error) { |
||
| 125 | throw new Exception("ERROR: Cannot send query to $this->databaseInstance database (no connection, error number" . $this->connection->connect_error . ")!"); |
||
| 126 | } |
||
| 127 | if ($types === NULL) { |
||
| 128 | $result = $this->connection->query($querystring); |
||
| 129 | if ($result === FALSE) { |
||
| 130 | throw new Exception("DB: Unable to execute simple statement! Error was --> " . $this->connection->error . " <--"); |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | // fancy! prepared statement with dedicated argument list |
||
| 134 | if (strlen($types) != count($arguments)) { |
||
| 135 | throw new Exception("DB: Prepared Statement: Number of arguments and the type list length differ!"); |
||
| 136 | } |
||
| 137 | $statementObject = $this->connection->stmt_init(); |
||
| 138 | if ($statementObject === FALSE) { |
||
| 139 | throw new Exception("DB: Unable to initialise prepared Statement!"); |
||
| 140 | } |
||
| 141 | $prepResult = $statementObject->prepare($querystring); |
||
| 142 | if ($prepResult === FALSE) { |
||
| 143 | throw new Exception("DB: Unable to prepare statement! Statement was --> $querystring <--, error was --> " . $statementObject->error . " <--."); |
||
| 144 | } |
||
| 145 | |||
| 146 | // we have a variable number of arguments packed into the ... array |
||
| 147 | // but the function needs to be called exactly once, with a series of |
||
| 148 | // individual arguments, not an array. The voodoo solution is to call |
||
| 149 | // it via call_user_func_array() |
||
| 150 | |||
| 151 | $localArray = $arguments; |
||
| 152 | array_unshift($localArray, $types); |
||
| 153 | $retval = call_user_func_array([$statementObject, "bind_param"], $localArray); |
||
| 154 | if ($retval === FALSE) { |
||
| 155 | throw new Exception("DB: Unable to bind parameters to prepared statement! Argument array was --> " . var_export($localArray, TRUE) . " <--. Error was --> " . $statementObject->error . " <--"); |
||
| 156 | } |
||
| 157 | $result = $statementObject->execute(); |
||
| 158 | if ($result === FALSE) { |
||
| 159 | throw new Exception("DB: Unable to execute prepared statement! Error was --> " . $statementObject->error . " <--"); |
||
| 160 | } |
||
| 161 | $selectResult = $statementObject->get_result(); |
||
| 162 | if ($selectResult !== FALSE) { |
||
| 163 | $result = $selectResult; |
||
| 164 | } |
||
| 165 | |||
| 166 | $statementObject->close(); |
||
| 167 | } |
||
| 168 | |||
| 169 | // all cases where $result could be FALSE have been caught earlier |
||
| 170 | if ($this->connection->errno) { |
||
| 171 | throw new Exception("ERROR: Cannot execute query in $this->databaseInstance database - (hopefully escaped) query was '$querystring', errno was " . $this->connection->errno . "!"); |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | if ($isMoreThanSelect) { |
||
| 176 | $this->loggerInstance->writeSQLAudit("[DB: " . strtoupper($this->databaseInstance) . "] " . $querystring); |
||
| 177 | if ($types !== NULL) { |
||
| 178 | $this->loggerInstance->writeSQLAudit("Argument type sequence: $types, parameters are: " . print_r($arguments, true)); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return $result; |
||
| 182 | } |
||
| 283 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.