From 6a46080b8e958f7f6951943fb4352ad53cbe8514 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 29 May 2024 14:26:10 +0200 Subject: [PATCH 1/4] CS --- proofs/predicate.php | 28 ++++++++++++++-------------- src/Map/DoubleIndex.php | 1 - src/Map/Implementation.php | 1 - src/Map/ObjectKeys.php | 1 - src/Map/Primitive.php | 1 - src/Sequence/Defer.php | 1 - src/Sequence/Implementation.php | 1 - src/Sequence/Lazy.php | 1 - src/Sequence/Primitive.php | 1 - src/Set/Defer.php | 1 - src/Set/Implementation.php | 1 - src/Set/Lazy.php | 1 - src/Set/Primitive.php | 1 - tests/Map/DoubleIndexTest.php | 4 +--- tests/Map/ObjectKeysTest.php | 4 +--- tests/Map/PrimitiveTest.php | 4 +--- tests/MapTest.php | 2 -- tests/Sequence/DeferTest.php | 3 --- tests/Sequence/LazyTest.php | 3 --- tests/Sequence/PrimitiveTest.php | 3 --- tests/Set/DeferTest.php | 2 -- tests/Set/LazyTest.php | 2 -- tests/Set/PrimitiveTest.php | 2 -- tests/SetTest.php | 1 - tests/StrTest.php | 1 - 25 files changed, 17 insertions(+), 54 deletions(-) diff --git a/proofs/predicate.php b/proofs/predicate.php index 39ccf58..9f312a1 100644 --- a/proofs/predicate.php +++ b/proofs/predicate.php @@ -7,21 +7,21 @@ yield test( 'Or predicate', static function($assert) { - $array = new \SplFixedArray; + $array = new SplFixedArray; $assert->true( - Instance::of(\Countable::class) - ->or(Instance::of(\stdClass::class)) + Instance::of(Countable::class) + ->or(Instance::of(stdClass::class)) ($array), ); $assert->true( - Instance::of(\stdClass::class) - ->or(Instance::of(\Countable::class)) + Instance::of(stdClass::class) + ->or(Instance::of(Countable::class)) ($array), ); $assert->false( - Instance::of(\Throwable::class) - ->or(Instance::of(\Unknown::class)) + Instance::of(Throwable::class) + ->or(Instance::of(Unknown::class)) ($array), ); }, @@ -30,21 +30,21 @@ static function($assert) { yield test( 'And predicate', static function($assert) { - $array = new \SplFixedArray; + $array = new SplFixedArray; $assert->true( - Instance::of(\Countable::class) - ->and(Instance::of(\Traversable::class)) + Instance::of(Countable::class) + ->and(Instance::of(Traversable::class)) ($array), ); $assert->false( - Instance::of(\Throwable::class) - ->and(Instance::of(\Countable::class)) + Instance::of(Throwable::class) + ->and(Instance::of(Countable::class)) ($array), ); $assert->false( - Instance::of(\Countable::class) - ->and(Instance::of(\Throwable::class)) + Instance::of(Countable::class) + ->and(Instance::of(Throwable::class)) ($array), ); }, diff --git a/src/Map/DoubleIndex.php b/src/Map/DoubleIndex.php index b3b828c..002b682 100644 --- a/src/Map/DoubleIndex.php +++ b/src/Map/DoubleIndex.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, diff --git a/src/Map/Implementation.php b/src/Map/Implementation.php index 63af24d..8e2a733 100644 --- a/src/Map/Implementation.php +++ b/src/Map/Implementation.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Set, Sequence, Pair, diff --git a/src/Map/ObjectKeys.php b/src/Map/ObjectKeys.php index 3538854..3fb69b1 100644 --- a/src/Map/ObjectKeys.php +++ b/src/Map/ObjectKeys.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, diff --git a/src/Map/Primitive.php b/src/Map/Primitive.php index 0ff1a6d..c5464ee 100644 --- a/src/Map/Primitive.php +++ b/src/Map/Primitive.php @@ -5,7 +5,6 @@ use Innmind\Immutable\{ Map, - Str, Sequence, Set, Pair, diff --git a/src/Sequence/Defer.php b/src/Sequence/Defer.php index 392f3e1..094bdbe 100644 --- a/src/Sequence/Defer.php +++ b/src/Sequence/Defer.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, Accumulate, diff --git a/src/Sequence/Implementation.php b/src/Sequence/Implementation.php index 8685efc..7238474 100644 --- a/src/Sequence/Implementation.php +++ b/src/Sequence/Implementation.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, diff --git a/src/Sequence/Lazy.php b/src/Sequence/Lazy.php index 66c9439..88f7c49 100644 --- a/src/Sequence/Lazy.php +++ b/src/Sequence/Lazy.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, diff --git a/src/Sequence/Primitive.php b/src/Sequence/Primitive.php index dad7833..f4789b7 100644 --- a/src/Sequence/Primitive.php +++ b/src/Sequence/Primitive.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Map, Sequence, - Str, Set, Maybe, SideEffect, diff --git a/src/Set/Defer.php b/src/Set/Defer.php index 801a26a..45f6fd4 100644 --- a/src/Set/Defer.php +++ b/src/Set/Defer.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/src/Set/Implementation.php b/src/Set/Implementation.php index 1d85606..a6d33d7 100644 --- a/src/Set/Implementation.php +++ b/src/Set/Implementation.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/src/Set/Lazy.php b/src/Set/Lazy.php index 790de79..1f1034b 100644 --- a/src/Set/Lazy.php +++ b/src/Set/Lazy.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, RegisterCleanup, diff --git a/src/Set/Primitive.php b/src/Set/Primitive.php index 8f18b7c..9ff2819 100644 --- a/src/Set/Primitive.php +++ b/src/Set/Primitive.php @@ -7,7 +7,6 @@ Map, Sequence, Set, - Str, Maybe, SideEffect, }; diff --git a/tests/Map/DoubleIndexTest.php b/tests/Map/DoubleIndexTest.php index 132ee16..44e65d6 100644 --- a/tests/Map/DoubleIndexTest.php +++ b/tests/Map/DoubleIndexTest.php @@ -7,8 +7,6 @@ Map\DoubleIndex, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -20,7 +18,7 @@ public function testInterface() { $m = new DoubleIndex; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/Map/ObjectKeysTest.php b/tests/Map/ObjectKeysTest.php index a4ce325..19c50b6 100644 --- a/tests/Map/ObjectKeysTest.php +++ b/tests/Map/ObjectKeysTest.php @@ -8,8 +8,6 @@ Map\DoubleIndex, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -21,7 +19,7 @@ public function testInterface() { $m = new ObjectKeys; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/Map/PrimitiveTest.php b/tests/Map/PrimitiveTest.php index dcd3a01..23256ca 100644 --- a/tests/Map/PrimitiveTest.php +++ b/tests/Map/PrimitiveTest.php @@ -7,8 +7,6 @@ Map\Primitive, Map\Implementation, Map, - Pair, - Str, Set, Sequence, }; @@ -20,7 +18,7 @@ public function testInterface() { $m = new Primitive; - $this->assertInstanceOf(Map\Implementation::class, $m); + $this->assertInstanceOf(Implementation::class, $m); $this->assertInstanceOf(\Countable::class, $m); } diff --git a/tests/MapTest.php b/tests/MapTest.php index 434e295..14c3060 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -5,8 +5,6 @@ use Innmind\Immutable\{ Map, - Pair, - Str, Set, Sequence, }; diff --git a/tests/Sequence/DeferTest.php b/tests/Sequence/DeferTest.php index a591b9c..8081fe5 100644 --- a/tests/Sequence/DeferTest.php +++ b/tests/Sequence/DeferTest.php @@ -7,9 +7,6 @@ Sequence\Defer, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Sequence/LazyTest.php b/tests/Sequence/LazyTest.php index 465bcde..e7eba1e 100644 --- a/tests/Sequence/LazyTest.php +++ b/tests/Sequence/LazyTest.php @@ -7,9 +7,6 @@ Sequence\Lazy, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Sequence/PrimitiveTest.php b/tests/Sequence/PrimitiveTest.php index da3f75e..a886c36 100644 --- a/tests/Sequence/PrimitiveTest.php +++ b/tests/Sequence/PrimitiveTest.php @@ -7,9 +7,6 @@ Sequence\Primitive, Sequence\Implementation, Map, - Sequence, - Str, - Set, SideEffect, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; diff --git a/tests/Set/DeferTest.php b/tests/Set/DeferTest.php index aa02f97..f1676e6 100644 --- a/tests/Set/DeferTest.php +++ b/tests/Set/DeferTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Defer, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/Set/LazyTest.php b/tests/Set/LazyTest.php index 2612e66..d8f1fa7 100644 --- a/tests/Set/LazyTest.php +++ b/tests/Set/LazyTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Lazy, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/Set/PrimitiveTest.php b/tests/Set/PrimitiveTest.php index d2d00f8..5937aa3 100644 --- a/tests/Set/PrimitiveTest.php +++ b/tests/Set/PrimitiveTest.php @@ -6,9 +6,7 @@ use Innmind\Immutable\{ Set\Primitive, Set\Implementation, - Set, Map, - Str, Sequence, SideEffect, }; diff --git a/tests/SetTest.php b/tests/SetTest.php index 1418b61..0f0da2d 100644 --- a/tests/SetTest.php +++ b/tests/SetTest.php @@ -6,7 +6,6 @@ use Innmind\Immutable\{ Set, Map, - Str, Sequence, Predicate, }; diff --git a/tests/StrTest.php b/tests/StrTest.php index 1490ea3..3a79c92 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -8,7 +8,6 @@ Sequence, Map, Set, - Exception\InvalidRegex, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; From 871512c5841c0f14db2ca73598edaa566542e0a0 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 29 May 2024 14:28:27 +0200 Subject: [PATCH 2/4] add Set::unsorted() --- CHANGELOG.md | 6 ++++++ docs/SET.md | 10 ++++++++++ proofs/set.php | 18 ++++++++++++++++++ src/Set.php | 13 +++++++++++++ 4 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d04dfb6..19b4ef4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +- `Innmind\Immutable\Set::unsorted()` + ## 5.3.0 - 2023-11-06 ### Added diff --git a/docs/SET.md b/docs/SET.md index ebc123a..8d47fed 100644 --- a/docs/SET.md +++ b/docs/SET.md @@ -267,6 +267,16 @@ $sequence = Set::ints(1, 4, 2, 3)->sort(fn($a, $b) => $a <=> $b); $sequence->equals(Sequence::ints(1, 2, 3, 4)); ``` +## `->unsorted()` + +It will transform the set into an unordered sequence. + +```php +$sequence = Set::ints(1, 4, 2, 3)->unsorted(); +// is the same as +$sequence = Sequence::of(...Set::of(1, 4, 2, 3)->toList()); +``` + ## `->merge()` Create a new set with all the elements from both sets. diff --git a/proofs/set.php b/proofs/set.php index 38be0c5..7792ba7 100644 --- a/proofs/set.php +++ b/proofs/set.php @@ -34,4 +34,22 @@ static function($assert, $first, $rest) { $assert->same($rest, $packed[1]->toList()); }, ); + + yield proof( + 'Set::unsorted()', + given( + DataSet\Sequence::of(DataSet\Type::any()), + ), + static function($assert, $values) { + $set = Set::of(...$values); + $sequence = $set->unsorted(); + + $assert->true( + $sequence->matches($set->contains(...)), + ); + $assert->true( + $set->matches($sequence->contains(...)), + ); + }, + ); }; diff --git a/src/Set.php b/src/Set.php index 60e6656..e8a99b6 100644 --- a/src/Set.php +++ b/src/Set.php @@ -362,6 +362,19 @@ public function sort(callable $function): Sequence return $this->implementation->sort($function); } + /** + * Return an unsorted sequence + * + * @return Sequence + */ + public function unsorted(): Sequence + { + return $this + ->implementation + ->sequence() + ->toSequence(); + } + /** * Create a new set with elements of both sets * From 591011491830ada088beac0b6d5dfe38ff1a3618 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 29 May 2024 14:34:17 +0200 Subject: [PATCH 3/4] discard psalm errors --- src/Accumulate.php | 4 ++++ src/Map/Primitive.php | 5 ++++- src/Sequence/Lazy.php | 1 + src/Sequence/Primitive.php | 6 +++++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Accumulate.php b/src/Accumulate.php index 634129a..829b3a6 100644 --- a/src/Accumulate.php +++ b/src/Accumulate.php @@ -54,7 +54,9 @@ public function key(): mixed public function next(): void { + /** @psalm-suppress InaccessibleProperty */ \next($this->keys); + /** @psalm-suppress InaccessibleProperty */ \next($this->values); if ($this->reachedCacheEnd()) { @@ -65,7 +67,9 @@ public function next(): void public function rewind(): void { + /** @psalm-suppress InaccessibleProperty */ \reset($this->keys); + /** @psalm-suppress InaccessibleProperty */ \reset($this->values); } diff --git a/src/Map/Primitive.php b/src/Map/Primitive.php index c5464ee..8b21ec0 100644 --- a/src/Map/Primitive.php +++ b/src/Map/Primitive.php @@ -327,7 +327,10 @@ public function reduce($carry, callable $reducer) public function empty(): bool { - /** @psalm-suppress MixedArgumentTypeCoercion */ + /** + * @psalm-suppress InaccessibleProperty + * @psalm-suppress MixedArgumentTypeCoercion + */ \reset($this->values); /** @psalm-suppress MixedArgumentTypeCoercion */ diff --git a/src/Sequence/Lazy.php b/src/Sequence/Lazy.php index 88f7c49..914a994 100644 --- a/src/Sequence/Lazy.php +++ b/src/Sequence/Lazy.php @@ -272,6 +272,7 @@ public function contains($element): bool /** @psalm-suppress ImpureFunctionCall */ $generator = ($this->values)($register); + /** @psalm-suppress ImpureMethodCall */ foreach ($generator as $value) { if ($value === $element) { /** @psalm-suppress ImpureMethodCall */ diff --git a/src/Sequence/Primitive.php b/src/Sequence/Primitive.php index f4789b7..6af6821 100644 --- a/src/Sequence/Primitive.php +++ b/src/Sequence/Primitive.php @@ -384,7 +384,10 @@ public function intersect(Implementation $sequence): self public function sort(callable $function): self { $self = clone $this; - /** @psalm-suppress ImpureFunctionCall */ + /** + * @psalm-suppress InaccessibleProperty + * @psalm-suppress ImpureFunctionCall + */ \usort($self->values, $function); return $self; @@ -527,6 +530,7 @@ public function aggregate(callable $map, callable $exfiltrate): self $values = $aggregate(static fn($a, $b) => $exfiltrate($map($a, $b))->iterator()); $aggregated = []; + /** @psalm-suppress ImpureMethodCall */ foreach ($values as $value) { $aggregated[] = $value; } From 32ceacb4861d5a705101985b6084531e82ba2d87 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 29 May 2024 14:37:42 +0200 Subject: [PATCH 4/4] specify next release --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19b4ef4..af94f56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [Unreleased] +## 5.4.0 - 2024-05-29 ### Added