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 | 21 | public function __construct(string $type) |
|
| 17 | { |
||
| 18 | 21 | $this->type = new StringPrimitive($type); |
|
| 19 | 21 | $this->spec = $this->getSpecFor($type); |
|
| 20 | 21 | $this->values = new Sequence; |
|
| 21 | 21 | } |
|
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | 13 | public function type(): StringPrimitive |
|
| 27 | { |
||
| 28 | 13 | 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 | 11 | public function toPrimitive() |
|
| 51 | { |
||
| 52 | 11 | 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 |
|
0 ignored issues
–
show
|
|||
| 99 | { |
||
| 100 | 2 | $this->validate($set); |
|
| 101 | |||
| 102 | 1 | $newSet = clone $this; |
|
| 103 | 1 | $newSet->values = $this->values->intersect( |
|
| 104 | 1 | new Sequence(...$set->toPrimitive()) |
|
| 105 | ); |
||
| 106 | |||
| 107 | 1 | return $newSet; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | 16 | public function add($element): SetInterface |
|
| 114 | { |
||
| 115 | 16 | $this->spec->validate($element); |
|
| 116 | |||
| 117 | 15 | if ($this->contains($element)) { |
|
| 118 | 2 | return $this; |
|
| 119 | } |
||
| 120 | |||
| 121 | 15 | $set = clone $this; |
|
| 122 | 15 | $set->values = $this->values->add($element); |
|
| 123 | |||
| 124 | 15 | return $set; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | 15 | public function contains($element): bool |
|
| 131 | { |
||
| 132 | 15 | return $this->values->contains($element); |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | 1 | public function remove($element): SetInterface |
|
| 139 | { |
||
| 140 | 1 | if (!$this->contains($element)) { |
|
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | 1 | $index = $this->values->indexOf($element); |
|
| 145 | 1 | $set = clone $this; |
|
| 146 | 1 | $set->values = (new Sequence) |
|
| 147 | 1 | ->append($this->values->slice(0, $index)) |
|
| 148 | 1 | ->append($this->values->slice($index + 1, $this->size())); |
|
| 149 | |||
| 150 | 1 | return $set; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | 2 | View Code Duplication | public function diff(SetInterface $set): SetInterface |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 157 | { |
||
| 158 | 2 | $this->validate($set); |
|
| 159 | |||
| 160 | 1 | $newSet = clone $this; |
|
| 161 | 1 | $newSet->values = $this->values->diff( |
|
| 162 | 1 | new Sequence(...$set->toPrimitive()) |
|
| 163 | ); |
||
| 164 | |||
| 165 | 1 | return $newSet; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 3 | public function equals(SetInterface $set): bool |
|
| 172 | { |
||
| 173 | 3 | $this->validate($set); |
|
| 174 | |||
| 175 | 2 | return $this->values->equals( |
|
| 176 | 2 | new Sequence(...$set->toPrimitive()) |
|
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | 1 | public function filter(\Closure $predicate): SetInterface |
|
| 184 | { |
||
| 185 | 1 | $set = clone $this; |
|
| 186 | 1 | $set->values = $this->values->filter($predicate); |
|
| 187 | |||
| 188 | 1 | return $set; |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function foreach(\Closure $function): SetInterface |
||
| 195 | { |
||
| 196 | 2 | $this->values->foreach($function); |
|
| 197 | |||
| 198 | 2 | return $this; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | 1 | public function groupBy(\Closure $discriminator): MapInterface |
|
| 205 | { |
||
| 206 | 1 | return $this->values->groupBy($discriminator); |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 1 | public function map(\Closure $function): SetInterface |
|
| 213 | { |
||
| 214 | 1 | $set = clone $this; |
|
| 215 | 1 | $set->values = $this->values->map($function); |
|
| 216 | |||
| 217 | 1 | return $set; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | 1 | public function partition(\Closure $predicate): SequenceInterface |
|
| 224 | { |
||
| 225 | 1 | $truthy = clone $this; |
|
| 226 | 1 | $falsy = clone $this; |
|
| 227 | 1 | $partitions = $this->values->partition($predicate); |
|
| 228 | 1 | $truthy->values = $partitions->get(0); |
|
| 229 | 1 | $falsy->values = $partitions->get(1); |
|
| 230 | |||
| 231 | 1 | return new Sequence($truthy, $falsy); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | 1 | public function join(string $separator): StringPrimitive |
|
| 238 | { |
||
| 239 | 1 | return $this->values->join($separator); |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | 1 | public function sort(\Closure $function): SequenceInterface |
|
| 246 | { |
||
| 247 | 1 | return $this->values->sort($function); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 2 | public function merge(SetInterface $set): SetInterface |
|
| 254 | { |
||
| 255 | 2 | $this->validate($set); |
|
| 256 | 1 | $newSet = clone $this; |
|
| 257 | |||
| 258 | 1 | $set->foreach(function ($value) use (&$newSet) { |
|
| 259 | 1 | $newSet = $newSet->add($value); |
|
| 260 | 1 | }); |
|
| 261 | |||
| 262 | 1 | return $newSet; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Make sure the set is compatible with the current one |
||
| 267 | * |
||
| 268 | * @param SetInterface $set |
||
| 269 | * |
||
| 270 | * @throws InvalidArgumentException |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | 8 | private function validate(SetInterface $set) |
|
| 275 | { |
||
| 276 | 8 | if (!$set->type()->equals($this->type)) { |
|
|
1 ignored issue
–
show
$this->type is of type object<Innmind\Immutable\StringPrimitive>, but the function expects a object<self>.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 277 | 4 | throw new InvalidArgumentException( |
|
| 278 | 4 | 'The 2 sets does not reference the same type' |
|
| 279 | ); |
||
| 280 | } |
||
| 281 | 4 | } |
|
| 282 | } |
||
| 283 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.