Complex classes like Zend2 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 Zend2, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Zend2 extends AdapterAbstract implements AdapterInterface |
||
13 | { |
||
14 | private $dbAdapter; |
||
15 | |||
16 | private $defaultDbSelect = null; |
||
17 | |||
18 | 106 | public function __construct(Options $options, DbAdapter $dbAdapter) |
|
23 | |||
24 | /** |
||
25 | * @param DbAdapter $dbAdapter |
||
26 | */ |
||
27 | 106 | protected function setDbAdapter(DbAdapter $dbAdapter): void |
|
28 | { |
||
29 | 106 | $this->dbAdapter = $dbAdapter; |
|
30 | 106 | } |
|
31 | |||
32 | /** |
||
33 | * @return DbAdapter |
||
34 | */ |
||
35 | 98 | protected function getDbAdapter(): DbAdapter |
|
39 | |||
40 | /** |
||
41 | * Return base db select without any join, etc. |
||
42 | * |
||
43 | * @return Db\Sql\Select |
||
44 | */ |
||
45 | 79 | public function getBlankDbSelect(): Db\Sql\Select |
|
46 | { |
||
47 | 79 | return new Db\Sql\Select($this->getOptions()->getTableName()); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param Db\Sql\Select $dbSelect |
||
52 | */ |
||
53 | 1 | public function setDefaultDbSelect(Db\Sql\Select $dbSelect): void |
|
54 | { |
||
55 | 1 | $this->defaultDbSelect = $dbSelect; |
|
56 | 1 | } |
|
57 | |||
58 | /** |
||
59 | * Return default db select. |
||
60 | * |
||
61 | * @return Db\Sql\Select |
||
62 | */ |
||
63 | 25 | public function getDefaultDbSelect() |
|
64 | { |
||
65 | 25 | if (null === $this->defaultDbSelect) { |
|
66 | 24 | $this->defaultDbSelect = $this->getBlankDbSelect(); |
|
67 | } |
||
68 | |||
69 | 25 | $dbSelect = clone $this->defaultDbSelect; |
|
70 | |||
71 | 25 | return $dbSelect; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 31 | public function lockTree(): void |
|
78 | { |
||
79 | 31 | $options = $this->getOptions(); |
|
80 | |||
81 | 31 | $dbAdapter = $this->getDbAdapter(); |
|
82 | |||
83 | 31 | $select = $this->getBlankDbSelect(); |
|
84 | 31 | $select->columns(array( |
|
85 | 31 | 'i' => $options->getIdColumnName(), |
|
86 | )); |
||
87 | |||
88 | 31 | $sql = $select->getSqlString($dbAdapter->getPlatform()).' FOR UPDATE'; |
|
89 | |||
90 | 31 | $dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
|
91 | 31 | } |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 31 | public function beginTransaction(): void |
|
97 | { |
||
98 | 31 | $this->getDbAdapter() |
|
99 | 31 | ->getDriver() |
|
100 | 31 | ->getConnection() |
|
101 | 31 | ->beginTransaction(); |
|
102 | 31 | } |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 16 | public function commitTransaction(): void |
|
108 | { |
||
109 | 16 | $this->getDbAdapter() |
|
110 | 16 | ->getDriver() |
|
111 | 16 | ->getConnection() |
|
112 | 16 | ->commit(); |
|
113 | 16 | } |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 16 | public function rollbackTransaction(): void |
|
119 | { |
||
120 | 16 | $this->getDbAdapter() |
|
121 | 16 | ->getDriver() |
|
122 | 16 | ->getConnection() |
|
123 | 16 | ->rollback(); |
|
124 | 16 | } |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 5 | public function update($nodeId, array $data): void |
|
130 | { |
||
131 | 5 | $options = $this->getOptions(); |
|
132 | |||
133 | 5 | $dbAdapter = $this->getDbAdapter(); |
|
134 | |||
135 | 5 | $data = $this->cleanData($data); |
|
136 | |||
137 | 5 | $update = new Db\Sql\Update($options->getTableName()); |
|
138 | 5 | $update->set($data) |
|
139 | 5 | ->where(array( |
|
140 | 5 | $options->getIdColumnName() => $nodeId, |
|
141 | )); |
||
142 | |||
143 | 5 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
|
144 | 5 | DbAdapter::QUERY_MODE_EXECUTE); |
|
145 | 5 | } |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 12 | public function insert(NodeInfo $nodeInfo, array $data) |
|
151 | { |
||
152 | 12 | $options = $this->getOptions(); |
|
153 | |||
154 | 12 | $dbAdapter = $this->getDbAdapter(); |
|
155 | |||
156 | 12 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
157 | 12 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
158 | 12 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
159 | 12 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
160 | |||
161 | 12 | if ($options->getScopeColumnName()) { |
|
|
|||
162 | 4 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
163 | } |
||
164 | |||
165 | 12 | $insert = new Db\Sql\Insert($options->getTableName()); |
|
166 | 12 | $insert->values($data); |
|
167 | 12 | $dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()), |
|
168 | 12 | DbAdapter::QUERY_MODE_EXECUTE); |
|
169 | |||
170 | 12 | $lastGeneratedValue = $dbAdapter->getDriver() |
|
171 | 12 | ->getLastGeneratedValue($options->getSequenceName()); |
|
172 | |||
173 | 12 | return $lastGeneratedValue; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 4 | public function delete($nodeId): void |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 14 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
197 | { |
||
198 | 14 | $options = $this->getOptions(); |
|
199 | |||
200 | 14 | if (0 == $shift) { |
|
201 | return; |
||
202 | } |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 14 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 5 | public function updateParentId($nodeId, $newParentId): void |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | 6 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | 7 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
355 | |||
356 | /** |
||
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | 12 | public function getRoots($scope = null): array |
|
380 | |||
381 | /** |
||
382 | * {@inheritdoc} |
||
383 | */ |
||
384 | 9 | public function getRoot($scope = null): array |
|
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | 5 | public function getNode($nodeId): ?array |
|
412 | |||
413 | /** |
||
414 | * {@inheritdoc} |
||
415 | */ |
||
416 | 56 | public function getNodeInfo($nodeId): ?NodeInfo |
|
436 | |||
437 | /** |
||
438 | * {@inheritdoc} |
||
439 | */ |
||
440 | 6 | public function getChildrenNodeInfo($parentNodeId): array |
|
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | 3 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
499 | |||
500 | /** |
||
501 | * {@inheritdoc} |
||
502 | */ |
||
503 | 9 | public function getAncestors($nodeId, int $startLevel = 0, bool $excludeLastNode = false): array |
|
504 | { |
||
505 | 9 | $options = $this->getOptions(); |
|
506 | |||
507 | 9 | $startLevel = (int) $startLevel; |
|
508 | |||
509 | // node does not exist |
||
510 | 9 | $nodeInfo = $this->getNodeInfo($nodeId); |
|
511 | 9 | if (!$nodeInfo) { |
|
512 | 2 | return array(); |
|
513 | } |
||
514 | |||
515 | 7 | $dbAdapter = $this->getDbAdapter(); |
|
516 | |||
517 | 7 | $select = $this->getDefaultDbSelect(); |
|
518 | |||
519 | 7 | if ($options->getScopeColumnName()) { |
|
520 | 2 | $select->where |
|
521 | 2 | ->equalTo($options->getScopeColumnName(), $nodeInfo->getScope()); |
|
522 | } |
||
523 | |||
524 | 7 | $select->where |
|
525 | 7 | ->lessThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft()) |
|
526 | 7 | ->AND |
|
527 | 7 | ->greaterThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight()); |
|
528 | |||
529 | 7 | $select->order($options->getLeftColumnName().' ASC'); |
|
530 | |||
531 | 7 | if (0 < $startLevel) { |
|
532 | 2 | $select->where |
|
533 | 2 | ->greaterThanOrEqualTo($options->getLevelColumnName(), $startLevel); |
|
534 | } |
||
535 | |||
536 | 7 | if (true == $excludeLastNode) { |
|
537 | 3 | $select->where |
|
538 | 3 | ->lessThan($options->getLevelColumnName(), $nodeInfo->getLevel()); |
|
539 | } |
||
540 | |||
541 | 7 | $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
|
542 | 7 | DbAdapter::QUERY_MODE_EXECUTE); |
|
543 | |||
544 | 7 | return $result->toArray(); |
|
545 | } |
||
546 | |||
547 | /** |
||
548 | * {@inheritdoc} |
||
549 | */ |
||
550 | 14 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, ?int $excludeBranch = null): array |
|
610 | } |
||
611 |
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: