Skip to content

Commit

Permalink
Merge pull request #313 from lptn/collection-memoizemethodcallresults
Browse files Browse the repository at this point in the history
Add assertions to Collections
  • Loading branch information
lptn authored Jan 31, 2023
2 parents 9e3532e + c6f534e commit 7f0d2fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
28 changes: 28 additions & 0 deletions stubs/Support/Collection.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Get the first item from the collection passing the given truth test.
*
* @psalm-mutation-free
*
* @template TFirstDefault
* @template TFirstDefaultFromClosure
*
Expand All @@ -32,6 +34,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
public function first(callable $callback = null, $default = null) {}

/**
* @psalm-mutation-free
*
* @template TGetDefault
* @template TGetClosureReturn
*
Expand Down Expand Up @@ -63,6 +67,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
public function pull($key, $default = null) {}

/**
* @psalm-mutation-free
*
* @param TKey $number
* @param (callable(TValue): int)|int|null $number
* @return (TKey is null ? TValue : static<int, TValue>)
Expand All @@ -72,6 +78,8 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
public function random($number = null) {}

/**
* @psalm-mutation-free
*
* @param TValue|(callable(TValue,TKey): bool) $value
* @param bool $strict
* @return TKey|false
Expand All @@ -88,4 +96,24 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
* )
*/
public function shift($count = 1) {}

/**
* Determine if the collection is empty or not.
*
* @psalm-mutation-free
* @psalm-assert-if-true null $this->first()
* @psalm-assert-if-true 0 $this->count()
* @psalm-assert-if-false positive-int $this->count()
* @return bool
*/
public function isEmpty() {}

/**
* Determine if the collection is not empty.
*
* @psalm-mutation-free
* @psalm-assert-if-false null $this->first()
* @return bool
*/
public function isNotEmpty() {}
}
18 changes: 15 additions & 3 deletions tests/acceptance/CollectionTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,25 @@ Feature: Collection types
return $this->getCollection()->all();
}
/**
* @return Collection<int, string>
*/
/** @return Collection<int, string> */
public function putTest(): Collection
{
return $this->getCollection()->put(5, 'five');
}
public function isEmpty_assertions_works(): null
{
$collection = $this->getCollection();
return $collection->isEmpty() ? $collection->first() : null;
}
public function isNotEmpty_assertions_works(): null
{
$collection = $this->getCollection();
return $collection->isNotEmpty() ? null : $collection->first();
}
}
"""
When I run Psalm
Expand Down

0 comments on commit 7f0d2fd

Please sign in to comment.