| Total Complexity | 54 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TTableGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TTableGateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 82 | class TTableGateway extends \Prado\TComponent |
||
| 83 | { |
||
| 84 | private $_command; |
||
| 85 | private $_connection; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Creates a new generic table gateway for a given table or view name |
||
|
|
|||
| 89 | * and a database connection. |
||
| 90 | * @param string|TDbTableInfo table or view name or table information. |
||
| 91 | * @param TDbConnection database connection. |
||
| 92 | */ |
||
| 93 | public function __construct($table, $connection) |
||
| 94 | { |
||
| 95 | $this->_connection = $connection; |
||
| 96 | if(is_string($table)) |
||
| 97 | $this->setTableName($table); |
||
| 98 | elseif($table instanceof TDbTableInfo) |
||
| 99 | $this->setTableInfo($table); |
||
| 100 | else |
||
| 101 | throw new TDbException('dbtablegateway_invalid_table_info'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param TDbTableInfo table or view information. |
||
| 106 | */ |
||
| 107 | protected function setTableInfo($tableInfo) |
||
| 108 | { |
||
| 109 | $builder = $tableInfo->createCommandBuilder($this->getDbConnection()); |
||
| 110 | $this->initCommandBuilder($builder); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets up the command builder for the given table. |
||
| 115 | * @param string table or view name. |
||
| 116 | */ |
||
| 117 | protected function setTableName($tableName) |
||
| 118 | { |
||
| 119 | $meta = TDbMetaData::getInstance($this->getDbConnection()); |
||
| 120 | $this->initCommandBuilder($meta->createCommandBuilder($tableName)); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getTableInfo() |
||
| 124 | { |
||
| 125 | return $this->getCommand()->getTableInfo(); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getTableName() |
||
| 129 | { |
||
| 130 | return $this->getTableInfo()->getTableName(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param TDbCommandBuilder database specific command builder. |
||
| 135 | */ |
||
| 136 | protected function initCommandBuilder($builder) |
||
| 137 | { |
||
| 138 | $this->_command = new TDataGatewayCommand($builder); |
||
| 139 | $this->_command->OnCreateCommand[] = [$this, 'onCreateCommand']; |
||
| 140 | $this->_command->OnExecuteCommand[] = [$this, 'onExecuteCommand']; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Raised when a command is prepared and parameter binding is completed. |
||
| 145 | * The parameter object is TDataGatewayEventParameter of which the |
||
| 146 | * {@link TDataGatewayEventParameter::getCommand Command} property can be |
||
| 147 | * inspected to obtain the sql query to be executed. |
||
| 148 | * @param TDataGatewayCommand originator $sender |
||
| 149 | * @param TDataGatewayEventParameter |
||
| 150 | */ |
||
| 151 | public function onCreateCommand($sender, $param) |
||
| 152 | { |
||
| 153 | $this->raiseEvent('OnCreateCommand', $this, $param); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Raised when a command is executed and the result from the database was returned. |
||
| 158 | * The parameter object is TDataGatewayResultEventParameter of which the |
||
| 159 | * {@link TDataGatewayEventParameter::getResult Result} property contains |
||
| 160 | * the data return from the database. The data returned can be changed |
||
| 161 | * by setting the {@link TDataGatewayEventParameter::setResult Result} property. |
||
| 162 | * @param TDataGatewayCommand originator $sender |
||
| 163 | * @param TDataGatewayResultEventParameter |
||
| 164 | */ |
||
| 165 | public function onExecuteCommand($sender, $param) |
||
| 166 | { |
||
| 167 | $this->raiseEvent('OnExecuteCommand', $this, $param); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return TDataGatewayCommand command builder and executor. |
||
| 172 | */ |
||
| 173 | protected function getCommand() |
||
| 174 | { |
||
| 175 | return $this->_command; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return TDbConnection database connection. |
||
| 180 | */ |
||
| 181 | public function getDbConnection() |
||
| 182 | { |
||
| 183 | return $this->_connection; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Execute arbituary sql command with binding parameters. |
||
| 188 | * @param string SQL query string. |
||
| 189 | * @param array binding parameters, positional or named. |
||
| 190 | * @return array query results. |
||
| 191 | */ |
||
| 192 | public function findBySql($sql, $parameters = []) |
||
| 193 | { |
||
| 194 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 195 | $criteria = $this->getCriteria($sql, $parameters, $args); |
||
| 196 | return $this->getCommand()->findBySql($criteria); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Execute arbituary sql command with binding parameters. |
||
| 201 | * @param string SQL query string. |
||
| 202 | * @param array binding parameters, positional or named. |
||
| 203 | * @return TDbDataReader query results. |
||
| 204 | */ |
||
| 205 | public function findAllBySql($sql, $parameters = []) |
||
| 206 | { |
||
| 207 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 208 | $criteria = $this->getCriteria($sql, $parameters, $args); |
||
| 209 | return $this->getCommand()->findAllBySql($criteria); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Find one single record that matches the criteria. |
||
| 214 | * |
||
| 215 | * Usage: |
||
| 216 | * <code> |
||
| 217 | * $table->find('username = :name AND password = :pass', |
||
| 218 | * array(':name'=>$name, ':pass'=>$pass)); |
||
| 219 | * $table->find('username = ? AND password = ?', array($name, $pass)); |
||
| 220 | * $table->find('username = ? AND password = ?', $name, $pass); |
||
| 221 | * //$criteria is of TSqlCriteria |
||
| 222 | * $table->find($criteria); //the 2nd parameter for find() is ignored. |
||
| 223 | * </code> |
||
| 224 | * |
||
| 225 | * @param string|TSqlCriteria SQL condition or criteria object. |
||
| 226 | * @param mixed parameter values. |
||
| 227 | * @return array matching record object. |
||
| 228 | */ |
||
| 229 | public function find($criteria, $parameters = []) |
||
| 230 | { |
||
| 231 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 232 | $criteria = $this->getCriteria($criteria, $parameters, $args); |
||
| 233 | return $this->getCommand()->find($criteria); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Accepts same parameters as find(), but returns TDbDataReader instead. |
||
| 238 | * @param string|TSqlCriteria SQL condition or criteria object. |
||
| 239 | * @param mixed parameter values. |
||
| 240 | * @return TDbDataReader matching records. |
||
| 241 | */ |
||
| 242 | public function findAll($criteria = null, $parameters = []) |
||
| 243 | { |
||
| 244 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 245 | if($criteria !== null) |
||
| 246 | $criteria = $this->getCriteria($criteria, $parameters, $args); |
||
| 247 | return $this->getCommand()->findAll($criteria); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Find one record using only the primary key or composite primary keys. Usage: |
||
| 252 | * |
||
| 253 | * <code> |
||
| 254 | * $table->findByPk($primaryKey); |
||
| 255 | * $table->findByPk($key1, $key2, ...); |
||
| 256 | * $table->findByPk(array($key1,$key2,...)); |
||
| 257 | * </code> |
||
| 258 | * |
||
| 259 | * @param mixed primary keys |
||
| 260 | * @return array matching record. |
||
| 261 | */ |
||
| 262 | public function findByPk($keys) |
||
| 263 | { |
||
| 264 | if(func_num_args() > 1) |
||
| 265 | $keys = func_get_args(); |
||
| 266 | return $this->getCommand()->findByPk($keys); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Similar to findByPk(), but returns TDbDataReader instead. |
||
| 271 | * |
||
| 272 | * For scalar primary keys: |
||
| 273 | * <code> |
||
| 274 | * $table->findAllByPk($key1, $key2, ...); |
||
| 275 | * $table->findAllByPk(array($key1, $key2, ...)); |
||
| 276 | * </code> |
||
| 277 | * |
||
| 278 | * For composite keys: |
||
| 279 | * <code> |
||
| 280 | * $table->findAllByPk(array($key1, $key2), array($key3, $key4), ...); |
||
| 281 | * $table->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...)); |
||
| 282 | * </code> |
||
| 283 | * @param mixed primary keys |
||
| 284 | * @return TDbDataReader data reader. |
||
| 285 | */ |
||
| 286 | public function findAllByPks($keys) |
||
| 287 | { |
||
| 288 | if(func_num_args() > 1) |
||
| 289 | $keys = func_get_args(); |
||
| 290 | return $this->getCommand()->findAllByPk($keys); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Delete records from the table with condition given by $where and |
||
| 295 | * binding values specified by $parameter argument. |
||
| 296 | * This method uses additional arguments as $parameters. E.g. |
||
| 297 | * <code> |
||
| 298 | * $table->delete('age > ? AND location = ?', $age, $location); |
||
| 299 | * </code> |
||
| 300 | * @param string delete condition. |
||
| 301 | * @param array condition parameters. |
||
| 302 | * @return integer number of records deleted. |
||
| 303 | */ |
||
| 304 | public function deleteAll($criteria, $parameters = []) |
||
| 305 | { |
||
| 306 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 307 | $criteria = $this->getCriteria($criteria, $parameters, $args); |
||
| 308 | return $this->getCommand()->delete($criteria); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Delete records by primary key. Usage: |
||
| 313 | * |
||
| 314 | * <code> |
||
| 315 | * $table->deleteByPk($primaryKey); //delete 1 record |
||
| 316 | * $table->deleteByPk($key1,$key2,...); //delete multiple records |
||
| 317 | * $table->deleteByPk(array($key1,$key2,...)); //delete multiple records |
||
| 318 | * </code> |
||
| 319 | * |
||
| 320 | * For composite primary keys (determined from the table definitions): |
||
| 321 | * <code> |
||
| 322 | * $table->deleteByPk(array($key1,$key2)); //delete 1 record |
||
| 323 | * |
||
| 324 | * //delete multiple records |
||
| 325 | * $table->deleteByPk(array($key1,$key2), array($key3,$key4),...); |
||
| 326 | * |
||
| 327 | * //delete multiple records |
||
| 328 | * $table->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. )); |
||
| 329 | * </code> |
||
| 330 | * |
||
| 331 | * @param mixed primary key values. |
||
| 332 | * @return int number of records deleted. |
||
| 333 | */ |
||
| 334 | public function deleteByPk($keys) |
||
| 335 | { |
||
| 336 | if(func_num_args() > 1) |
||
| 337 | $keys = func_get_args(); |
||
| 338 | return $this->getCommand()->deleteByPk($keys); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Alias for deleteByPk() |
||
| 343 | */ |
||
| 344 | public function deleteAllByPks($keys) |
||
| 345 | { |
||
| 346 | if(func_num_args() > 1) |
||
| 347 | $keys = func_get_args(); |
||
| 348 | return $this->deleteByPk($keys); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Find the number of records. |
||
| 353 | * @param string|TSqlCriteria SQL condition or criteria object. |
||
| 354 | * @param mixed parameter values. |
||
| 355 | * @return int number of records. |
||
| 356 | */ |
||
| 357 | public function count($criteria = null, $parameters = []) |
||
| 358 | { |
||
| 359 | $args = func_num_args() > 1 ? array_slice(func_get_args(), 1) : null; |
||
| 360 | if($criteria !== null) |
||
| 361 | $criteria = $this->getCriteria($criteria, $parameters, $args); |
||
| 362 | return $this->getCommand()->count($criteria); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Updates the table with new name-value pair $data. Each array key must |
||
| 367 | * correspond to a column name in the table. The update condition is |
||
| 368 | * specified by the $where argument and additional binding values can be |
||
| 369 | * specified using the $parameter argument. |
||
| 370 | * This method uses additional arguments as $parameters. E.g. |
||
| 371 | * <code> |
||
| 372 | * $gateway->update($data, 'age > ? AND location = ?', $age, $location); |
||
| 373 | * </code> |
||
| 374 | * @param array new record data. |
||
| 375 | * @param string update condition |
||
| 376 | * @param array additional binding name-value pairs. |
||
| 377 | * @return integer number of records updated. |
||
| 378 | */ |
||
| 379 | public function update($data, $criteria, $parameters = []) |
||
| 380 | { |
||
| 381 | $args = func_num_args() > 2 ? array_slice(func_get_args(), 2) : null; |
||
| 382 | $criteria = $this->getCriteria($criteria, $parameters, $args); |
||
| 383 | return $this->getCommand()->update($data, $criteria); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Inserts a new record into the table. Each array key must |
||
| 388 | * correspond to a column name in the table unless a null value is permitted. |
||
| 389 | * @param array new record data. |
||
| 390 | * @return mixed last insert id if one column contains a serial or sequence, |
||
| 391 | * otherwise true if command executes successfully and affected 1 or more rows. |
||
| 392 | */ |
||
| 393 | public function insert($data) |
||
| 394 | { |
||
| 395 | return $this->getCommand()->insert($data); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return mixed last insert id, null if none is found. |
||
| 400 | */ |
||
| 401 | public function getLastInsertId() |
||
| 402 | { |
||
| 403 | return $this->getCommand()->getLastInsertId(); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Create a new TSqlCriteria object from a string $criteria. The $args |
||
| 408 | * are additional parameters and are used in place of the $parameters |
||
| 409 | * if $parameters is not an array and $args is an arrary. |
||
| 410 | * @param string|TSqlCriteria sql criteria |
||
| 411 | * @param mixed parameters passed by the user. |
||
| 412 | * @param array additional parameters obtained from function_get_args(). |
||
| 413 | * @return TSqlCriteria criteria object. |
||
| 414 | */ |
||
| 415 | protected function getCriteria($criteria, $parameters, $args) |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Dynamic find method using parts of method name as search criteria. |
||
| 430 | * Method name starting with "findBy" only returns 1 record. |
||
| 431 | * Method name starting with "findAllBy" returns 0 or more records. |
||
| 432 | * Method name starting with "deleteBy" deletes records by the trail criteria. |
||
| 433 | * The condition is taken as part of the method name after "findBy", "findAllBy" |
||
| 434 | * or "deleteBy". |
||
| 435 | * |
||
| 436 | * The following are equivalent: |
||
| 437 | * <code> |
||
| 438 | * $table->findByName($name) |
||
| 439 | * $table->find('Name = ?', $name); |
||
| 440 | * </code> |
||
| 441 | * <code> |
||
| 442 | * $table->findByUsernameAndPassword($name,$pass); // OR may be used |
||
| 443 | * $table->findBy_Username_And_Password($name,$pass); // _OR_ may be used |
||
| 444 | * $table->find('Username = ? AND Password = ?', $name, $pass); |
||
| 445 | * </code> |
||
| 446 | * <code> |
||
| 447 | * $table->findAllByAge($age); |
||
| 448 | * $table->findAll('Age = ?', $age); |
||
| 449 | * </code> |
||
| 450 | * <code> |
||
| 451 | * $table->deleteAll('Name = ?', $name); |
||
| 452 | * $table->deleteByName($name); |
||
| 453 | * </code> |
||
| 454 | * @return mixed single record if method name starts with "findBy", 0 or more records |
||
| 455 | * if method name starts with "findAllBy" |
||
| 456 | */ |
||
| 457 | public function __call($method, $args) |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths