Complex classes like Doctrine2DBAL 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 Doctrine2DBAL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Doctrine2DBAL extends AdapterAbstract implements AdapterInterface |
||
13 | { |
||
14 | private $connection; |
||
15 | |||
16 | private $defaultDbSelect; |
||
17 | |||
18 | /** |
||
19 | * @param Options $options |
||
20 | * @param DbConnection $connection |
||
21 | */ |
||
22 | 55 | public function __construct(Options $options, DbConnection $connection) |
|
27 | |||
28 | /** |
||
29 | * @param DbConnection $dbAdapter |
||
30 | */ |
||
31 | 55 | protected function setConnection(DbConnection $dbAdapter): void |
|
32 | { |
||
33 | 55 | $this->connection = $dbAdapter; |
|
34 | 55 | } |
|
35 | |||
36 | /** |
||
37 | * @return DbConnection |
||
38 | */ |
||
39 | 52 | private function getConnection(): DbConnection |
|
43 | |||
44 | /** |
||
45 | * Return base db select without any join, etc. |
||
46 | * |
||
47 | * @return QueryBuilder |
||
48 | */ |
||
49 | 32 | public function getBlankDbSelect(): QueryBuilder |
|
50 | { |
||
51 | 32 | $queryBuilder = $this->getConnection() |
|
52 | 32 | ->createQueryBuilder(); |
|
53 | |||
54 | 32 | $queryBuilder->select(sprintf('%s.*', $this->getOptions()->getTableAlias())) |
|
55 | 32 | ->from($this->getOptions()->getTableName(), $this->getOptions()->getTableAlias()); |
|
56 | |||
57 | 32 | return $queryBuilder; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param QueryBuilder $dbSelect |
||
62 | */ |
||
63 | 6 | public function setDefaultDbSelect(QueryBuilder $dbSelect): void |
|
64 | { |
||
65 | 6 | $this->defaultDbSelect = $dbSelect; |
|
66 | 6 | } |
|
67 | |||
68 | /** |
||
69 | * Return clone of default db select. |
||
70 | * |
||
71 | * @return QueryBuilder |
||
72 | */ |
||
73 | 17 | public function getDefaultDbSelect(): QueryBuilder |
|
74 | { |
||
75 | 17 | if (null === $this->defaultDbSelect) { |
|
76 | 13 | $this->defaultDbSelect = $this->getBlankDbSelect(); |
|
77 | } |
||
78 | |||
79 | 17 | $dbSelect = clone $this->defaultDbSelect; |
|
80 | |||
81 | 17 | return $dbSelect; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 1 | public function lockTree(): void |
|
88 | { |
||
89 | 1 | $options = $this->getOptions(); |
|
90 | |||
91 | 1 | $connection = $this->getConnection(); |
|
92 | |||
93 | 1 | $sql = $this->getBlankDbSelect(); |
|
94 | 1 | $sql->select($options->getIdColumnName(true).' AS i'); |
|
95 | |||
96 | 1 | $sql = $sql->getSQL().' FOR UPDATE'; |
|
97 | |||
98 | 1 | $connection->executeQuery($sql); |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 1 | public function beginTransaction(): void |
|
105 | { |
||
106 | 1 | $this->getConnection() |
|
107 | 1 | ->beginTransaction(); |
|
108 | 1 | } |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 1 | public function commitTransaction(): void |
|
114 | { |
||
115 | 1 | $this->getConnection() |
|
116 | 1 | ->commit(); |
|
117 | 1 | } |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 1 | public function rollbackTransaction(): void |
|
123 | { |
||
124 | 1 | $this->getConnection() |
|
125 | 1 | ->rollBack(); |
|
126 | 1 | } |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 3 | public function update($nodeId, array $data): void |
|
132 | { |
||
133 | 3 | $options = $this->getOptions(); |
|
134 | |||
135 | 3 | $connection = $this->getConnection(); |
|
136 | |||
137 | 3 | $data = $this->cleanData($data); |
|
138 | |||
139 | 3 | $sql = $connection->createQueryBuilder(); |
|
140 | |||
141 | 3 | $sql->update($options->getTableName(), null) |
|
142 | 3 | ->where($options->getIdColumnName().' = :'.$options->getIdColumnName()); |
|
143 | |||
144 | 3 | foreach ($data as $key => $value) { |
|
145 | 3 | $sql->set($connection->quoteIdentifier($key), ':'.$key); |
|
146 | } |
||
147 | |||
148 | 3 | $data[$options->getIdColumnName()] = $nodeId; |
|
149 | |||
150 | 3 | $connection->executeUpdate($sql->getSQL(), $data); |
|
151 | 3 | } |
|
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 3 | public function insert(NodeInfo $nodeInfo, array $data) |
|
157 | { |
||
158 | 3 | $options = $this->getOptions(); |
|
159 | |||
160 | 3 | $connection = $this->getConnection(); |
|
161 | |||
162 | 3 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
163 | 3 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
164 | 3 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
165 | 3 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
166 | |||
167 | 3 | if ($options->getScopeColumnName()) { |
|
|
|||
168 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
169 | } |
||
170 | |||
171 | 3 | $connection->insert($options->getTableName(), $data); |
|
172 | |||
173 | 3 | return $connection->lastInsertId($options->getSequenceName()); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 2 | public function delete($nodeId): void |
|
180 | { |
||
181 | 2 | $options = $this->getOptions(); |
|
182 | |||
183 | 2 | $connection = $this->getConnection(); |
|
184 | |||
185 | 2 | $sql = $connection->createQueryBuilder(); |
|
186 | 2 | $sql->delete($options->getTableName()) |
|
187 | 2 | ->where($options->getIdColumnName().' = :id'); |
|
188 | |||
189 | $params = array( |
||
190 | 2 | ':id' => $nodeId, |
|
191 | ); |
||
192 | |||
193 | 2 | $connection->executeQuery($sql->getSQL(), $params); |
|
194 | 2 | } |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
200 | { |
||
201 | 2 | $options = $this->getOptions(); |
|
202 | |||
203 | 2 | if (0 == $shift) { |
|
204 | return; |
||
205 | } |
||
206 | |||
207 | 2 | $connection = $this->getConnection(); |
|
208 | |||
209 | 2 | $sql = $connection->createQueryBuilder(); |
|
210 | 2 | $sql->update($options->getTableName()) |
|
211 | 2 | ->set($options->getLeftColumnName(), $options->getLeftColumnName().' + :shift') |
|
212 | 2 | ->where($options->getLeftColumnName().' > :fromIndex'); |
|
213 | |||
214 | $params = array( |
||
215 | 2 | ':shift' => $shift, |
|
216 | 2 | ':fromIndex' => $fromIndex, |
|
217 | ); |
||
218 | |||
219 | 2 | if ($options->getScopeColumnName()) { |
|
220 | 1 | $sql->andWhere($options->getScopeColumnName().' = :scope'); |
|
221 | 1 | $params[':scope'] = $scope; |
|
222 | } |
||
223 | |||
224 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
225 | 2 | } |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
231 | { |
||
232 | 2 | $options = $this->getOptions(); |
|
233 | |||
234 | 2 | if (0 == $shift) { |
|
235 | return; |
||
236 | } |
||
237 | |||
238 | 2 | $connection = $this->getConnection(); |
|
239 | |||
240 | 2 | $sql = $connection->createQueryBuilder(); |
|
241 | 2 | $sql->update($options->getTableName()) |
|
242 | 2 | ->set($options->getRightColumnName(), $options->getRightColumnName().' + :shift') |
|
243 | 2 | ->where($options->getRightColumnName().' > :fromIndex'); |
|
244 | |||
245 | $params = array( |
||
246 | 2 | ':shift' => $shift, |
|
247 | 2 | ':fromIndex' => $fromIndex, |
|
248 | ); |
||
249 | |||
250 | 2 | if ($options->getScopeColumnName()) { |
|
251 | 1 | $sql->andWhere($options->getScopeColumnName().' = :scope'); |
|
252 | 1 | $params[':scope'] = $scope; |
|
253 | } |
||
254 | |||
255 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
256 | 2 | } |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | public function updateParentId($nodeId, $newParentId): void |
|
262 | { |
||
263 | 1 | $options = $this->getOptions(); |
|
264 | |||
265 | 1 | $connection = $this->getConnection(); |
|
266 | |||
267 | 1 | $sql = $connection->createQueryBuilder(); |
|
268 | 1 | $sql->update($options->getTableName()) |
|
269 | 1 | ->set($options->getParentIdColumnName(), ':parentId') |
|
270 | 1 | ->where($options->getIdColumnName().' = :nodeId'); |
|
271 | |||
272 | $params = array( |
||
273 | 1 | ':parentId' => $newParentId, |
|
274 | 1 | ':nodeId' => $nodeId, |
|
275 | ); |
||
276 | |||
277 | 1 | $connection->executeUpdate($sql->getSQL(), $params); |
|
278 | 1 | } |
|
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | 2 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | 2 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
317 | { |
||
318 | 2 | if (0 == $shift) { |
|
319 | return; |
||
320 | } |
||
321 | |||
322 | 2 | $options = $this->getOptions(); |
|
323 | |||
324 | 2 | $connection = $this->getConnection(); |
|
325 | |||
326 | 2 | $sql = $connection->createQueryBuilder(); |
|
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | 4 | public function getRoots($scope = null): array |
|
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | 2 | public function getRoot($scope = null): array |
|
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | 2 | public function getNode($nodeId): ?array |
|
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | 19 | public function getNodeInfo($nodeId): ?NodeInfo |
|
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | 4 | public function getChildrenNodeInfo($parentNodeId): array |
|
467 | |||
468 | /** |
||
469 | * {@inheritdoc} |
||
470 | */ |
||
471 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
490 | |||
491 | /** |
||
492 | * {@inheritdoc} |
||
493 | */ |
||
494 | 6 | public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array |
|
541 | |||
542 | /** |
||
543 | * {@inheritdoc} |
||
544 | */ |
||
545 | 9 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array |
|
599 | } |
||
600 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: