Completed
Push — develop ( e67729...75d57c )
by Bartko
02:02
created

Zend1   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 556
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 98.09%

Importance

Changes 0
Metric Value
dl 0
loc 556
rs 6.433
c 0
b 0
f 0
ccs 257
cts 262
cp 0.9809
wmc 57
lcom 1
cbo 5

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setDbAdapter() 0 4 1
A getDbAdapter() 0 4 1
A getBlankDbSelect() 0 9 1
A setDefaultDbSelect() 0 4 1
A getDefaultDbSelect() 0 10 2
A lockTree() 0 13 1
A beginTransaction() 0 4 1
A commitTransaction() 0 4 1
A rollbackTransaction() 0 4 1
A update() 0 13 1
A insert() 0 23 3
A delete() 0 12 1
B moveLeftIndexes() 0 24 3
B moveRightIndexes() 0 26 3
A updateParentId() 0 15 1
B updateLevels() 0 29 3
B moveBranch() 0 30 3
A getRoots() 0 16 3
A getRoot() 0 6 2
A getNode() 0 15 2
A getNodeInfo() 0 19 3
B getChildrenNodeInfo() 0 33 3
A updateNodeMetadata() 0 17 1
B getAncestors() 0 41 5
B getDescendants() 0 55 8
A getWhereBetween() 0 9 1

How to fix   Complexity   

Complex Class

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
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\Adapter;
6
7
use StefanoTree\NestedSet\NodeInfo;
8
use StefanoTree\NestedSet\Options;
9
use Zend_Db_Adapter_Abstract as ZendDbAdapter;
10
use Zend_Db_Select as ZendDbSelect;
11
12
class Zend1 extends AdapterAbstract implements AdapterInterface
13
{
14
    protected $dbAdapter;
15
16
    protected $defaultDbSelect;
17
18 55
    public function __construct(Options $options, ZendDbAdapter $dbAdapter)
19
    {
20 55
        $this->setOptions($options);
21 55
        $this->setDbAdapter($dbAdapter);
22 55
    }
23
24
    /**
25
     * @param ZendDbAdapter $dbAdapter
26
     */
27 55
    protected function setDbAdapter(ZendDbAdapter $dbAdapter): void
28
    {
29 55
        $this->dbAdapter = $dbAdapter;
30 55
    }
31
32
    /**
33
     * @return ZendDbAdapter
34
     */
35 52
    public function getDbAdapter(): ZendDbAdapter
36
    {
37 52
        return $this->dbAdapter;
38
    }
39
40
    /**
41
     * Return base db select without any join, etc.
42
     *
43
     * @return ZendDbSelect
44
     */
45 32
    public function getBlankDbSelect(): ZendDbSelect
46
    {
47 32
        return $this->getDbAdapter()
48 32
            ->select()
49 32
            ->from(array(
50 32
                $this->getOptions()->getTableAlias() => $this->getOptions()->getTableName(),
51
                )
52
            );
53
    }
54
55
    /**
56
     * @param ZendDbSelect $dbSelect
57
     */
58 6
    public function setDefaultDbSelect(ZendDbSelect $dbSelect): void
59
    {
60 6
        $this->defaultDbSelect = $dbSelect;
61 6
    }
62
63
    /**
64
     * Return clone of default select.
65
     *
66
     * @return ZendDbSelect
67
     */
68 17
    public function getDefaultDbSelect(): ZendDbSelect
69
    {
70 17
        if (null == $this->defaultDbSelect) {
71 13
            $this->defaultDbSelect = $this->getBlankDbSelect();
72
        }
73
74 17
        $dbSelect = clone $this->defaultDbSelect;
75
76 17
        return $dbSelect;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function lockTree(): void
83
    {
84 1
        $options = $this->getOptions();
85
86 1
        $dbAdapter = $this->getDbAdapter();
87
88 1
        $select = $this->getBlankDbSelect()
89 1
            ->reset(\Zend_Db_Select::COLUMNS)
90 1
            ->columns(array('i' => $options->getIdColumnName(true)))
91 1
            ->forUpdate(true);
92
93 1
        $dbAdapter->fetchAll($select);
94 1
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function beginTransaction(): void
100
    {
101 1
        $this->getDbAdapter()->beginTransaction();
102 1
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function commitTransaction(): void
108
    {
109 1
        $this->getDbAdapter()->commit();
110 1
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function rollbackTransaction(): void
116
    {
117 1
        $this->getDbAdapter()->rollBack();
118 1
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 3
    public function update($nodeId, array $data): void
124
    {
125 3
        $options = $this->getOptions();
126
127 3
        $dbAdapter = $this->getDbAdapter();
128
129 3
        $data = $this->cleanData($data);
130
131
        $where = array(
132 3
            $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId,
133
        );
134 3
        $dbAdapter->update($options->getTableName(), $data, $where);
135 3
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 3
    public function insert(NodeInfo $nodeInfo, array $data)
141
    {
142 3
        $options = $this->getOptions();
143 3
        $dbAdapter = $this->getDbAdapter();
144
145 3
        $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId();
146 3
        $data[$options->getLevelColumnName()] = $nodeInfo->getLevel();
147 3
        $data[$options->getLeftColumnName()] = $nodeInfo->getLeft();
148 3
        $data[$options->getRightColumnName()] = $nodeInfo->getRight();
149
150 3
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
151 1
            $data[$options->getScopeColumnName()] = $nodeInfo->getScope();
152
        }
153
154 3
        $dbAdapter->insert($options->getTableName(), $data);
155 3
        if ('' != $options->getSequenceName()) {
156
            $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName());
157
        } else {
158 3
            $lastGeneratedValue = $dbAdapter->lastInsertId();
159
        }
160
161 3
        return $lastGeneratedValue;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 2
    public function delete($nodeId): void
168
    {
169 2
        $options = $this->getOptions();
170
171 2
        $dbAdapter = $this->getDbAdapter();
172
173
        $where = array(
174 2
            $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId,
175
        );
176
177 2
        $dbAdapter->delete($options->getTableName(), $where);
178 2
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 2
    public function moveLeftIndexes($fromIndex, $shift, $scope = null): void
184
    {
185 2
        $options = $this->getOptions();
186
187 2
        if (0 == $shift) {
188
            return;
189
        }
190
191 2
        $dbAdapter = $this->getDbAdapter();
192 2
        $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName())
193 2
            .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName())
194 2
            .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift'
195 2
            .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' > :fromIndex';
196
197 2
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
198 1
            $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope);
199
        }
200
201
        $binds = array(
202 2
            ':shift' => $shift,
203 2
            ':fromIndex' => $fromIndex,
204
        );
205 2
        $dbAdapter->prepare($sql)->execute($binds);
206 2
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 2
    public function moveRightIndexes($fromIndex, $shift, $scope = null): void
212
    {
213 2
        $options = $this->getOptions();
214
215 2
        if (0 == $shift) {
216
            return;
217
        }
218
219 2
        $dbAdapter = $this->getDbAdapter();
220
221 2
        $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName())
222 2
            .' SET '.$dbAdapter->quoteIdentifier($options->getRightColumnName())
223 2
            .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift'
224 2
            .' WHERE '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' > :fromIndex';
225
226 2
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
227 1
            $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope);
228
        }
229
230
        $binds = array(
231 2
            ':shift' => $shift,
232 2
            ':fromIndex' => $fromIndex,
233
        );
234
235 2
        $dbAdapter->prepare($sql)->execute($binds);
236 2
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 1
    public function updateParentId($nodeId, $newParentId): void
242
    {
243 1
        $options = $this->getOptions();
244
245 1
        $dbAdapter = $this->getDbAdapter();
246
247
        $bind = array(
248 1
            $options->getParentIdColumnName() => $newParentId,
249
        );
250
251
        $where = array(
252 1
            $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId,
253
        );
254 1
        $dbAdapter->update($options->getTableName(), $bind, $where);
255 1
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 2
    public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void
261
    {
262 2
        $options = $this->getOptions();
263
264 2
        if (0 == $shift) {
265
            return;
266
        }
267
268 2
        $dbAdapter = $this->getDbAdapter();
269
270 2
        $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName())
271 2
            .' SET '.$dbAdapter->quoteIdentifier($options->getLevelColumnName())
272 2
            .' = '.$dbAdapter->quoteIdentifier($options->getLevelColumnName()).' + :shift'
273 2
            .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName())
274 2
            .' >= :leftFrom'.' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName())
275 2
            .' <= :rightTo';
276
277 2
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
278 1
            $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope);
279
        }
280
281
        $binds = array(
282 2
            ':shift' => $shift,
283 2
            ':leftFrom' => $leftIndexFrom,
284 2
            ':rightTo' => $rightIndexTo,
285
        );
