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 |
||
| 27 | class QueryBuilder extends Parenthesis implements QueryBuilderInterface |
||
| 28 | { |
||
| 29 | /** The table to query |
||
| 30 | * @var string */ |
||
| 31 | protected $tableName = ''; |
||
| 32 | |||
| 33 | /** The alias of the main table |
||
| 34 | * @var string */ |
||
| 35 | protected $alias = ''; |
||
| 36 | |||
| 37 | /** Columns to fetch (null is equal to ['*']) |
||
| 38 | * @var array|null */ |
||
| 39 | protected $columns = null; |
||
| 40 | |||
| 41 | /** Joins get concatenated with space |
||
| 42 | * @var string[] */ |
||
| 43 | protected $joins = []; |
||
| 44 | |||
| 45 | /** Limit amount of rows |
||
| 46 | * @var int */ |
||
| 47 | protected $limit; |
||
| 48 | |||
| 49 | /** Offset to start from |
||
| 50 | * @var int */ |
||
| 51 | protected $offset; |
||
| 52 | |||
| 53 | /** Group by conditions get concatenated with comma |
||
| 54 | * @var string[] */ |
||
| 55 | protected $groupBy = []; |
||
| 56 | |||
| 57 | /** Order by conditions get concatenated with comma |
||
| 58 | * @var string[] */ |
||
| 59 | protected $orderBy = []; |
||
| 60 | |||
| 61 | /** Modifiers get concatenated with space |
||
| 62 | * @var string[] */ |
||
| 63 | protected $modifier = []; |
||
| 64 | |||
| 65 | /** EntityManager to use for quoting |
||
| 66 | * @var EntityManager */ |
||
| 67 | protected $entityManager; |
||
| 68 | |||
| 69 | /** The default EntityManager to use to for quoting |
||
| 70 | * @var EntityManager */ |
||
| 71 | public static $defaultEntityManager; |
||
| 72 | |||
| 73 | /** @noinspection PhpMissingParentConstructorInspection */ |
||
| 74 | /** |
||
| 75 | * Constructor |
||
| 76 | * |
||
| 77 | * Create a select statement for $tableName with an object oriented interface. |
||
| 78 | * |
||
| 79 | * It uses static::$defaultEntityManager if $entityManager is not given. |
||
| 80 | * |
||
| 81 | * @param string $tableName The main table to use in FROM clause |
||
| 82 | * @param string $alias An alias for the table |
||
| 83 | * @param EntityManager $entityManager EntityManager for quoting |
||
| 84 | */ |
||
| 85 | 116 | public function __construct($tableName, $alias = '', EntityManager $entityManager = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Replaces question marks in $expression with $args |
||
| 94 | * |
||
| 95 | * @param string $expression Expression with placeholders |
||
| 96 | * @param array|mixed $args Arguments for placeholders |
||
| 97 | * @return string |
||
| 98 | * @throws \ORM\Exceptions\NoConnection |
||
| 99 | * @throws \ORM\Exceptions\NotScalar |
||
| 100 | */ |
||
| 101 | 136 | protected function convertPlaceholders( |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return EntityManager |
||
| 129 | */ |
||
| 130 | 96 | public function getEntityManager() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Common implementation for creating a where condition |
||
| 137 | * |
||
| 138 | * @param string $column Column or expression with placeholders |
||
| 139 | * @param string $operator Operator or value if operator is omited |
||
| 140 | * @param string $value Value or array of values |
||
| 141 | * @return string |
||
| 142 | * @throws NoOperator |
||
| 143 | * @internal |
||
| 144 | */ |
||
| 145 | 94 | public function createWhereCondition($column, $operator = null, $value = null) |
|
| 165 | |||
| 166 | 67 | private function buildExpression($column, $value, $operator = null) |
|
| 179 | |||
| 180 | 46 | private function getDefaultOperator($value) |
|
| 188 | |||
| 189 | /** {@inheritdoc} */ |
||
| 190 | 2 | public function columns(array $columns = null) |
|
| 196 | |||
| 197 | /** {@inheritdoc} */ |
||
| 198 | 5 | public function column($column, $args = [], $alias = '') |
|
| 210 | |||
| 211 | /** @internal |
||
| 212 | * @return self */ |
||
| 213 | 1 | public function close() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Common implementation for *Join methods |
||
| 220 | * |
||
| 221 | * @param string $join The join type (e. g. `LEFT JOIN`) |
||
| 222 | * @param string $tableName Table name to join |
||
| 223 | * @param string $expression Expression to use in on clause or single column for USING |
||
| 224 | * @param string $alias Alias for the table |
||
| 225 | * @param array|mixed $args Arguments to use in $expression |
||
| 226 | * @param bool $empty Create an empty join (without USING and ON) |
||
| 227 | * @return ParenthesisInterface|QueryBuilder |
||
| 228 | * @throws \ORM\Exceptions\NoConnection |
||
| 229 | * @throws \ORM\Exceptions\NotScalar |
||
| 230 | * @internal |
||
| 231 | */ |
||
| 232 | 42 | protected function createJoin($join, $tableName, $expression, $alias, $args, $empty) |
|
| 257 | |||
| 258 | /** {@inheritdoc} */ |
||
| 259 | 17 | public function join($tableName, $expression = '', $alias = '', $args = []) |
|
| 270 | |||
| 271 | /** {@inheritdoc} */ |
||
| 272 | 11 | public function leftJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 283 | |||
| 284 | /** {@inheritdoc} */ |
||
| 285 | 7 | public function rightJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 296 | |||
| 297 | /** {@inheritdoc} */ |
||
| 298 | 7 | public function fullJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 309 | |||
| 310 | /** {@inheritdoc} */ |
||
| 311 | 4 | public function groupBy($column, $args = []) |
|
| 317 | |||
| 318 | /** {@inheritdoc} */ |
||
| 319 | 5 | public function orderBy($column, $direction = self::DIRECTION_ASCENDING, $args = []) |
|
| 327 | |||
| 328 | /** {@inheritdoc} */ |
||
| 329 | 2 | public function limit($limit) |
|
| 335 | |||
| 336 | /** {@inheritdoc} */ |
||
| 337 | 1 | public function offset($offset) |
|
| 343 | |||
| 344 | /** {@inheritdoc} */ |
||
| 345 | 156 | public function getQuery() |
|
| 357 | |||
| 358 | /** {@inheritdoc} */ |
||
| 359 | 1 | public function modifier($modifier) |
|
| 365 | } |
||
| 366 |