Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class QueryBuilder |
||
| 6 | { |
||
| 7 | const TYPE_INSERT = 1; |
||
| 8 | const TYPE_SELECT = 2; |
||
| 9 | const TYPE_UPDATE = 3; |
||
| 10 | const TYPE_DELETE = 4; |
||
| 11 | const TYPE_REPLACE = 5; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Connection |
||
| 15 | */ |
||
| 16 | private $conn; |
||
| 17 | |||
| 18 | private $type; |
||
| 19 | |||
| 20 | private $sqlParts = array( |
||
| 21 | 'select' => array(), |
||
| 22 | 'from' => array(), |
||
| 23 | 'where' => array(), |
||
| 24 | 'groupBy' => array(), |
||
| 25 | 'groupByLimit' => null, |
||
| 26 | 'withinGroupOrderBy' => null, |
||
| 27 | 'orderBy' => array(), |
||
| 28 | 'facet' => array(), |
||
| 29 | 'resultSetNames' => array(0), |
||
| 30 | 'set' => array(), |
||
| 31 | 'values' => array(), |
||
| 32 | 'options' => array(), |
||
| 33 | 'firstResult' => 0, |
||
| 34 | 'maxResults' => null, |
||
| 35 | ); |
||
| 36 | |||
| 37 | private static $multipleParts = array( |
||
| 38 | 'select' => true, |
||
| 39 | 'from' => true, |
||
| 40 | 'where' => true, |
||
| 41 | 'groupBy' => true, |
||
| 42 | 'groupByLimit' => false, |
||
| 43 | 'withinGroupOrderBy' => false, |
||
| 44 | 'orderBy' => true, |
||
| 45 | 'facet' => true, |
||
| 46 | 'resultSetNames' => true, |
||
| 47 | 'set' => true, |
||
| 48 | 'values' => true, |
||
| 49 | 'options' => true, |
||
| 50 | 'firstResult' => false, |
||
| 51 | 'maxResults' => false, |
||
| 52 | ); |
||
| 53 | |||
| 54 | private $isDirty = true; |
||
| 55 | |||
| 56 | private $sql; |
||
| 57 | |||
| 58 | private $parameters = array(); |
||
| 59 | |||
| 60 | private $parametersCounter = 0; |
||
| 61 | |||
| 62 | 78 | public function __construct(Connection $conn) |
|
| 66 | |||
| 67 | public function getEscaper() |
||
| 71 | |||
| 72 | 57 | public function select($select = null) |
|
| 81 | |||
| 82 | 9 | public function addSelect($select) |
|
| 88 | |||
| 89 | 6 | public function update($index) |
|
| 95 | |||
| 96 | 6 | public function insert($index) |
|
| 102 | |||
| 103 | 3 | public function replace($index) |
|
| 109 | |||
| 110 | 3 | public function delete($index) |
|
| 116 | |||
| 117 | 6 | public function set($key, $value) |
|
| 121 | |||
| 122 | 9 | public function values(array $values) |
|
| 126 | |||
| 127 | 3 | public function addValues(array $values) |
|
| 131 | |||
| 132 | 57 | public function from($index) |
|
| 136 | |||
| 137 | 3 | public function addFrom($index) |
|
| 141 | |||
| 142 | 39 | public function where($where) |
|
| 146 | |||
| 147 | 9 | public function andWhere($where) |
|
| 151 | |||
| 152 | 12 | public function groupBy($groupBy, $limit = null) |
|
| 158 | |||
| 159 | 3 | public function addGroupBy($groupBy) |
|
| 163 | |||
| 164 | 3 | public function withinGroupOrderBy($order, $direction = null) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param string|array $facet 'brand_id', or array('brand_id', 'year') or array('brand_id' => 'brand', 'year') |
||
| 171 | * @param string $by |
||
| 172 | * @param string $order |
||
| 173 | * @param string $direction |
||
| 174 | * @param int $limit |
||
| 175 | * @param int $skip |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | 3 | public function facet($facet, $by = null, $order = null, $direction = null, $limit = null, $skip = 0) |
|
| 185 | |||
| 186 | public function nameResultSet($name) |
||
| 190 | |||
| 191 | 9 | public function orderBy($order, $direction = null) |
|
| 195 | |||
| 196 | 6 | public function addOrderBy($order, $direction = null) |
|
| 200 | |||
| 201 | 6 | public function setOption($name, $value) |
|
| 205 | |||
| 206 | 6 | public function setMaxResults($limit) |
|
| 210 | |||
| 211 | 3 | public function setFirstResult($skip) |
|
| 215 | |||
| 216 | 9 | public function setParameter($parameter, $value) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Creates a new named parameter and bind the value $value to it. |
||
| 225 | * |
||
| 226 | * @param string $value |
||
| 227 | * @param string $prefix The name to bind with. |
||
| 228 | * |
||
| 229 | * @return string the placeholder name used. |
||
| 230 | */ |
||
| 231 | 6 | public function createParameter($value, $prefix = 'gen_') |
|
| 239 | |||
| 240 | 9 | public function getParameters() |
|
| 244 | |||
| 245 | public function execute() |
||
| 249 | |||
| 250 | public function getResult() |
||
| 254 | |||
| 255 | public function getMultiResult() |
||
| 259 | |||
| 260 | 75 | public function getSql() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Either appends to or replaces a single, generic query part. |
||
| 292 | * |
||
| 293 | * @param string $sqlPartName |
||
| 294 | * @param string|array $sqlPart |
||
| 295 | * @param bool $append |
||
| 296 | * |
||
| 297 | * @return $this This QueryBuilder instance. |
||
| 298 | */ |
||
| 299 | 75 | protected function add($sqlPartName, $sqlPart, $append = false) |
|
| 315 | |||
| 316 | 57 | protected function buildSqlForSelect() |
|
| 349 | |||
| 350 | 9 | protected function buildSqlForInsert() |
|
| 367 | |||
| 368 | 6 | protected function buildSqlForUpdate() |
|
| 380 | |||
| 381 | 3 | protected function buildSqlForDelete() |
|
| 388 | |||
| 389 | 66 | protected function buildWherePart() |
|
| 397 | |||
| 398 | 57 | protected function buildGroupByPart() |
|
| 413 | |||
| 414 | 57 | protected function buildOrderByPart() |
|
| 427 | |||
| 428 | 57 | protected function buildOptionsPart() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Build FACET {expr_list} [BY {expr_list}] [ORDER BY {expr | FACET()} {ASC | DESC}] [LIMIT [offset,] count] |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 57 | protected function buildFacetPart() |
|
| 479 | |||
| 480 | 15 | protected function getDirection($order, $direction) |
|
| 492 | } |
||
| 493 |