Skip to content

Commit 6ce78ab

Browse files
committed
Collection: Add and implement isEmpty(), add PHPStan assertions
1 parent f6c9b61 commit 6ce78ab

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Toolkit/Collection/ReadOnlyCollectionTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function __construct($items = [])
3535
$this->Items = $this->getItemsArray($items);
3636
}
3737

38+
/**
39+
* @inheritDoc
40+
*/
41+
public function isEmpty(): bool
42+
{
43+
return !$this->Items;
44+
}
45+
3846
/**
3947
* @inheritDoc
4048
*/

src/Toolkit/Contract/Collection/CollectionInterface.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use OutOfRangeException;
1212

1313
/**
14-
* An array-like collection of items
15-
*
1614
* @api
1715
*
1816
* @template TKey of array-key
@@ -60,6 +58,15 @@ interface CollectionInterface extends
6058
*/
6159
public function __construct($items = []);
6260

61+
/**
62+
* Check if the collection is empty
63+
*
64+
* @phpstan-assert-if-false non-empty-array<TKey,TValue> $this->all()
65+
* @phpstan-assert-if-false !null $this->first()
66+
* @phpstan-assert-if-false !null $this->last()
67+
*/
68+
public function isEmpty(): bool;
69+
6370
/**
6471
* Add or replace an item with a given key
6572
*
@@ -73,6 +80,9 @@ public function set($key, $value);
7380
* Check if an item with a given key exists
7481
*
7582
* @param TKey $key
83+
* @phpstan-assert-if-true non-empty-array<TKey,TValue> $this->all()
84+
* @phpstan-assert-if-true !null $this->first()
85+
* @phpstan-assert-if-true !null $this->last()
7686
*/
7787
public function has($key): bool;
7888

@@ -246,6 +256,9 @@ public function slice(int $offset, ?int $length = null);
246256
* Check if a value is in the collection
247257
*
248258
* @param TValue $value
259+
* @phpstan-assert-if-true non-empty-array<TKey,TValue> $this->all()
260+
* @phpstan-assert-if-true !null $this->first()
261+
* @phpstan-assert-if-true !null $this->last()
249262
*/
250263
public function hasValue($value, bool $strict = false): bool;
251264

0 commit comments

Comments
 (0)