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