| Conditions | 34 |
| Paths | 222 |
| Total Lines | 136 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 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 |
||
| 135 | final public function __execute($method, $arguments, $filepath) |
||
| 136 | { |
||
| 137 | $result = null; |
||
| 138 | |||
| 139 | try { |
||
| 140 | if (preg_match('/^(?:select|(?:dele|upda)te|insert)$/', $method)) { |
||
| 141 | $sql = $arguments[0]; |
||
| 142 | $bind = null; |
||
| 143 | if (array_key_exists(1, $arguments)) { |
||
| 144 | $bind = $arguments[1]; |
||
| 145 | } |
||
| 146 | |||
| 147 | if (is_string($sql)) { |
||
| 148 | if ($method !== 'select' && $this->isAutoCommit) { |
||
| 149 | $this->manager->beginTransaction(); |
||
| 150 | } |
||
| 151 | if (is_array($bind)) { |
||
| 152 | $result = $this->manager->query($sql, $bind)->{$method}(); |
||
| 153 | } else { |
||
| 154 | $result = $this->manager->query($sql)->{$method}(); |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | throw new DatabaseException("Invalid SQL or bind parameters: " . $sql .", " . strval($bind)); |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | $bind = null; |
||
| 161 | if (array_key_exists(0, $arguments)) { |
||
| 162 | $bind = $arguments[0]; |
||
| 163 | } |
||
| 164 | |||
| 165 | $trace = debug_backtrace(); |
||
| 166 | $modelMethod = null; |
||
| 167 | for ($i = 0; $i < count($trace); $i++) { |
||
| 168 | if ($this->inArray($trace[$i]["function"], ["__call", "__execute"])) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($trace[$i]["function"] !== null) { |
||
| 173 | $modelMethod = $trace[$i]["function"]; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | $namespace = substr($this->getNamespace($filepath), 1); |
||
| 179 | $queryKey = $namespace . "\\" . basename($filepath, ".php") . "#" . $modelMethod; |
||
| 180 | |||
| 181 | $query = null; |
||
| 182 | foreach ($this->queryAnnotations as $queryAnnotation) { |
||
| 183 | $queryFunctions = $queryAnnotation->get($queryKey); |
||
| 184 | |||
| 185 | if ($queryFunctions === null) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | foreach ($queryFunctions as $queryFunction) { |
||
| 190 | $xmlObjectList = $queryFunction->fetch(); |
||
| 191 | foreach ($xmlObjectList as $xmlObject) { |
||
| 192 | if ($xmlObject !== null) { |
||
| 193 | $xmlElement = $xmlObject->xpath("//mapper[@namespace='$namespace']/*[@id='$method']"); |
||
| 194 | |||
| 195 | if (!empty($xmlElement)) { |
||
| 196 | $query = ["sql" => trim($xmlElement[0]->__toString()), "method" => $xmlElement[0]->getName()]; |
||
| 197 | $entity = $xmlElement[0]->attributes()["entity"]; |
||
| 198 | $query["entity"] = $entity !== null ? $entity->__toString() : null; |
||
| 199 | break; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($query === null) { |
||
| 207 | throw new DatabaseException("SQL statement can't getting from xml file."); |
||
| 208 | } |
||
| 209 | |||
| 210 | $sql = $query["sql"]; |
||
| 211 | $method = $query["method"]; |
||
| 212 | $entityClassPath = $query["entity"]; |
||
| 213 | |||
| 214 | if ($entityClassPath !== null) { |
||
| 215 | if (!class_exists($entityClassPath)) { |
||
| 216 | throw new DatabaseException("Entity classpath is not found: " . $entityClassPath); |
||
| 217 | } |
||
| 218 | |||
| 219 | switch ($method) { |
||
| 220 | case "select": |
||
| 221 | if (is_string($sql)) { |
||
| 222 | if (is_array($bind)) { |
||
| 223 | $result = $this->manager->query($sql, $bind)->select()->toEntity($entityClassPath); |
||
| 224 | } else { |
||
| 225 | $result = $this->manager->query($sql)->select()->toEntity($entityClassPath); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | $errorMessage = "Invalid SQL or bind parameters: " . $sql; |
||
| 229 | if (is_array($bind)) { |
||
| 230 | $errorMessage .= ", " . strval($bind); |
||
| 231 | } |
||
| 232 | |||
| 233 | throw new DatabaseException($errorMessage); |
||
| 234 | } |
||
| 235 | |||
| 236 | break; |
||
| 237 | case "insert": |
||
| 238 | case "update": |
||
| 239 | case "delete": |
||
| 240 | // Not implement |
||
| 241 | throw new DatabaseException("Entity mapping is select only."); |
||
| 242 | } |
||
| 243 | } else { |
||
| 244 | if (is_string($sql)) { |
||
| 245 | if ($method !== 'select' && $this->isAutoCommit) { |
||
| 246 | $this->manager->beginTransaction(); |
||
| 247 | } |
||
| 248 | if (is_array($bind)) { |
||
| 249 | $result = $this->manager->query($sql, $bind)->{$method}(); |
||
| 250 | } else { |
||
| 251 | $result = $this->manager->query($sql)->{$method}(); |
||
| 252 | } |
||
| 253 | } else { |
||
| 254 | $errorMessage = "Invalid SQL or bind parameters: " . $sql; |
||
| 255 | if (is_array($bind)) { |
||
| 256 | $errorMessage .= ", " . strval($bind); |
||
| 257 | } |
||
| 258 | |||
| 259 | throw new DatabaseException($errorMessage); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } catch (DatabaseException $e) { |
||
| 264 | $this->manager->rollback(); |
||
| 265 | $this->manager->disconnect(); |
||
| 266 | throw $e; |
||
| 267 | } |
||
| 268 | |||
| 269 | return $result; |
||
| 270 | } |
||
| 271 | |||
| 308 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.