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 | public function __construct($tableName, $alias = '', EntityManager $entityManager = null) |
||
| 89 | 142 | ||
| 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 | */ |
||
| 97 | protected function convertPlaceholders( |
||
| 122 | |||
| 123 | 122 | /** |
|
| 124 | * @return EntityManager |
||
| 125 | */ |
||
| 126 | public function getEntityManager() |
||
| 130 | |||
| 131 | 124 | /** |
|
| 132 | * Common implementation for creating a where condition |
||
| 133 | * |
||
| 134 | * @param string $column Column or expression with placeholders |
||
| 135 | * @param string $operator Operator or value if operator is omited |
||
| 136 | * @param string $value Value or array of values |
||
| 137 | * @return string |
||
| 138 | * @internal |
||
| 139 | */ |
||
| 140 | public function createWhereCondition($column, $operator = null, $value = null) |
||
| 158 | |||
| 159 | private function buildExpression($column, $value, $operator = null) |
||
| 172 | |||
| 173 | 74 | private function getDefaultOperator($value) |
|
| 181 | 74 | ||
| 182 | 14 | /** {@inheritdoc} */ |
|
| 183 | public function columns(array $columns = null) |
||
| 189 | 28 | ||
| 190 | /** {@inheritdoc} */ |
||
| 191 | 28 | public function column($column, $args = [], $alias = '') |
|
| 203 | 5 | ||
| 204 | /** |
||
| 205 | 5 | * @internal |
|
| 206 | * @return static |
||
| 207 | 5 | */ |
|
| 208 | public function close() |
||
| 212 | 1 | ||
| 213 | /** |
||
| 214 | 1 | * Common implementation for *Join methods |
|
| 215 | * |
||
| 216 | * @param string $join The join type (e. g. `LEFT JOIN`) |
||
| 217 | * @param string $tableName Table name to join |
||
| 218 | * @param string|boolean $expression Expression, single column name or boolean to create an empty join |
||
| 219 | * @param string $alias Alias for the table |
||
| 220 | * @param array $args Arguments for expression |
||
| 221 | * @return ParenthesisInterface|QueryBuilder |
||
| 222 | * @internal |
||
| 223 | */ |
||
| 224 | protected function createJoin($join, $tableName, $expression = '', $alias = '', $args = []) |
||
| 255 | |||
| 256 | /** {@inheritdoc} */ |
||
| 257 | 40 | public function join($tableName, $expression = '', $alias = '', $args = []) |
|
| 261 | 19 | ||
| 262 | /** {@inheritdoc} */ |
||
| 263 | 19 | public function leftJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 267 | |||
| 268 | /** {@inheritdoc} */ |
||
| 269 | 11 | public function rightJoin($tableName, $expression = '', $alias = '', $args = []) |
|
| 273 | 11 | ||
| 274 | /** {@inheritdoc} */ |
||
| 275 | public function fullJoin($tableName, $expression = '', $alias = '', $args = []) |
||
| 279 | 7 | ||
| 280 | 7 | /** {@inheritdoc} */ |
|
| 281 | 7 | public function groupBy($column, $args = []) |
|
| 287 | 7 | ||
| 288 | 7 | /** {@inheritdoc} */ |
|
| 289 | 7 | public function orderBy($column, $direction = self::DIRECTION_ASCENDING, $args = []) |
|
| 297 | 4 | ||
| 298 | /** {@inheritdoc} */ |
||
| 299 | public function limit($limit) |
||
| 305 | 5 | ||
| 306 | /** {@inheritdoc} */ |
||
| 307 | 5 | public function offset($offset) |
|
| 313 | 2 | ||
| 314 | /** {@inheritdoc} */ |
||
| 315 | 2 | public function getQuery() |
|
| 327 | 186 | ||
| 328 | /** {@inheritdoc} */ |
||
| 329 | public function modifier($modifier) |
||
| 335 | } |
||
| 336 |