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 |
||
99 | public function exec($querystring, $types = NULL, &...$arguments) { |
||
100 | // log exact query to audit log, if it's not a SELECT |
||
101 | $isMoreThanSelect = FALSE; |
||
102 | if (preg_match("/^SELECT/i", $querystring) == 0 && preg_match("/^DESC/i", $querystring) == 0) { |
||
103 | $isMoreThanSelect = TRUE; |
||
104 | if ($this->readOnly) { // let's not do this. |
||
105 | throw new Exception("This is a read-only DB connection, but this is statement is not a SELECT!"); |
||
106 | } |
||
107 | } |
||
108 | // log exact query to debug log, if log level is at 5 |
||
109 | $this->loggerInstance->debug(5, "DB ATTEMPT: " . $querystring . "\n"); |
||
110 | if ($types !== NULL) { |
||
111 | $this->loggerInstance->debug(5, "Argument type sequence: $types, parameters are: " . print_r($arguments, true)); |
||
112 | } |
||
113 | |||
114 | if ($this->connection->connect_error) { |
||
115 | throw new Exception("ERROR: Cannot send query to $this->databaseInstance database (no connection, error number" . $this->connection->connect_error . ")!"); |
||
116 | } |
||
117 | if ($types === NULL) { |
||
118 | $result = $this->connection->query($querystring); |
||
119 | if ($result === FALSE) { |
||
120 | throw new Exception("DB: Unable to execute simple statement! Error was --> " . $this->connection->error . " <--"); |
||
121 | } |
||
122 | } else { |
||
123 | // fancy! prepared statement with dedicated argument list |
||
124 | if (strlen($types) != count($arguments)) { |
||
125 | throw new Exception("DB: Prepared Statement: Number of arguments and the type list length differ!"); |
||
126 | } |
||
127 | $statementObject = $this->connection->stmt_init(); |
||
128 | if ($statementObject === FALSE) { |
||
129 | throw new Exception("DB: Unable to initialise prepared Statement!"); |
||
130 | } |
||
131 | $prepResult = $statementObject->prepare($querystring); |
||
132 | if ($prepResult === FALSE) { |
||
133 | throw new Exception("DB: Unable to prepare statement! Statement was --> $querystring <--, error was --> " . $statementObject->error . " <--."); |
||
134 | } |
||
135 | |||
136 | // we have a variable number of arguments packed into the ... array |
||
137 | // but the function needs to be called exactly once, with a series of |
||
138 | // individual arguments, not an array. The voodoo solution is to call |
||
139 | // it via call_user_func_array() |
||
140 | |||
141 | $localArray = $arguments; |
||
142 | array_unshift($localArray, $types); |
||
143 | $retval = call_user_func_array([$statementObject, "bind_param"], $localArray); |
||
144 | if ($retval === FALSE) { |
||
145 | throw new Exception("DB: Unable to bind parameters to prepared statement! Argument array was --> " . var_export($localArray, TRUE) . " <--. Error was --> " . $statementObject->error . " <--"); |
||
146 | } |
||
147 | $result = $statementObject->execute(); |
||
148 | if ($result === FALSE) { |
||
149 | throw new Exception("DB: Unable to execute prepared statement! Error was --> " . $statementObject->error . " <--"); |
||
150 | } |
||
151 | $selectResult = $statementObject->get_result(); |
||
152 | if ($selectResult !== FALSE) { |
||
153 | $result = $selectResult; |
||
154 | } |
||
155 | |||
156 | $statementObject->close(); |
||
157 | } |
||
158 | |||
159 | // all cases where $result could be FALSE have been caught earlier |
||
160 | if ($this->connection->errno) { |
||
161 | throw new Exception("ERROR: Cannot execute query in $this->databaseInstance database - (hopefully escaped) query was '$querystring', errno was " . $this->connection->errno . "!"); |
||
162 | } |
||
163 | |||
164 | |||
165 | if ($isMoreThanSelect) { |
||
166 | $this->loggerInstance->writeSQLAudit("[DB: " . strtoupper($this->databaseInstance) . "] " . $querystring); |
||
167 | if ($types !== NULL) { |
||
168 | $this->loggerInstance->writeSQLAudit("Argument type sequence: $types, parameters are: " . print_r($arguments, true)); |
||
169 | } |
||
170 | } |
||
171 | return $result; |
||
172 | } |
||
262 |