Complex classes like Zend1 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 Zend1, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Zend1 extends AdapterAbstract implements AdapterInterface |
||
13 | { |
||
14 | protected $dbAdapter; |
||
15 | |||
16 | 56 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
17 | { |
||
18 | 56 | $this->setOptions($options); |
|
19 | 56 | $this->setDbAdapter($dbAdapter); |
|
20 | 56 | } |
|
21 | |||
22 | /** |
||
23 | * @param ZendDbAdapter $dbAdapter |
||
24 | */ |
||
25 | 56 | protected function setDbAdapter(ZendDbAdapter $dbAdapter): void |
|
26 | { |
||
27 | 56 | $this->dbAdapter = $dbAdapter; |
|
28 | 56 | } |
|
29 | |||
30 | /** |
||
31 | * @return ZendDbAdapter |
||
32 | */ |
||
33 | 53 | public function getDbAdapter(): ZendDbAdapter |
|
34 | { |
||
35 | 53 | return $this->dbAdapter; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | * |
||
41 | * @return ZendDbSelect |
||
42 | */ |
||
43 | 32 | public function getBlankDbSelect(): ZendDbSelect |
|
44 | { |
||
45 | 32 | return $this->getDbAdapter() |
|
46 | 32 | ->select() |
|
47 | 32 | ->from($this->getOptions()->getTableName()); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Return default db select. Always new instance. |
||
52 | * |
||
53 | * @return ZendDbSelect |
||
54 | */ |
||
55 | 17 | public function getDefaultDbSelect(): ZendDbSelect |
|
56 | { |
||
57 | 17 | return $this->getDbSelectBuilder()(); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 1 | public function lockTree(): void |
|
64 | { |
||
65 | 1 | $options = $this->getOptions(); |
|
66 | |||
67 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
68 | |||
69 | 1 | $select = $this->getBlankDbSelect() |
|
70 | 1 | ->reset(\Zend_Db_Select::COLUMNS) |
|
71 | 1 | ->columns(array('i' => $options->getIdColumnName(true))) |
|
72 | 1 | ->forUpdate(true); |
|
73 | |||
74 | 1 | $dbAdapter->fetchAll($select); |
|
75 | 1 | } |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 1 | public function beginTransaction(): void |
|
81 | { |
||
82 | 1 | $this->getDbAdapter()->beginTransaction(); |
|
83 | 1 | } |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 1 | public function commitTransaction(): void |
|
89 | { |
||
90 | 1 | $this->getDbAdapter()->commit(); |
|
91 | 1 | } |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 1 | public function rollbackTransaction(): void |
|
97 | { |
||
98 | 1 | $this->getDbAdapter()->rollBack(); |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 3 | public function update($nodeId, array $data): void |
|
105 | { |
||
106 | 3 | $options = $this->getOptions(); |
|
107 | |||
108 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
109 | |||
110 | 3 | $data = $this->cleanData($data); |
|
111 | |||
112 | $where = array( |
||
113 | 3 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
114 | ); |
||
115 | 3 | $dbAdapter->update($options->getTableName(), $data, $where); |
|
116 | 3 | } |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 4 | public function insert(NodeInfo $nodeInfo, array $data) |
|
122 | { |
||
123 | 4 | $options = $this->getOptions(); |
|
124 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
125 | |||
126 | 4 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
127 | 4 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
128 | 4 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
129 | 4 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
130 | |||
131 | 4 | if ($options->getScopeColumnName()) { |
|
|
|||
132 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
133 | } |
||
134 | |||
135 | 4 | $dbAdapter->insert($options->getTableName(), $data); |
|
136 | |||
137 | 4 | if (array_key_exists($options->getIdColumnName(), $data)) { |
|
138 | 1 | return $data[$options->getIdColumnName()]; |
|
139 | } else { |
||
140 | 3 | if ('' != $options->getSequenceName()) { |
|
141 | 3 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
|
142 | } else { |
||
143 | $lastGeneratedValue = $dbAdapter->lastInsertId(); |
||
144 | } |
||
145 | |||
146 | 3 | return $lastGeneratedValue; |
|
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 2 | public function delete($nodeId): void |
|
154 | { |
||
155 | 2 | $options = $this->getOptions(); |
|
156 | |||
157 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
158 | |||
159 | $where = array( |
||
160 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
161 | ); |
||
162 | |||
163 | 2 | $dbAdapter->delete($options->getTableName(), $where); |
|
164 | 2 | } |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
170 | { |
||
171 | 2 | $options = $this->getOptions(); |
|
172 | |||
173 | 2 | if (0 == $shift) { |
|
174 | return; |
||
175 | } |
||
176 | |||
177 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
178 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
179 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
180 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift' |
|
181 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' > :fromIndex'; |
|
182 | |||
183 | 2 | if ($options->getScopeColumnName()) { |
|
184 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
185 | } |
||
186 | |||
187 | $binds = array( |
||
188 | 2 | ':shift' => $shift, |
|
189 | 2 | ':fromIndex' => $fromIndex, |
|
190 | ); |
||
191 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
192 | 2 | } |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
198 | { |
||
199 | 2 | $options = $this->getOptions(); |
|
200 | |||
201 | 2 | if (0 == $shift) { |
|
202 | return; |
||
203 | } |
||
204 | |||
205 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
206 | |||
207 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
208 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
209 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
|
210 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' > :fromIndex'; |
|
211 | |||
212 | 2 | if ($options->getScopeColumnName()) { |
|
213 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
214 | } |
||
215 | |||
216 | $binds = array( |
||
217 | 2 | ':shift' => $shift, |
|
218 | 2 | ':fromIndex' => $fromIndex, |
|
219 | ); |
||
220 | |||
221 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
222 | 2 | } |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 1 | public function updateParentId($nodeId, $newParentId): void |
|
228 | { |
||
229 | 1 | $options = $this->getOptions(); |
|
230 | |||
231 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
232 | |||
233 | $bind = array( |
||
234 | 1 | $options->getParentIdColumnName() => $newParentId, |
|
235 | ); |
||
236 | |||
237 | $where = array( |
||
238 | 1 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
239 | ); |
||
240 | 1 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
241 | 1 | } |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 2 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | 2 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
280 | { |
||
281 | 2 | if (0 == $shift) { |
|
282 | return; |
||
283 | } |
||
284 | |||
285 | 2 | $options = $this->getOptions(); |
|
286 | |||
287 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
288 | |||
289 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
290 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
291 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift, ' |
|
292 | 2 | .$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
293 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
|
294 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' >= :leftFrom' |
|
295 | 2 | .' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' <= :rightTo'; |
|
296 | |||
297 | 2 | if ($options->getScopeColumnName()) { |
|
298 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
299 | } |
||
300 | |||
301 | $binds = array( |
||
302 | 2 | ':shift' => $shift, |
|
303 | 2 | ':leftFrom' => $leftIndexFrom, |
|
304 | 2 | ':rightTo' => $rightIndexTo, |
|
305 | ); |
||
306 | |||
307 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
308 | 2 | } |
|
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | 4 | public function getRoots($scope = null): array |
|
314 | { |
||
315 | 4 | $options = $this->getOptions(); |
|
316 | |||
317 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
318 | |||
319 | 4 | $select = $this->getBlankDbSelect() |
|
320 | 4 | ->where($options->getParentIdColumnName(true).' IS NULL') |
|
321 | 4 | ->order($options->getIdColumnName(true)); |
|
322 | |||
323 | 4 | if (null != $scope && $options->getScopeColumnName()) { |
|
324 | 1 | $select->where($options->getScopeColumnName(true).' = ?', $scope); |
|
325 | } |
||
326 | |||
327 | 4 | return $dbAdapter->fetchAll($select); |
|
328 | } |
||
329 | |||
330 | /** |
||
331 | * {@inheritdoc} |
||
332 | */ |
||
333 | 2 | public function getRoot($scope = null): array |
|
334 | { |
||
335 | 2 | $result = $this->getRoots($scope); |
|
336 | |||
337 | 2 | return ($result) ? $result[0] : array(); |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | 2 | public function getNode($nodeId): ?array |
|
344 | { |
||
345 | 2 | $options = $this->getOptions(); |
|
346 | |||
347 | 2 | $nodeId = (int) $nodeId; |
|
348 | |||
349 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
350 | |||
351 | 2 | $select = $this->getDefaultDbSelect() |
|
352 | 2 | ->where($options->getIdColumnName(true).' = ?', $nodeId); |
|
353 | |||
354 | 2 | $row = $dbAdapter->fetchRow($select); |
|
355 | |||
356 | 2 | return $row ? $row : null; |
|
357 | } |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | 19 | public function getNodeInfo($nodeId): ?NodeInfo |
|
363 | { |
||
364 | 19 | $options = $this->getOptions(); |
|
365 | |||
366 | 19 | $nodeId = (int) $nodeId; |
|
367 | |||
368 | 19 | $dbAdapter = $this->getDbAdapter(); |
|
369 | |||
370 | 19 | $select = $this->getBlankDbSelect() |
|
371 | 19 | ->where($options->getIdColumnName(true).' = ?', $nodeId); |
|
372 | |||
373 | 19 | $row = $dbAdapter->fetchRow($select); |
|
374 | |||
375 | 19 | $data = $row ? $row : null; |
|
376 | |||
377 | 19 | $result = ($data) ? $this->_buildNodeInfoObject($data) : null; |
|
378 | |||
379 | 19 | return $result; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | 4 | public function getChildrenNodeInfo($parentNodeId): array |
|
386 | { |
||
387 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
388 | 4 | $options = $this->getOptions(); |
|
389 | |||
390 | $columns = array( |
||
391 | 4 | $options->getIdColumnName(), |
|
392 | 4 | $options->getLeftColumnName(), |
|
393 | 4 | $options->getRightColumnName(), |
|
394 | 4 | $options->getParentIdColumnName(), |
|
395 | 4 | $options->getLevelColumnName(), |
|
396 | ); |
||
397 | |||
398 | 4 | if ($options->getScopeColumnName()) { |
|
399 | 2 | $columns[] = $options->getScopeColumnName(); |
|
400 | } |
||
401 | |||
402 | 4 | $select = $this->getBlankDbSelect(); |
|
403 | 4 | $select->reset(\Zend_Db_Select::COLUMNS); |
|
404 | 4 | $select->columns($columns); |
|
405 | 4 | $select->order($options->getLeftColumnName(true)); |
|
406 | 4 | $select->where($options->getParentIdColumnName(true).' = ?', $parentNodeId); |
|
407 | |||
408 | 4 | $data = $dbAdapter->fetchAll($select); |
|
409 | |||
410 | 4 | $result = array(); |
|
411 | |||
412 | 4 | foreach ($data as $nodeData) { |
|
413 | 3 | $result[] = $this->_buildNodeInfoObject($nodeData); |
|
414 | } |
||
415 | |||
416 | 4 | return $result; |
|
417 | } |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | 6 | public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array |
|
444 | { |
||
445 | 6 | $options = $this->getOptions(); |
|
446 | |||
447 | // node does not exist |
||
448 | 6 | if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
|
449 | 1 | return array(); |
|
450 | } |
||
451 | |||
452 | 5 | $dbAdapter = $this->getDbAdapter(); |
|
453 | |||
454 | 5 | $select = $this->getDefaultDbSelect(); |
|
455 | |||
456 | 5 | if ($options->getScopeColumnName()) { |
|
457 | 2 | $select->where($options->getScopeColumnName(true).' = ?', $nodeInfo->getScope()); |
|
458 | } |
||
459 | |||
460 | 5 | $select->where( |
|
482 | |||
483 | /** |
||
484 | * {@inheritdoc} |
||
485 | */ |
||
486 | 9 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array |
|
541 | |||
542 | 2 | protected function getWhereBetween($column, $first, $second) |
|
551 | } |
||
552 |
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: