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 |
||
| 25 | class QueryBuilder extends Parenthesis implements QueryBuilderInterface |
||
| 26 | { |
||
| 27 | /** The table to query |
||
| 28 | * @var string */ |
||
| 29 | protected $tableName = ''; |
||
| 30 | |||
| 31 | /** The alias of the main table |
||
| 32 | * @var string */ |
||
| 33 | protected $alias = ''; |
||
| 34 | |||
| 35 | /** Columns to fetch (null is equal to ['*']) |
||
| 36 | * @var array|null */ |
||
| 37 | protected $columns = null; |
||
| 38 | |||
| 39 | /** Joins get concatenated with space |
||
| 40 | * @var string[] */ |
||
| 41 | protected $joins = []; |
||
| 42 | |||
| 43 | /** Limit amount of rows |
||
| 44 | * @var int */ |
||
| 45 | protected $limit; |
||
| 46 | |||
| 47 | /** Offset to start from |
||
| 48 | * @var int */ |
||
| 49 | protected $offset; |
||
| 50 | |||
| 51 | /** Group by conditions get concatenated with comma |
||
| 52 | * @var string[] */ |
||
| 53 | protected $groupBy = []; |
||
| 54 | |||
| 55 | /** Order by conditions get concatenated with comma |
||
| 56 | * @var string[] */ |
||
| 57 | protected $orderBy = []; |
||
| 58 | |||
| 59 | /** Modifiers get concatenated with space |
||
| 60 | * @var string[] */ |
||
| 61 | protected $modifier = []; |
||
| 62 | |||
| 63 | /** EntityManager to use for quoting |
||
| 64 | * @var EntityManager */ |
||
| 65 | protected $entityManager; |
||
| 66 | |||
| 67 | /** The default EntityManager to use to for quoting |
||
| 68 | * @var EntityManager */ |
||
| 69 | public static $defaultEntityManager; |
||
| 70 | |||
| 71 | /** @noinspection PhpMissingParentConstructorInspection */ |
||
| 72 | /** |
||
| 73 | * Constructor |
||
| 74 | * |
||
| 75 | * Create a select statement for $tableName with an object oriented interface. |
||
| 76 | * |
||
| 77 | * It uses static::$defaultEntityManager if $entityManager is not given. |
||
| 78 | * |
||
| 79 | * @param string $tableName The main table to use in FROM clause |
||
| 80 | * @param string $alias An alias for the table |
||
| 81 | * @param EntityManager $entityManager EntityManager for quoting |
||
| 82 | */ |
||
| 83 | 113 | public function __construct($tableName, $alias = '', EntityManager $entityManager = null) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Replaces question marks in $expression with $args |
||
| 92 | * |
||
| 93 | * @param string $expression Expression with placeholders |
||
| 94 | * @param array|mixed $args Arguments for placeholders |
||
| 95 | * @return string |
||
| 96 | * @throws \ORM\Exceptions\NoConnection |
||
| 97 | * @throws \ORM\Exceptions\NotScalar |
||
| 98 | */ |
||
| 99 | 131 | protected function convertPlaceholders( |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return EntityManager |
||
| 127 | */ |
||
| 128 | 93 | public function getEntityManager() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Common implementation for creating a where condition |
||
| 135 | * |
||
| 136 | * @param string $column Column or expression with placeholders |
||
| 137 | * @param string $operator Operator or value if operator is omited |
||
| 138 | * @param string $value Value or array of values |
||
| 139 | * @return string |
||
| 140 | * @throws \ORM\Exceptions\NoConnection |
||
| 141 | * @throws \ORM\Exceptions\NotScalar |
||
| 142 | * @internal |
||
| 143 | */ |
||
| 144 | 91 | public function createWhereCondition($column, $operator = '', $value = '') |
|
| 174 | |||
| 175 | /** {@inheritdoc} */ |
||
| 176 | 2 | public function columns(array $columns = null) |
|
| 182 | |||
| 183 | /** {@inheritdoc} */ |
||
| 184 | 5 | public function column($column, $args = [], $alias = '') |
|
| 196 | |||
| 197 | /** @internal |
||
| 198 | * @return self */ |
||
| 199 | 1 | public function close() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Common implementation for *Join methods |
||
| 206 | * |
||
| 207 | * @param string $join The join type (e. g. `LEFT JOIN`) |
||
| 208 | * @param string $tableName Table name to join |
||
| 209 | * @param string $expression Expression to use in on clause or single column for USING |
||
| 210 | * @param string $alias Alias for the table |
||
| 211 | * @param array|mixed $args Arguments to use in $expression |
||
| 212 | * @param bool $empty Create an empty join (without USING and ON) |
||
| 213 | * @return ParenthesisInterface|QueryBuilder |
||
| 214 | * @throws \ORM\Exceptions\NoConnection |
||
| 215 | * @throws \ORM\Exceptions\NotScalar |
||
| 216 | * @internal |
||
| 217 | */ |
||
| 218 | 40 | protected function createJoin($join, $tableName, $expression, $alias, $args, $empty) |
|
| 243 | |||
| 244 | /** {@inheritdoc} */ |
||
| 245 | 15 | public function join($tableName, $expression = '', $alias = '', $args = []) |
|
| 256 | |||
| 257 | /** {@inheritdoc} */ |
||
| 258 | 11 | public function leftJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 269 | |||
| 270 | /** {@inheritdoc} */ |
||
| 271 | 7 | public function rightJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 282 | |||
| 283 | /** {@inheritdoc} */ |
||
| 284 | 7 | public function fullJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 295 | |||
| 296 | /** {@inheritdoc} */ |
||
| 297 | 4 | public function groupBy($column, $args = []) |
|
| 303 | |||
| 304 | /** {@inheritdoc} */ |
||
| 305 | 5 | public function orderBy($column, $direction = self::DIRECTION_ASCENDING, $args = []) |
|
| 313 | |||
| 314 | /** {@inheritdoc} */ |
||
| 315 | 2 | public function limit($limit) |
|
| 321 | |||
| 322 | /** {@inheritdoc} */ |
||
| 323 | 1 | public function offset($offset) |
|
| 329 | |||
| 330 | /** {@inheritdoc} */ |
||
| 331 | 152 | public function getQuery() |
|
| 343 | |||
| 344 | /** {@inheritdoc} */ |
||
| 345 | 1 | public function modifier($modifier) |
|
| 351 | } |
||
| 352 |