Innmind /
Immutable
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Innmind\Immutable; |
||
| 5 | |||
| 6 | use Innmind\Immutable\Exception\InvalidArgumentException; |
||
| 7 | |||
| 8 | class Set implements SetInterface |
||
| 9 | { |
||
| 10 | use Type; |
||
| 11 | |||
| 12 | private $type; |
||
| 13 | private $spec; |
||
| 14 | private $values; |
||
| 15 | |||
| 16 | 19 | public function __construct(string $type) |
|
| 17 | { |
||
| 18 | 19 | $this->type = $type; |
|
| 19 | 19 | $this->spec = $this->getSpecFor($type); |
|
| 20 | 19 | $this->values = new Sequence; |
|
| 21 | 19 | } |
|
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | 11 | public function type(): string |
|
| 27 | { |
||
| 28 | 11 | return $this->type; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 2 | public function size(): int |
|
| 35 | { |
||
| 36 | 2 | return $this->values->size(); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 1 | public function count(): int |
|
| 43 | { |
||
| 44 | 1 | return $this->values->size(); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 9 | public function toPrimitive() |
|
| 51 | { |
||
| 52 | 9 | return $this->values->toPrimitive(); |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 1 | public function current() |
|
| 59 | { |
||
| 60 | 1 | return $this->values->current(); |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 1 | public function key() |
|
| 67 | { |
||
| 68 | 1 | return $this->values->key(); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 1 | public function next() |
|
| 75 | { |
||
| 76 | 1 | $this->values->next(); |
|
| 77 | 1 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | 1 | public function rewind() |
|
| 83 | { |
||
| 84 | 1 | $this->values->rewind(); |
|
| 85 | 1 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 1 | public function valid() |
|
| 91 | { |
||
| 92 | 1 | return $this->values->valid(); |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 2 | View Code Duplication | public function intersect(SetInterface $set): SetInterface |
| 99 | { |
||
| 100 | 2 | $this->validate($set); |
|
| 101 | |||
| 102 | 1 | $newSet = clone $this; |
|
| 103 | 1 | $newSet->values = $this->values->intersect($set->values); |
|
|
0 ignored issues
–
show
|
|||
| 104 | |||
| 105 | 1 | return $newSet; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 15 | public function add($element): SetInterface |
|
| 112 | { |
||
| 113 | 15 | $this->spec->validate($element); |
|
| 114 | |||
| 115 | 14 | if ($this->contains($element)) { |
|
| 116 | 1 | return $this; |
|
| 117 | } |
||
| 118 | |||
| 119 | 14 | $set = clone $this; |
|
| 120 | 14 | $set->values = $this->values->add($element); |
|
| 121 | |||
| 122 | 14 | return $set; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | 14 | public function contains($element): bool |
|
| 129 | { |
||
| 130 | 14 | return $this->values->contains($element); |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 1 | public function remove($element): SetInterface |
|
| 137 | { |
||
| 138 | 1 | if (!$this->contains($element)) { |
|
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | 1 | $index = $this->values->indexOf($element); |
|
| 143 | 1 | $set = clone $this; |
|
| 144 | 1 | $set->values = (new Sequence) |
|
| 145 | 1 | ->append($this->values->slice(0, $index)) |
|
| 146 | 1 | ->append($this->values->slice($index + 1, $this->size())); |
|
| 147 | |||
| 148 | 1 | return $set; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | 2 | View Code Duplication | public function diff(SetInterface $set): SetInterface |
| 155 | { |
||
| 156 | 2 | $this->validate($set); |
|
| 157 | |||
| 158 | 1 | $newSet = clone $this; |
|
| 159 | 1 | $newSet->values = $this->values->diff($set->values); |
|
|
0 ignored issues
–
show
Accessing
values on the interface Innmind\Immutable\SetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 160 | |||
| 161 | 1 | return $newSet; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 2 | public function equals(SetInterface $set): bool |
|
| 168 | { |
||
| 169 | 2 | $this->validate($set); |
|
| 170 | |||
| 171 | 1 | return $this->values->equals($set->values); |
|
|
0 ignored issues
–
show
Accessing
values on the interface Innmind\Immutable\SetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 1 | public function filter(\Closure $predicate): SetInterface |
|
| 178 | { |
||
| 179 | 1 | $set = clone $this; |
|
| 180 | 1 | $set->values = $this->values->filter($predicate); |
|
| 181 | |||
| 182 | 1 | return $set; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 1 | public function foreach(\Closure $function): SetInterface |
|
| 189 | { |
||
| 190 | 1 | $this->values->foreach($function); |
|
| 191 | |||
| 192 | 1 | return $this; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | 1 | public function groupBy(\Closure $discriminator): MapInterface |
|
| 199 | { |
||
| 200 | 1 | return $this->values->groupBy($discriminator); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 1 | public function map(\Closure $function): SetInterface |
|
| 207 | { |
||
| 208 | 1 | $set = clone $this; |
|
| 209 | 1 | $set->values = $this->values->map($function); |
|
| 210 | |||
| 211 | 1 | return $set; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | 1 | public function partition(\Closure $predicate): SequenceInterface |
|
| 218 | { |
||
| 219 | 1 | $truthy = clone $this; |
|
| 220 | 1 | $falsy = clone $this; |
|
| 221 | 1 | $partitions = $this->values->partition($predicate); |
|
| 222 | 1 | $truthy->values = $partitions->get(0); |
|
| 223 | 1 | $falsy->values = $partitions->get(1); |
|
| 224 | |||
| 225 | 1 | return new Sequence($truthy, $falsy); |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | 1 | public function join(string $separator): StringPrimitive |
|
| 232 | { |
||
| 233 | 1 | return $this->values->join($separator); |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | 1 | public function sort(\Closure $function): SequenceInterface |
|
| 240 | { |
||
| 241 | 1 | return $this->values->sort($function); |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Make sure the set is compatible with the current one |
||
| 246 | * |
||
| 247 | * @param SetInterface $set |
||
| 248 | * |
||
| 249 | * @throws InvalidArgumentException |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | 6 | private function validate(SetInterface $set) |
|
| 254 | { |
||
| 255 | 6 | if ($set->type() !== $this->type) { |
|
| 256 | 3 | throw new InvalidArgumentException( |
|
| 257 | 3 | 'The 2 sets does not reference the same type' |
|
| 258 | ); |
||
| 259 | } |
||
| 260 | 3 | } |
|
| 261 | } |
||
| 262 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: