DoubleLinkedList::contains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace GenericCollections\Internal;
3
4
/**
5
 * This is a double linked list that contains elements by position
6
 *
7
 * It extends \SplDoublyLinkedList and add this methods:
8
 * - contains: return if the value is found in the list
9
 * - search: search for a value in the list, return the index
10
 * - clear: clear the list
11
 *
12
 * The search depends on the flag strictComparison.
13
 * If is TRUE then the comparison is identical (===), this is the default behavior
14
 * If is FALSE then the comparison is equal (==)
15
 *
16
 * @package GenericCollections\Internal
17
 */
18
class DoubleLinkedList extends \SplDoublyLinkedList
19
{
20
    /**
21
     * Set if the search inside the storage must be identical (true) or equal (false)
22
     *
23
     * @var bool
24
     */
25
    private $strict = true;
26
27 1
    public function strictComparisonOn()
28
    {
29 1
        $this->strict = true;
30 1
    }
31
32 5
    public function strictComparisonOff()
33
    {
34 5
        $this->strict = false;
35 5
    }
36
37 2
    public function getStrictComparison()
38
    {
39 2
        return $this->strict;
40
    }
41
42
    /**
43
     * Returns TRUE if the element exist in the storage
44
     *
45
     * @param mixed $element
46
     * @return bool
47
     */
48 11
    public function contains($element)
49
    {
50 11
        return (-1 !== $this->search($element));
51
    }
52
53
    /**
54
     * Perform a linear search inside the storage,
55
     * because the elements contained are not sorted.
56
     *
57
     * Return the index of the element found, -1 if not found
58
     *
59
     * @param $element
60
     * @return int
61
     */
62 13
    public function search($element)
63
    {
64 13
        $position = -1;
65 13
        if (! $this->isEmpty()) {
66 13
            foreach ($this as $index => $current) {
67 13
                if (($this->strict) ? $current === $element : $current == $element) {
68 12
                    $position = $index;
69 13
                    break;
70
                }
71
            }
72
        }
73 13
        return $position;
74
    }
75
76
    /**
77
     * Clear the contents of the container
78
     *
79
     * There is a bug & patch for SplDoublyLinkedList https://bugs.php.net/bug.php?id=60759
80
     * that does the same operation (pop until count == 0)
81
     */
82 1
    public function clear()
83
    {
84 1
        while ($this->count() > 0) {
85 1
            $this->pop();
86
        }
87 1
    }
88
}
89