Complex classes like Query 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Query implements QueryInterface |
||
| 18 | { |
||
| 19 | /* @var \PDO The PDO. */ |
||
| 20 | private $pdo; |
||
| 21 | |||
| 22 | /* @var array The bindings. */ |
||
| 23 | private $bindings; |
||
| 24 | |||
| 25 | /* @var QueryBuilder The query builder. */ |
||
| 26 | private $queryBuilder; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Construct a query object with the given pdo and table. |
||
| 30 | * |
||
| 31 | * @param \PDO $pdo |
||
| 32 | * @param string $table |
||
| 33 | */ |
||
| 34 | 21 | public function __construct(\PDO $pdo, $table) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Returns a string representation of the query object. |
||
| 43 | * |
||
| 44 | * @return string a string representation of the query object. |
||
| 45 | */ |
||
| 46 | 21 | public function __toString() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | 17 | public function select($columns = ['*']) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 1 | public function insert(array $values) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | 1 | public function update(array $values) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 1 | public function delete() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 1 | public function join($table, $primary, $operator, $secondary) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | 1 | public function leftJoin($table, $primary, $operator, $secondary) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 1 | public function rightJoin($table, $primary, $operator, $secondary) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 1 | public function crossJoin($table, $primary, $operator, $secondary) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 7 | public function where(QueryExpression $exp) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 1 | public function groupBy($column) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 1 | public function orderBy($column, $order = null) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | 2 | public function limit($limit) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 1 | public function offset($offset) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the result of the executed prepared query. |
||
| 189 | * |
||
| 190 | * @return QueryResult the result of the executed prepared query. |
||
| 191 | */ |
||
| 192 | 4 | public function execute() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the data type of the given value. |
||
| 209 | * |
||
| 210 | * @param mixed $value |
||
| 211 | * @return int the data type of the given value. |
||
| 212 | */ |
||
| 213 | 1 | private function getPdoDataType($value) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Returns a binding for the given clause and value. |
||
| 230 | * |
||
| 231 | * @param string $clause |
||
| 232 | * @param string $value |
||
| 233 | * @return string a binding for the given clause and value. |
||
| 234 | */ |
||
| 235 | 10 | public function addBinding($clause, $value) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Returns bindings for the given clause and values. |
||
| 244 | * |
||
| 245 | * @param string $clause |
||
| 246 | * @param array $values |
||
| 247 | * @return array bindings for the given clause and values. |
||
| 248 | */ |
||
| 249 | 3 | public function addBindings($clause, array $values) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Returns a binding for the given clause and value. |
||
| 262 | * |
||
| 263 | * @param string $clause |
||
| 264 | * @param string $value |
||
| 265 | * @return string a binding for the given clause and value. |
||
| 266 | */ |
||
| 267 | 2 | private function setBinding($clause, $value) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Returns bindings for the given clause and values. |
||
| 274 | * |
||
| 275 | * @param string $clause |
||
| 276 | * @param array $values |
||
| 277 | * @return array bindings for the given clause and values. |
||
| 278 | */ |
||
| 279 | 2 | private function setBindings($clause, array $values) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Remove the bindings that are associated with the given clause. |
||
| 286 | * |
||
| 287 | * @param string $clause |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | 4 | private function removeBindings($clause) |
|
| 296 | |||
| 297 | public static function Greater($left, $right) |
||
| 301 | |||
| 302 | public static function GreaterOrEqual($left, $right) |
||
| 306 | |||
| 307 | public static function Lesser($left, $right) |
||
| 311 | |||
| 312 | public static function LessOrEqual($left, $right) |
||
| 316 | |||
| 317 | 7 | public static function Equal($left, $right) |
|
| 321 | |||
| 322 | public static function NotEqual($left, $right) |
||
| 326 | |||
| 327 | 1 | public static function NotLike($left, $right) |
|
| 331 | |||
| 332 | 6 | public static function Like($left, $right) |
|
| 336 | |||
| 337 | public static function Is($left, $right) |
||
| 341 | |||
| 342 | 1 | public static function IsNot($left, $right) |
|
| 346 | |||
| 347 | 3 | public static function In($needle, $haystack) |
|
| 351 | |||
| 352 | // Predicate logic |
||
| 353 | 3 | public static function And(QueryExpression $left, QueryExpression ...$others) |
|
| 357 | |||
| 358 | public static function Or(QueryExpression $left, QueryExpression ...$others) |
||
| 362 | |||
| 363 | public static function Not(QueryExpression $exp) |
||
| 367 | } |
||
| 368 |