286
287 2
        $dbAdapter->prepare($sql)->execute($binds);
288 2
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293 2
    public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void
294
    {
295 2
        if (0 == $shift) {
296
            return;
297
        }
298
299 2
        $options = $this->getOptions();
300
301 2
        $dbAdapter = $this->getDbAdapter();
302
303 2
        $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName())
304 2
            .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName())
305 2
            .' = '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' + :shift, '
306 2
            .$dbAdapter->quoteIdentifier($options->getRightColumnName())
307 2
            .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift'
308 2
            .' WHERE '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()).' >= :leftFrom'
309 2
            .' AND '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' <= :rightTo';
310
311 2
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
312 1
            $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope);
313
        }
314
315
        $binds = array(
316 2
            ':shift' => $shift,
317 2
            ':leftFrom' => $leftIndexFrom,
318 2
            ':rightTo' => $rightIndexTo,
319
        );
320
321 2
        $dbAdapter->prepare($sql)->execute($binds);
322 2
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327 4
    public function getRoots($scope = null): array
328
    {
329 4
        $options = $this->getOptions();
330
331 4
        $dbAdapter = $this->getDbAdapter();
332
333 4
        $select = $this->getBlankDbSelect()
334 4
            ->where($options->getParentIdColumnName(true).' IS NULL')
335 4
            ->order($options->getIdColumnName(true));
336
337 4
        if (null != $scope && $options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
338 1
            $select->where($options->getScopeColumnName(true).' = ?', $scope);
339
        }
340
341 4
        return $dbAdapter->fetchAll($select);
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347 2
    public function getRoot($scope = null): array
348
    {
349 2
        $result = $this->getRoots($scope);
350
351 2
        return ($result) ? $result[0] : array();
352
    }
353
354
    /**
355
     * {@inheritdoc}
356
     */
357 2
    public function getNode($nodeId): ?array
358
    {
359 2
        $options = $this->getOptions();
360
361 2
        $nodeId = (int) $nodeId;
362
363 2
        $dbAdapter = $this->getDbAdapter();
364
365 2
        $select = $this->getDefaultDbSelect()
366 2
            ->where($options->getIdColumnName(true).' = ?', $nodeId);
367
368 2
        $row = $dbAdapter->fetchRow($select);
369
370 2
        return $row ? $row : null;
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376 19
    public function getNodeInfo($nodeId): ?NodeInfo
377
    {
378 19
        $options = $this->getOptions();
379
380 19
        $nodeId = (int) $nodeId;
381
382 19
        $dbAdapter = $this->getDbAdapter();
383
384 19
        $select = $this->getBlankDbSelect()
385 19
            ->where($options->getIdColumnName(true).' = ?', $nodeId);
386
387 19
        $row = $dbAdapter->fetchRow($select);
388
389 19
        $data = $row ? $row : null;
390
391 19
        $result = ($data) ? $this->_buildNodeInfoObject($data) : null;
392
393 19
        return $result;
394
    }
395
396
    /**
397
     * {@inheritdoc}
398
     */
399 4
    public function getChildrenNodeInfo($parentNodeId): array
400
    {
401 4
        $dbAdapter = $this->getDbAdapter();
402 4
        $options = $this->getOptions();
403
404
        $columns = array(
405 4
            $options->getIdColumnName(),
406 4
            $options->getLeftColumnName(),
407 4
            $options->getRightColumnName(),
408 4
            $options->getParentIdColumnName(),
409 4
            $options->getLevelColumnName(),
410
        );
411
412 4
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
413 2
            $columns[] = $options->getScopeColumnName();
414
        }
415
416 4
        $select = $this->getBlankDbSelect();
417 4
        $select->reset(\Zend_Db_Select::COLUMNS);
418 4
        $select->columns($columns);
419 4
        $select->order($options->getLeftColumnName(true));
420 4
        $select->where($options->getParentIdColumnName(true).' = ?', $parentNodeId);
421
422 4
        $data = $dbAdapter->fetchAll($select);
423
424 4
        $result = array();
425
426 4
        foreach ($data as $nodeData) {
427 3
            $result[] = $this->_buildNodeInfoObject($nodeData);
428
        }
429
430 4
        return $result;
431
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436 2
    public function updateNodeMetadata(NodeInfo $nodeInfo): void
437
    {
438 2
        $dbAdapter = $this->getDbAdapter();
439 2
        $options = $this->getOptions();
440
441
        $bind = array(
442 2
            $options->getRightColumnName() => $nodeInfo->getRight(),
443 2
            $options->getLeftColumnName() => $nodeInfo->getLeft(),
444 2
            $options->getLevelColumnName() => $nodeInfo->getLevel(),
445
        );
446
447
        $where = array(
448 2
            $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeInfo->getId(),
449
        );
450
451 2
        $dbAdapter->update($options->getTableName(), $bind, $where);
452 2
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457 6
    public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array
458
    {
459 6
        $options = $this->getOptions();
460
461 6
        $startLevel = (int) $startLevel;
462
463
        // node does not exist
464 6
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
465 1
            return array();
466
        }
467
468 5
        $dbAdapter = $this->getDbAdapter();
469
470 5
        $select = $this->getDefaultDbSelect();
471
472 5
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
473 2
            $select->where($options->getScopeColumnName(true).' = ?', $nodeInfo->getScope());
474
        }
475
476 5
        $select->where(
477 5
            $dbAdapter->quoteIdentifier($options->getLeftColumnName(true)).' <= ?', $nodeInfo->getLeft()
478 5
        )->where(
479 5
            $dbAdapter->quoteIdentifier($options->getRightColumnName(true)).' >= ?', $nodeInfo->getRight()
480 5
        )->order($options->getLeftColumnName(true).' ASC');
481
482 5
        if (0 < $startLevel) {
483 2
            $select->where(
484 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' >= ?', $startLevel
485
            );
486
        }
487
488 5
        if (0 < $excludeLastNLevels) {
489 2
            $select->where(
490 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' <= ?', $nodeInfo->getLevel() - $excludeLastNLevels
491
            );
492
        }
493
494 5
        $result = $dbAdapter->fetchAll($select);
495
496 5
        return $result;
497
    }
498
499
    /**
500
     * {@inheritdoc}
501
     */
502 9
    public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array
503
    {
504 9
        $options = $this->getOptions();
505
506 9
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
507 1
            return array();
508
        }
509
510 8
        $dbAdapter = $this->getDbAdapter();
511 8
        $select = $this->getDefaultDbSelect();
512 8
        $select->order($options->getLeftColumnName(true).' ASC');
513
514 8
        if ($options->getScopeColumnName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options->getScopeColumnName() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
515 3
            $select->where($options->getScopeColumnName(true).' = ?', $nodeInfo->getScope());
516
        }
517
518 8
        if (0 != $startLevel) {
519 3
            $level = $nodeInfo->getLevel() + (int) $startLevel;
520 3
            $select->where(
521 3
                $dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' >= ?', $level
522
            );
523
        }
524
525 8
        if (null != $levels) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $levels of type null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
526 2
            $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels);
527 2
            $select->where(
528 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName(true)).' < ?', $endLevel
529
            );
530
        }
531
532 8
        if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) {
533 2
            $where = sprintf(
534 2
                '(%s OR %s) AND (%s OR %s)',
535 2
                $this->getWhereBetween($options->getLeftColumnName(true), $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1),
536 2
                $this->getWhereBetween($options->getLeftColumnName(true),
537 2
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
538 2
                $this->getWhereBetween($options->getRightColumnName(true),
539 2
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
540 2
                $this->getWhereBetween($options->getRightColumnName(true),
541 2
                    $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1)
542
            );
543 2
            $select->where($where);
544
        } else {
545 6
            $select->where(
546 6
                $dbAdapter->quoteIdentifier($options->getLeftColumnName(true)).' >= ?', $nodeInfo->getLeft()
547
            );
548 6
            $select->where(
549 6
                $dbAdapter->quoteIdentifier($options->getRightColumnName(true)).' <= ?', $nodeInfo->getRight()
550
            );
551
        }
552
553 8
        $resultArray = $dbAdapter->fetchAll($select);
554
555 8
        return (0 < count($resultArray)) ? $resultArray : array();
556
    }
557
558 2
    protected function getWhereBetween($column, $first, $second)
559
    {
560 2
        $dbAdapter = $this->getDbAdapter();
561 2
        $quotedColumn = $dbAdapter->quoteIdentifier($column);
562 2
        $quotedFirst = $dbAdapter->quote($first);
563 2
        $quotedSecond = $dbAdapter->quote($second);
564
565 2
        return sprintf('(%s BETWEEN %s AND %s)', $quotedColumn, $quotedFirst, $quotedSecond);
566
    }
567
}
568