Complex classes like ProxyQuery 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 ProxyQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ProxyQuery implements ProxyQueryInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var QueryBuilder |
||
| 27 | */ |
||
| 28 | protected $queryBuilder; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $sortBy; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var mixed |
||
| 37 | */ |
||
| 38 | protected $sortOrder; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $uniqueParameterId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $entityJoinAliases; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * For BC reasons, this property is true by default. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private $distinct = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The map of query hints. |
||
| 59 | * |
||
| 60 | * @var array<string,mixed> |
||
| 61 | */ |
||
| 62 | private $hints = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param QueryBuilder $queryBuilder |
||
| 66 | */ |
||
| 67 | public function __construct($queryBuilder) |
||
| 68 | { |
||
| 69 | $this->queryBuilder = $queryBuilder; |
||
| 70 | $this->uniqueParameterId = 0; |
||
| 71 | $this->entityJoinAliases = []; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function __call($name, $args) |
||
| 75 | { |
||
| 76 | return call_user_func_array([$this->queryBuilder, $name], $args); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function __get($name) |
||
| 80 | { |
||
| 81 | return $this->queryBuilder->$name; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function __clone() |
||
| 85 | { |
||
| 86 | $this->queryBuilder = clone $this->queryBuilder; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Optimize queries with a lot of rows. |
||
| 91 | * It is not recommended to use "false" with left joins. |
||
| 92 | * |
||
| 93 | * @param bool $distinct |
||
| 94 | * |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | final public function setDistinct($distinct) |
||
| 98 | { |
||
| 99 | if (!is_bool($distinct)) { |
||
| 100 | throw new \InvalidArgumentException('$distinct is not a boolean'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->distinct = $distinct; |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | final public function isDistinct() |
||
| 112 | { |
||
| 113 | return $this->distinct; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function execute(array $params = [], $hydrationMode = null) |
||
| 117 | { |
||
| 118 | // always clone the original queryBuilder |
||
| 119 | $queryBuilder = clone $this->queryBuilder; |
||
| 120 | |||
| 121 | $rootAlias = current($queryBuilder->getRootAliases()); |
||
| 122 | |||
| 123 | // todo : check how doctrine behave, potential SQL injection here ... |
||
| 124 | if ($this->getSortBy()) { |
||
| 125 | $sortBy = $this->getSortBy(); |
||
| 126 | if (false === strpos($sortBy, '.')) { // add the current alias |
||
| 127 | $sortBy = $rootAlias.'.'.$sortBy; |
||
| 128 | } |
||
| 129 | $queryBuilder->addOrderBy($sortBy, $this->getSortOrder()); |
||
| 130 | } else { |
||
| 131 | $queryBuilder->resetDQLPart('orderBy'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /* By default, always add a sort on the identifier fields of the first |
||
| 135 | * used entity in the query, because RDBMS do not guarantee a |
||
| 136 | * particular order when no ORDER BY clause is specified, or when |
||
| 137 | * the field used for sorting is not unique. |
||
| 138 | */ |
||
| 139 | |||
| 140 | $identifierFields = $queryBuilder |
||
| 141 | ->getEntityManager() |
||
| 142 | ->getMetadataFactory() |
||
| 143 | ->getMetadataFor(current($queryBuilder->getRootEntities())) |
||
| 144 | ->getIdentifierFieldNames(); |
||
| 145 | |||
| 146 | $existingOrders = []; |
||
| 147 | /** @var Query\Expr\OrderBy $order */ |
||
| 148 | foreach ($queryBuilder->getDQLPart('orderBy') as $order) { |
||
| 149 | foreach ($order->getParts() as $part) { |
||
| 150 | $existingOrders[] = trim(str_replace([Criteria::DESC, Criteria::ASC], '', $part)); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | foreach ($identifierFields as $identifierField) { |
||
| 155 | $order = $rootAlias.'.'.$identifierField; |
||
| 156 | if (!in_array($order, $existingOrders)) { |
||
| 157 | $queryBuilder->addOrderBy( |
||
| 158 | $order, |
||
| 159 | $this->getSortOrder() // reusing the sort order is the most natural way to go |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | $query = $this->getFixedQueryBuilder($queryBuilder)->getQuery(); |
||
| 165 | foreach ($this->hints as $name => $value) { |
||
| 166 | $query->setHint($name, $value); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $query->execute($params, $hydrationMode); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function setSortBy($parentAssociationMappings, $fieldMapping) |
||
| 173 | { |
||
| 174 | $alias = $this->entityJoin($parentAssociationMappings); |
||
| 175 | $this->sortBy = $alias.'.'.$fieldMapping['fieldName']; |
||
| 176 | |||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getSortBy() |
||
| 181 | { |
||
| 182 | return $this->sortBy; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function setSortOrder($sortOrder) |
||
| 186 | { |
||
| 187 | if (!in_array(strtoupper($sortOrder), $validSortOrders = ['ASC', 'DESC'])) { |
||
| 188 | throw new \InvalidArgumentException(sprintf( |
||
| 189 | '"%s" is not a valid sort order, valid values are "%s"', |
||
| 190 | $sortOrder, |
||
| 191 | implode(', ', $validSortOrders) |
||
| 192 | )); |
||
| 193 | } |
||
| 194 | $this->sortOrder = $sortOrder; |
||
| 195 | |||
| 196 | return $this; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getSortOrder() |
||
| 200 | { |
||
| 201 | return $this->sortOrder; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getSingleScalarResult() |
||
| 205 | { |
||
| 206 | $query = $this->queryBuilder->getQuery(); |
||
| 207 | |||
| 208 | return $query->getSingleScalarResult(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | public function getQueryBuilder() |
||
| 215 | { |
||
| 216 | return $this->queryBuilder; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function setFirstResult($firstResult) |
||
| 220 | { |
||
| 221 | $this->queryBuilder->setFirstResult($firstResult); |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getFirstResult() |
||
| 227 | { |
||
| 228 | return $this->queryBuilder->getFirstResult(); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function setMaxResults($maxResults) |
||
| 232 | { |
||
| 233 | $this->queryBuilder->setMaxResults($maxResults); |
||
| 234 | |||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function getMaxResults() |
||
| 242 | |||
| 243 | public function getUniqueParameterId() |
||
| 244 | { |
||
| 245 | return $this->uniqueParameterId++; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function entityJoin(array $associationMappings) |
||
| 249 | { |
||
| 250 | $alias = current($this->queryBuilder->getRootAliases()); |
||
| 251 | |||
| 252 | $newAlias = 's'; |
||
| 253 | |||
| 254 | $joinedEntities = $this->queryBuilder->getDQLPart('join'); |
||
| 255 | |||
| 256 | foreach ($associationMappings as $associationMapping) { |
||
| 257 | // Do not add left join to already joined entities with custom query |
||
| 258 | foreach ($joinedEntities as $joinExprList) { |
||
| 259 | foreach ($joinExprList as $joinExpr) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Sets a {@see \Doctrine\ORM\Query} hint. If the hint name is not recognized, it is silently ignored. |
||
| 285 | * |
||
| 286 | * @param string $name the name of the hint |
||
| 287 | * @param mixed $value the value of the hint |
||
| 288 | * |
||
| 289 | * @return ProxyQueryInterface |
||
| 290 | * |
||
| 291 | * @see \Doctrine\ORM\Query::setHint |
||
| 292 | * @see \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER |
||
| 293 | */ |
||
| 294 | final public function setHint($name, $value) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * This method alters the query to return a clean set of object with a working |
||
| 303 | * set of Object. |
||
| 304 | * |
||
| 305 | * @return QueryBuilder |
||
| 306 | */ |
||
| 307 | protected function getFixedQueryBuilder(QueryBuilder $queryBuilder) |
||
| 374 | } |
||
| 375 |