• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chdemko / php-sorted-collections / 24611342335

18 Apr 2026 06:41PM UTC coverage: 99.869% (-0.1%) from 100.0%
24611342335

push

github

chdemko
🚨 Fix scrutinizer

22 of 23 new or added lines in 4 files covered. (95.65%)

761 of 762 relevant lines covered (99.87%)

115.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/src/SortedCollection/TreeSet.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\TreeSet class
5
 *
6
 * @author    Christophe Demko <chdemko@gmail.com>
7
 * @copyright Copyright (C) 2012-2024 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13

14
// Declare chdemko\SortedCollection namespace
15
namespace chdemko\SortedCollection;
16

17
/**
18
 * Tree set
19
 *
20
 * @package    SortedCollection
21
 * @subpackage Set
22
 *
23
 * @since 1.0.0
24
 *
25
 * @property-read callable   $comparator  The element comparison function
26
 * @property-read mixed      $first       The first element of the set
27
 * @property-read mixed      $last        The last element of the set
28
 * @property-read integer    $count       The number of elements in the set
29
 */
30
class TreeSet extends AbstractSet
31
{
32
    /**
33
     * Constructor
34
     *
35
     * @param callable $comparator Comparison function
36
     *
37
     * @since 1.0.0
38
     */
39
    protected function __construct($comparator = null)
40
    {
41
        $this->setMap(TreeMap::create($comparator));
75✔
42
    }
43

44
    /**
45
     * Create
46
     *
47
     * @param callable $comparator Comparison function
48
     *
49
     * @return TreeSet A new TreeSet
50
     *
51
     * @since 1.0.0
52
     */
53
    public static function create($comparator = null)
54
    {
55
        return new static($comparator);
75✔
56
    }
57

58
    /**
59
     * Put values in the set
60
     *
61
     * @param iterable $traversable Values to put in the set
62
     *
63
     * @return TreeSet $this for chaining
64
     *
65
     * @since 1.0.0
66
     */
67
    public function put($traversable = array())
68
    {
69
        foreach ($traversable as $value) {
73✔
70
            $this[$value] = true;
66✔
71
        }
72

73
        return $this;
73✔
74
    }
75

76
    /**
77
     * Clear the set
78
     *
79
     * @return TreeSet $this for chaining
80
     *
81
     * @since 1.0.0
82
     */
83
    public function clear()
84
    {
85
        $map = $this->getMap();
71✔
86

87
        if ($map instanceof TreeMap) {
71✔
88
            $map->clear();
71✔
89
        } else {
NEW
90
            $this->setMap(TreeMap::create($map->comparator()));
×
91
        }
92

93
        return $this;
71✔
94
    }
95

96
    /**
97
     * Initialise the set
98
     *
99
     * @param iterable $traversable Values to initialise the set
100
     *
101
     * @return TreeSet $this for chaining
102
     *
103
     * @since 1.0.0
104
     */
105
    public function initialise($traversable = array())
106
    {
107
        return $this->clear()->put($traversable);
67✔
108
    }
109

110
    /**
111
     * Clone the set
112
     *
113
     * @return void
114
     *
115
     * @since 1.0.0
116
     */
117
    public function __clone()
118
    {
119
        $this->setMap(clone $this->getMap());
1✔
120
    }
121

122
    /**
123
     * Set the value for an element
124
     *
125
     * @param mixed $element The element
126
     * @param mixed $value   The value
127
     *
128
     * @return void
129
     *
130
     * @since 1.0.0
131
     */
132
    public function offsetSet($element, $value): void
133
    {
134
        $map = $this->getMap();
66✔
135

136
        if ($value) {
66✔
137
            $map[$element] = true;
66✔
138
        } else {
139
            unset($map[$element]);
1✔
140
        }
141
    }
142

143
    /**
144
     * Serialize the object
145
     *
146
     * @return array Array of values
147
     *
148
     * @since 1.0.0
149
     */
150
    public function jsonSerialize(): array
151
    {
152
        $array = array();
10✔
153

154
        foreach ($this as $value) {
10✔
155
            $array[] = $value;
10✔
156
        }
157

158
        return array('TreeSet' => $array);
10✔
159
    }
160

161
    /**
162
     * Unset the existence of an element
163
     *
164
     * @param mixed $element The element
165
     *
166
     * @return void
167
     *
168
     * @since 1.0.0
169
     */
170
    public function offsetUnset($element): void
171
    {
172
        $map = $this->getMap();
1✔
173
        unset($map[$element]);
1✔
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc