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 | 55 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
17 | { |
||
18 | 55 | $this->setOptions($options); |
|
19 | 55 | $this->setDbAdapter($dbAdapter); |
|
20 | 55 | } |
|
21 | |||
22 | /** |
||
23 | * @param ZendDbAdapter $dbAdapter |
||
24 | */ |
||
25 | 55 | protected function setDbAdapter(ZendDbAdapter $dbAdapter): void |
|
26 | { |
||
27 | 55 | $this->dbAdapter = $dbAdapter; |
|
28 | 55 | } |
|
29 | |||
30 | /** |
||
31 | * @return ZendDbAdapter |
||
32 | */ |
||
33 | 52 | public function getDbAdapter(): ZendDbAdapter |
|
34 | { |
||
35 | 52 | return $this->dbAdapter; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Return base db select without any join, etc. |
||
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 | 3 | public function insert(NodeInfo $nodeInfo, array $data) |
|
122 | { |
||
123 | 3 | $options = $this->getOptions(); |
|
124 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
125 | |||
126 | 3 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
127 | 3 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
128 | 3 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
129 | 3 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
130 | |||
131 | 3 | if ($options->getScopeColumnName()) { |
|
|
|||
132 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
133 | } |
||
134 | |||
135 | 3 | $dbAdapter->insert($options->getTableName(), $data); |
|
136 | 3 | if ('' != $options->getSequenceName()) { |
|
137 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
||
138 | } else { |
||
139 | 3 | $lastGeneratedValue = $dbAdapter->lastInsertId(); |
|
140 | } |
||
141 | |||
142 | 3 | return $lastGeneratedValue; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 2 | public function delete($nodeId): void |
|
149 | { |
||
150 | 2 | $options = $this->getOptions(); |
|
151 | |||
152 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
153 | |||
154 | $where = array( |
||
155 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
156 | ); |
||
157 | |||
158 | 2 | $dbAdapter->delete($options->getTableName(), $where); |
|
159 | 2 | } |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
165 | { |
||
166 | 2 | $options = $this->getOptions(); |
|
167 | |||
168 | 2 | if (0 == $shift) { |
|
169 | return; |
||
170 | } |
||
171 | |||
172 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
173 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
174 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
175 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift' |
|
176 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' > :fromIndex'; |
|
177 | |||
178 | 2 | if ($options->getScopeColumnName()) { |
|
179 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
180 | } |
||
181 | |||
182 | $binds = array( |
||
183 | 2 | ':shift' => $shift, |
|
184 | 2 | ':fromIndex' => $fromIndex, |
|
185 | ); |
||
186 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
187 | 2 | } |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
193 | { |
||
194 | 2 | $options = $this->getOptions(); |
|
195 | |||
196 | 2 | if (0 == $shift) { |
|
197 | return; |
||
198 | } |
||
199 | |||
200 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
201 | |||
202 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
203 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
204 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
|
205 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' > :fromIndex'; |
|
206 | |||
207 | 2 | if ($options->getScopeColumnName()) { |
|
208 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
209 | } |
||
210 | |||
211 | $binds = array( |
||
212 | 2 | ':shift' => $shift, |
|
213 | 2 | ':fromIndex' => $fromIndex, |
|
214 | ); |
||
215 | |||
216 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
217 | 2 | } |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 1 | public function updateParentId($nodeId, $newParentId): void |
|
223 | { |
||
224 | 1 | $options = $this->getOptions(); |
|
225 | |||
226 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
227 | |||
228 | $bind = array( |
||
229 | 1 | $options->getParentIdColumnName() => $newParentId, |
|
230 | ); |
||
231 | |||
232 | $where = array( |
||
233 | 1 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
234 | ); |
||
235 | 1 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
236 | 1 | } |
|
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | 2 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
242 | { |
||
243 | 2 | $options = $this->getOptions(); |
|
244 | |||
245 | 2 | if (0 == $shift) { |
|
246 | return; |
||
247 | } |
||
248 | |||
249 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
250 | |||
251 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
252 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLevelColumnName()) |
|
253 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getLevelColumnName()).' + :shift' |
|
254 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
255 | 2 | .' >= :leftFrom'.' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
256 | 2 | .' <= :rightTo'; |
|
257 | |||
258 | 2 | if ($options->getScopeColumnName()) { |
|
259 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
260 | } |
||
261 | |||
262 | $binds = array( |
||
263 | 2 | ':shift' => $shift, |
|
264 | 2 | ':leftFrom' => $leftIndexFrom, |
|
265 | 2 | ':rightTo' => $rightIndexTo, |
|
266 | ); |
||
267 | |||
268 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
269 | 2 | } |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | 2 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
275 | { |
||
276 | 2 | if (0 == $shift) { |
|
277 | return; |
||
278 | } |
||
279 | |||
280 | 2 | $options = $this->getOptions(); |
|
281 | |||
282 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
283 | |||
284 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
285 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
286 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift, ' |
|
287 | 2 | .$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
288 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
|
289 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' >= :leftFrom' |
|
290 | 2 | .' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' <= :rightTo'; |
|
291 | |||
292 | 2 | if ($options->getScopeColumnName()) { |
|
293 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
294 | } |
||
295 | |||
296 | $binds = array( |
||
297 | 2 | ':shift' => $shift, |
|
298 | 2 | ':leftFrom' => $leftIndexFrom, |
|
299 | 2 | ':rightTo' => $rightIndexTo, |
|
300 | ); |
||
301 | |||
302 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
303 | 2 | } |
|
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | 4 | public function getRoots($scope = null): array |
|
309 | { |
||
310 | 4 | $options = $this->getOptions(); |
|
311 | |||
312 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
313 | |||
314 | 4 | $select = $this->getBlankDbSelect() |
|
315 | 4 | ->where($options->getParentIdColumnName(true).' IS NULL') |
|
316 | 4 | ->order($options->getIdColumnName(true)); |
|
317 | |||
318 | 4 | if (null != $scope && $options->getScopeColumnName()) { |
|
319 | 1 | $select->where($options->getScopeColumnName(true).' = ?', $scope); |
|
320 | } |
||
321 | |||
322 | 4 | return $dbAdapter->fetchAll($select); |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * {@inheritdoc} |
||
327 | */ |
||
328 | 2 | public function getRoot($scope = null): array |
|
329 | { |
||
330 | 2 | $result = $this->getRoots($scope); |
|
331 | |||
332 | 2 | return ($result) ? $result[0] : array(); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | 2 | public function getNode($nodeId): ?array |
|
339 | { |
||
340 | 2 | $options = $this->getOptions(); |
|
341 | |||
342 | 2 | $nodeId = (int) $nodeId; |
|
343 | |||
344 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
345 | |||
346 | 2 | $select = $this->getDefaultDbSelect() |
|
347 | 2 | ->where($options->getIdColumnName(true).' = ?', $nodeId); |
|
348 | |||
349 | 2 | $row = $dbAdapter->fetchRow($select); |
|
350 | |||
351 | 2 | return $row ? $row : null; |
|
352 | } |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | 19 | public function getNodeInfo($nodeId): ?NodeInfo |
|
358 | { |
||
359 | 19 | $options = $this->getOptions(); |
|
360 | |||
361 | 19 | $nodeId = (int) $nodeId; |
|
362 | |||
363 | 19 | $dbAdapter = $this->getDbAdapter(); |
|
364 | |||
365 | 19 | $select = $this->getBlankDbSelect() |
|
366 | 19 | ->where($options->getIdColumnName(true).' = ?', $nodeId); |
|
367 | |||
368 | 19 | $row = $dbAdapter->fetchRow($select); |
|
369 | |||
370 | 19 | $data = $row ? $row : null; |
|
371 | |||
372 | 19 | $result = ($data) ? $this->_buildNodeInfoObject($data) : null; |
|
373 | |||
374 | 19 | return $result; |
|
375 | } |
||
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 4 | public function getChildrenNodeInfo($parentNodeId): array |
|
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
418 | { |
||
419 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
420 | 2 | $options = $this->getOptions(); |
|
421 | |||
422 | $bind = array( |
||
423 | 2 | $options->getRightColumnName() => $nodeInfo->getRight(), |
|
424 | 2 | $options->getLeftColumnName() => $nodeInfo->getLeft(), |
|
425 | 2 | $options->getLevelColumnName() => $nodeInfo->getLevel(), |
|
426 | ); |
||
427 | |||
428 | $where = array( |
||
429 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeInfo->getId(), |
|
430 | ); |
||
431 | |||
432 | 2 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
433 | 2 | } |
|
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | 6 | public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array |
|
479 | |||
480 | /** |
||
481 | * {@inheritdoc} |
||
482 | */ |
||
483 | 9 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array |
|
538 | |||
539 | 2 | protected function getWhereBetween($column, $first, $second) |
|
548 | } |
||
549 |
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: