Skip to content

Commit 8f88454

Browse files
committed
Merge branch 'develop'
* develop: specify next release add keep() method suppress psalm error add ->exclude() methods be more explicit with the integers force type declaration remove unnecessary annotations discard impure function calls update psalm
2 parents 59fe265 + ecea75e commit 8f88454

32 files changed

+571
-46
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## 4.6.0 - 2022-10-08
4+
5+
## Added
6+
7+
- `Innmind\Immutable\Map::exclude()`
8+
- `Innmind\Immutable\Maybe::exclude()`
9+
- `Innmind\Immutable\Maybe::keep()`
10+
- `Innmind\Immutable\Sequence::exclude()`
11+
- `Innmind\Immutable\Sequence::keep()`
12+
- `Innmind\Immutable\Set::exclude()`
13+
- `Innmind\Immutable\Set::keep()`
14+
- `Innmind\Immutable\Predicate`
15+
- `Innmind\Immutable\Predicate\Instance`
16+
317
## 4.5.0 - 2022-09-18
418

519
### Added

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
"require-dev": {
3333
"phpunit/phpunit": "~9.0",
34-
"vimeo/psalm": "~4.1 <4.16",
34+
"vimeo/psalm": "~4.28",
3535
"innmind/black-box": "^4.4",
3636
"innmind/coding-standard": "~2.0"
3737
},

docs/MAP.md

+10
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ $map = $map->filter(fn($key, $value) => ($key + $value) % 2 === 0);
9898
$map->equals(Map::of([1, 1]));
9999
```
100100

101+
## `->exclude()`
102+
103+
Removes the pairs from the map that match the given predicate.
104+
105+
```php
106+
$map = Map::of([1, 1], [3, 2]);
107+
$map = $map->exclude(fn($key, $value) => ($key + $value) % 2 === 0);
108+
$map->equals(Map::of([3, 2]));
109+
```
110+
101111
## `->foreach()`
102112

103113
Use this method to call a function for each pair of the map. Since this structure is immutable it returns a `SideEffect` object, as its name suggest it is the only place acceptable to create side effects.

docs/MAYBE.md

+8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ $validDsn = $dsn->filter(fn(string $url): bool => \filter_var($url, \FILTER_VALI
123123

124124
`$validDsn` will contain either a valid url or nothing.
125125

126+
## `->keep()`
127+
128+
This is similar to `->filter()` with the advantage of psalm understanding the type in the new `Maybe`.
129+
130+
## `->exclude()`
131+
132+
This is the inverse of the `->filter()` method.
133+
126134
## `->either()`
127135

128136
This returns an [`Either`](EITHER.md) containing the value on the right side and `null` on the left side.

docs/SEQUENCE.md

+20
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ $sequence = Sequence::ints(1, 2, 3, 4)->filter(fn($i) => $i % 2 === 0);
158158
$sequence->equals(Sequence::ints(2, 4));
159159
```
160160

161+
## `->keep()`
162+
163+
This is similar to `->filter()` with the advantage of psalm understanding the type in the new `Sequence`.
164+
165+
```php
166+
use Innmind\Immutable\Predicate\Instance;
167+
168+
$sequence = Sequence::of(null, new \stdClass, 'foo')->keep(Instance::of('stdClass'));
169+
$sequence; // Sequence<stdClass>
170+
```
171+
172+
## `->exclude()`
173+
174+
Removes elements from the sequence that match the given predicate.
175+
176+
```php
177+
$sequence = Sequence::ints(1, 2, 3, 4)->filter(fn($i) => $i % 2 === 0);
178+
$sequence->equals(Sequence::ints(1, 3));
179+
```
180+
161181
## `->foreach()`
162182

163183
Use this method to call a function for each element of the sequence. Since this structure is immutable it returns a `SideEffect` object, as its name suggest it is the only place acceptable to create side effects.

docs/SET.md

+20
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ $set = Set::ints(1, 2, 3, 4)->filter(fn($i) => $i % 2 === 0);
148148
$set->equals(Set::ints(2, 4));
149149
```
150150

151+
## `->keep()`
152+
153+
This is similar to `->filter()` with the advantage of psalm understanding the type in the new `Set`.
154+
155+
```php
156+
use Innmind\Immutable\Predicate\Instance;
157+
158+
$set = Set::of(null, new \stdClass, 'foo')->keep(Instance::of('stdClass'));
159+
$set; // Set<stdClass>
160+
```
161+
162+
## `->exclude()`
163+
164+
Removes elements from the set that match the given predicate.
165+
166+
```php
167+
$set = Set::ints(1, 2, 3, 4)->filter(fn($i) => $i % 2 === 0);
168+
$set->equals(Set::ints(1, 3));
169+
```
170+
151171
## `->foreach()`
152172

153173
Use this method to call a function for each element of the set. Since this structure is immutable it returns a `SideEffect` object, as its name suggest it is the only place acceptable to create side effects.

psalm.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="true"
3+
errorLevel="1"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"
@@ -12,8 +12,4 @@
1212
<directory name="vendor" />
1313
</ignoreFiles>
1414
</projectFiles>
15-
16-
<issueHandlers>
17-
<LessSpecificReturnType errorLevel="info" />
18-
</issueHandlers>
1915
</psalm>

src/Either/Left.php

+3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ public function flatMap(callable $map): Either
4848

4949
public function leftMap(callable $map): self
5050
{
51+
/** @psalm-suppress ImpureFunctionCall */
5152
return new self($map($this->value));
5253
}
5354

5455
public function match(callable $right, callable $left)
5556
{
57+
/** @psalm-suppress ImpureFunctionCall */
5658
return $left($this->value);
5759
}
5860

5961
public function otherwise(callable $otherwise): Either
6062
{
63+
/** @psalm-suppress ImpureFunctionCall */
6164
return $otherwise($this->value);
6265
}
6366

src/Either/Right.php

+5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public function __construct($value)
3030

3131
public function map(callable $map): self
3232
{
33+
/** @psalm-suppress ImpureFunctionCall */
3334
return new self($map($this->value));
3435
}
3536

3637
public function flatMap(callable $map): Either
3738
{
39+
/** @psalm-suppress ImpureFunctionCall */
3840
return $map($this->value);
3941
}
4042

@@ -53,6 +55,7 @@ public function leftMap(callable $map): self
5355

5456
public function match(callable $right, callable $left)
5557
{
58+
/** @psalm-suppress ImpureFunctionCall */
5659
return $right($this->value);
5760
}
5861

@@ -63,10 +66,12 @@ public function otherwise(callable $otherwise): Either
6366

6467
public function filter(callable $predicate, callable $otherwise): Implementation
6568
{
69+
/** @psalm-suppress ImpureFunctionCall */
6670
if ($predicate($this->value) === true) {
6771
return $this;
6872
}
6973

74+
/** @psalm-suppress ImpureFunctionCall */
7075
return new Left($otherwise());
7176
}
7277

src/Map.php

+19
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@ public static function of(array ...$pairs): self
5757
return $self;
5858
}
5959

60+
/**
61+
* @return 0|positive-int
62+
*/
6063
public function size(): int
6164
{
6265
return $this->implementation->size();
6366
}
6467

68+
/**
69+
* @return 0|positive-int
70+
*/
6571
public function count(): int
6672
{
6773
return $this->size();
@@ -134,6 +140,19 @@ public function filter(callable $predicate): self
134140
return new self($this->implementation->filter($predicate));
135141
}
136142

143+
/**
144+
* Exclude elements that match the predicate
145+
*
146+
* @param callable(T, S): bool $predicate
147+
*
148+
* @return self<T, S>
149+
*/
150+
public function exclude(callable $predicate): self
151+
{
152+
/** @psalm-suppress MixedArgument */
153+
return $this->filter(static fn($key, $value) => !$predicate($key, $value));
154+
}
155+
137156
/**
138157
* Run the given function for each element of the map
139158
*

src/Map/DoubleIndex.php

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function filter(callable $predicate): self
150150
public function foreach(callable $function): SideEffect
151151
{
152152
foreach ($this->pairs->iterator() as $pair) {
153+
/** @psalm-suppress ImpureFunctionCall */
153154
$function($pair->key(), $pair->value());
154155
}
155156

@@ -169,6 +170,7 @@ public function groupBy(callable $discriminator): Map
169170
$groups = Map::of();
170171

171172
foreach ($this->pairs->iterator() as $pair) {
173+
/** @psalm-suppress ImpureFunctionCall */
172174
$key = $discriminator($pair->key(), $pair->value());
173175

174176
$group = $groups->get($key)->match(
@@ -259,6 +261,7 @@ public function partition(callable $predicate): Map
259261
$key = $pair->key();
260262
$value = $pair->value();
261263

264+
/** @psalm-suppress ImpureFunctionCall */
262265
$return = $predicate($key, $value);
263266

264267
if ($return === true) {
@@ -281,6 +284,7 @@ public function partition(callable $predicate): Map
281284
public function reduce($carry, callable $reducer)
282285
{
283286
foreach ($this->pairs->iterator() as $pair) {
287+
/** @psalm-suppress ImpureFunctionCall */
284288
$carry = $reducer($carry, $pair->key(), $pair->value());
285289
}
286290

src/Map/Implementation.php

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ interface Implementation extends \Countable
3131
*/
3232
public function __invoke($key, $value): self;
3333

34+
/**
35+
* @return 0|positive-int
36+
*/
3437
public function size(): int;
3538

3639
/**

src/Map/ObjectKeys.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public static function of($key, $value): Maybe
7373

7474
public function size(): int
7575
{
76-
/** @psalm-suppress ImpureMethodCall */
76+
/**
77+
* @psalm-suppress ImpureMethodCall
78+
* @var 0|positive-int
79+
*/
7780
return $this->values->count();
7881
}
7982

@@ -174,6 +177,7 @@ public function filter(callable $predicate): self
174177
/** @var S $v */
175178
$v = $this->values[$k];
176179

180+
/** @psalm-suppress ImpureFunctionCall */
177181
if ($predicate($key, $v) === true) {
178182
$values[$k] = $v;
179183
}
@@ -197,6 +201,7 @@ public function foreach(callable $function): SideEffect
197201
*/
198202
$v = $this->values[$k];
199203

204+
/** @psalm-suppress ImpureFunctionCall */
200205
$function($key, $v);
201206
}
202207

@@ -225,6 +230,7 @@ public function groupBy(callable $discriminator): Map
225230
*/
226231
$v = $this->values[$k];
227232

233+
/** @psalm-suppress ImpureFunctionCall */
228234
$discriminant = $discriminator($key, $v);
229235

230236
$group = $groups->get($discriminant)->match(
@@ -282,7 +288,7 @@ public function map(callable $function): self
282288
*/
283289
$v = $this->values[$k];
284290

285-
/** @psalm-suppress ImpureMethodCall */
291+
/** @psalm-suppress ImpureFunctionCall */
286292
$values[$k] = $function($key, $v);
287293
}
288294

@@ -346,6 +352,7 @@ public function partition(callable $predicate): Map
346352
*/
347353
$v = $this->values[$k];
348354

355+
/** @psalm-suppress ImpureFunctionCall */
349356
$return = $predicate($key, $v);
350357

351358
if ($return === true) {
@@ -377,6 +384,7 @@ public function reduce($carry, callable $reducer)
377384
*/
378385
$v = $this->values[$k];
379386

387+
/** @psalm-suppress ImpureFunctionCall */
380388
$carry = $reducer($carry, $key, $v);
381389
}
382390

@@ -404,6 +412,7 @@ public function find(callable $predicate): Maybe
404412
*/
405413
$v = $this->values[$k];
406414

415+
/** @psalm-suppress ImpureFunctionCall */
407416
if ($predicate($key, $v)) {
408417
return Maybe::just(new Pair($key, $v));
409418
}

src/Map/Primitive.php

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function filter(callable $predicate): self
160160
$values = [];
161161

162162
foreach ($this->values as $k => $v) {
163+
/** @psalm-suppress ImpureFunctionCall */
163164
if ($predicate($k, $v) === true) {
164165
$values[$k] = $v;
165166
}
@@ -174,6 +175,7 @@ public function filter(callable $predicate): self
174175
public function foreach(callable $function): SideEffect
175176
{
176177
foreach ($this->values as $k => $v) {
178+
/** @psalm-suppress ImpureFunctionCall */
177179
$function($k, $v);
178180
}
179181

@@ -193,6 +195,7 @@ public function groupBy(callable $discriminator): Map
193195
$groups = Map::of();
194196

195197
foreach ($this->values as $key => $value) {
198+
/** @psalm-suppress ImpureFunctionCall */
196199
$discriminant = $discriminator($key, $value);
197200

198201
$group = $groups->get($discriminant)->match(
@@ -241,6 +244,7 @@ public function map(callable $function): self
241244
$values = [];
242245

243246
foreach ($this->values as $k => $v) {
247+
/** @psalm-suppress ImpureFunctionCall */
244248
$values[$k] = $function($k, $v);
245249
}
246250

@@ -289,6 +293,7 @@ public function partition(callable $predicate): Map
289293
$falsy = $this->clearMap();
290294

291295
foreach ($this->values as $k => $v) {
296+
/** @psalm-suppress ImpureFunctionCall */
292297
$return = $predicate($k, $v);
293298

294299
if ($return === true) {
@@ -311,6 +316,7 @@ public function partition(callable $predicate): Map
311316
public function reduce($carry, callable $reducer)
312317
{
313318
foreach ($this->values as $k => $v) {
319+
/** @psalm-suppress ImpureFunctionCall */
314320
$carry = $reducer($carry, $k, $v);
315321
}
316322

@@ -329,6 +335,7 @@ public function empty(): bool
329335
public function find(callable $predicate): Maybe
330336
{
331337
foreach ($this->values as $k => $v) {
338+
/** @psalm-suppress ImpureFunctionCall */
332339
if ($predicate($k, $v)) {
333340
return Maybe::just(new Pair($k, $v));
334341
}

0 commit comments

Comments
 (0)