-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract read-only part of Collection
This allows us to mark the extracted interface as covariant, thus allowing consumers to ensure through static analysis that a collection will not be altered in a way that would no longer make it a collection of items of a specific type. Closes #298
- Loading branch information
Showing
3 changed files
with
131 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Doctrine\Common\Collections; | ||
|
||
use Countable; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* @psalm-template TKey of array-key | ||
* @template-covariant T | ||
* @template-extends IteratorAggregate<TKey, T> | ||
*/ | ||
interface ReadableCollection extends Countable, IteratorAggregate | ||
{ | ||
/** | ||
* Checks whether the collection is empty (contains no elements). | ||
* | ||
* @return bool TRUE if the collection is empty, FALSE otherwise. | ||
*/ | ||
public function isEmpty(); | ||
|
||
/** | ||
* Checks whether the collection contains an element with the specified key/index. | ||
* | ||
* @param string|int $key The key/index to check for. | ||
* @psalm-param TKey $key | ||
* | ||
* @return bool TRUE if the collection contains an element with the specified key/index, | ||
* FALSE otherwise. | ||
*/ | ||
public function containsKey($key); | ||
|
||
/** | ||
* Gets the element at the specified key/index. | ||
* | ||
* @param string|int $key The key/index of the element to retrieve. | ||
* @psalm-param TKey $key | ||
* | ||
* @return mixed | ||
* @psalm-return T|null | ||
*/ | ||
public function get($key); | ||
|
||
/** | ||
* Gets all keys/indices of the collection. | ||
* | ||
* @return int[]|string[] The keys/indices of the collection, in the order of the corresponding | ||
* elements in the collection. | ||
* @psalm-return TKey[] | ||
*/ | ||
public function getKeys(); | ||
|
||
/** | ||
* Gets all values of the collection. | ||
* | ||
* @return mixed[] The values of all elements in the collection, in the | ||
* order they appear in the collection. | ||
* @psalm-return list<T> | ||
*/ | ||
public function getValues(); | ||
|
||
/** | ||
* Gets a native PHP array representation of the collection. | ||
* | ||
* @return mixed[] | ||
* @psalm-return array<TKey,T> | ||
*/ | ||
public function toArray(); | ||
|
||
/** | ||
* Sets the internal iterator to the first element in the collection and returns this element. | ||
* | ||
* @return mixed | ||
* @psalm-return T|false | ||
*/ | ||
public function first(); | ||
|
||
/** | ||
* Sets the internal iterator to the last element in the collection and returns this element. | ||
* | ||
* @return mixed | ||
* @psalm-return T|false | ||
*/ | ||
public function last(); | ||
|
||
/** | ||
* Gets the key/index of the element at the current iterator position. | ||
* | ||
* @return int|string|null | ||
* @psalm-return TKey|null | ||
*/ | ||
public function key(); | ||
|
||
/** | ||
* Gets the element of the collection at the current iterator position. | ||
* | ||
* @return mixed | ||
* @psalm-return T|false | ||
*/ | ||
public function current(); | ||
|
||
/** | ||
* Moves the internal iterator position to the next element and returns this element. | ||
* | ||
* @return mixed | ||
* @psalm-return T|false | ||
*/ | ||
public function next(); | ||
|
||
/** | ||
* Extracts a slice of $length elements starting at position $offset from the Collection. | ||
* | ||
* If $length is null it returns all elements from $offset to the end of the Collection. | ||
* Keys have to be preserved by this method. Calling this method will only return the | ||
* selected slice and NOT change the elements contained in the collection slice is called on. | ||
* | ||
* @param int $offset The offset to start from. | ||
* @param int|null $length The maximum number of elements to return, or null for no limit. | ||
* | ||
* @return mixed[] | ||
* @psalm-return array<TKey,T> | ||
*/ | ||
public function slice($offset, $length = null); | ||
} |