Skip to content

Commit

Permalink
Add phpstan typing annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
KmeCnin committed Nov 24, 2023
1 parent 5f5ddb6 commit bbb6e31
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 45 deletions.
10 changes: 7 additions & 3 deletions src/Functional/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* Iterates over a collection of elements, yielding each in turn to a callback function. Each invocation of $callback
* is called with three arguments: (element, index, collection)
*
* @param Traversable|array $collection
* @param callable $callback
* @return null
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):void $callback
*
* @return void
* @no-named-arguments
*/
function each($collection, callable $callback)
Expand Down
8 changes: 6 additions & 2 deletions src/Functional/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
* Returns true if every value in the collection passes the callback truthy test. Opposite of Functional\none().
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return bool
* @no-named-arguments
*/
Expand Down
10 changes: 7 additions & 3 deletions src/Functional/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
/**
* Alias of Functional\select()
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return array<K,V>
* @no-named-arguments
*/
function filter($collection, callable $callback)
Expand Down
10 changes: 7 additions & 3 deletions src/Functional/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
* function returns as soon as it finds an acceptable element, and doesn't traverse the entire collection. Callback
* arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
* @template T
*
* @param iterable<T> $collection
* @param null|callable(T):bool $callback
*
* @return ($collection is non-empty-array ? ($callback is null ? T : T|null) : T|null)
*
* @no-named-arguments
*/
function first($collection, callable $callback = null)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/FlatMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
* then flat_map(collection, callback) will return [1,2,3,[4]]
* while flatten(map(collection, callback)) will return [1,2,3,4]
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):(V2|iterable<V2>) $callback
*
* @return array<K,V2>
* @no-named-arguments
*/
function flat_map($collection, callable $callback)
Expand Down
8 changes: 6 additions & 2 deletions src/Functional/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
/**
* Return value itself, without any modifications.
*
* @param mixed $value
* @return mixed
* @template T
*
* @param T $value
*
* @return T
*
* @no-named-arguments
*/
function id($value)
Expand Down
10 changes: 7 additions & 3 deletions src/Functional/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* Looks through each element in the collection, returning the last one that passes a truthy test (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
* @template T
*
* @param iterable<T> $collection
* @param null|callable(T):bool $callback
*
* @return T|null
*
* @no-named-arguments
*/
function last($collection, callable $callback = null)
Expand Down
12 changes: 9 additions & 3 deletions src/Functional/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
* Produces a new array of elements by mapping each element in collection through a transformation function (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):V2 $callback
*
* @return array<K,V2>
*
* @no-named-arguments
*/
function map($collection, callable $callback)
Expand Down
14 changes: 10 additions & 4 deletions src/Functional/ReduceLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
use Traversable;

/**
* @param Traversable|array $collection
* @param callable $callback
* @param mixed $initial
* @return mixed
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>,V2):V2 $callback
* @param V2 $initial
*
* @return V2
*
* @no-named-arguments
*/
function reduce_left($collection, callable $callback, $initial = null)
Expand Down
12 changes: 9 additions & 3 deletions src/Functional/Reindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
* Produces a new array of elements by assigning the values to keys generated by a transformation function (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template K2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):K2 $callback
*
* @return array<K2,V>
*
* @no-named-arguments
*/
function reindex($collection, callable $callback)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Looks through each element in the list, returning an array of all the elements that pass a truthy test (callback).
* Opposite is Functional\reject(). Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @return array
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return array<K,V>
*
* @no-named-arguments
*/
function select($collection, callable $callback = null)
Expand Down
9 changes: 7 additions & 2 deletions src/Functional/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Returns true if some of the elements in the collection pass the callback truthy test. Short-circuits and stops
* traversing the collection if a truthy element is found. Callback arguments will be value, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return bool
*
* @no-named-arguments
*/
function some($collection, callable $callback = null)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
/**
* Takes a collection and returns the sum of the elements
*
* @param Traversable|array $collection
* @param integer|float $initial
* @return integer|float
* @template K
* @template V of int|float
*
* @param iterable<K,V> $collection
* @param V $initial
*
* @return V
*
* @no-named-arguments
*/
function sum($collection, $initial = 0)
Expand Down
13 changes: 9 additions & 4 deletions src/Functional/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
/**
* Returns an array of unique elements
*
* @param Traversable|array $collection
* @param callable $callback
* @param bool $strict
* @return array
* @template K
* @template V
* @template D
*
* @param iterable<K,V> $collection
* @param null|callable(V,K,iterable<K,V>):D $callback
*
* @return array<K,V>
*
* @no-named-arguments
*/
function unique($collection, callable $callback = null, $strict = true)
Expand Down
15 changes: 11 additions & 4 deletions src/Functional/With.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
/**
* Invoke a callback on a value if the value is not null
*
* @param mixed $value
* @param callable $callback
* @template V
* @template V2
* @template V3
* @template V4
*
* @param V|null|callable():V4 $value
* @param callable(V):V2 $callback
* @param bool $invokeValue Set to false to not invoke $value if it is a callable. Will be removed in 2.0
* @param mixed $default The default value to return if $value is null
* @return mixed
* @param V3 $default The default value to return if $value is null
*
* @return ($value is null ? V3 : ($value is callable ? V4 : V2))
*
* @no-named-arguments
*/
function with($value, callable $callback, $invokeValue = true, $default = null)
Expand Down

0 comments on commit bbb6e31

Please sign in to comment.