Skip to content

Commit 671e436

Browse files
committed
Add PhpStan type annotations
1 parent 5f5ddb6 commit 671e436

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+749
-352
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"squizlabs/php_codesniffer": "~3.0",
2424
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.5",
25-
"friendsofphp/php-cs-fixer": "^2.17"
25+
"friendsofphp/php-cs-fixer": "^2.17",
26+
"phpstan/phpstan": "^1.10"
2627
},
2728
"autoload": {
2829
"psr-4": {

src/Functional/Ary.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@
1111
namespace Functional;
1212

1313
use Functional\Exceptions\InvalidArgumentException;
14-
use Traversable;
1514

1615
/**
1716
* Call $func with only abs($count) arguments, taken either from the
1817
* left or right depending on the sign
18+
*
19+
* @template V
20+
* @template T
21+
*
22+
* @param callable(T...):V $func
23+
* @param non-zero-int $count
24+
*
25+
* @return callable(T...):V
26+
*
1927
* @no-named-arguments
2028
*/
2129
function ary(callable $func, int $count): callable
@@ -25,7 +33,7 @@ function ary(callable $func, int $count): callable
2533
return function (...$args) use ($func, $count) {
2634
if ($count > 0) {
2735
return $func(...take_left($args, $count));
28-
} else if ($count < 0) {
36+
} else {
2937
return $func(...take_right($args, -$count));
3038
}
3139
};

src/Functional/Average.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
namespace Functional;
1212

1313
use Functional\Exceptions\InvalidArgumentException;
14-
use Traversable;
1514

1615
/**
1716
* Returns the average of all numeric values in the array or null if no numeric value was found
1817
*
19-
* @param Traversable|array $collection
20-
* @return null|float|int
18+
* @param iterable<mixed> $collection
19+
*
20+
* @return ($collection is iterable<int> ? float|int : (
21+
* $collection is iterable<float> ? float : (
22+
* $collection is iterable<int|float|numeric-string> ? float|int : null
23+
* )
24+
* ))
25+
*
2126
* @no-named-arguments
2227
*/
2328
function average($collection)

src/Functional/ButLast.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
namespace Functional;
1212

1313
use Functional\Exceptions\InvalidArgumentException;
14-
use Traversable;
1514

1615
/**
1716
* Returns an array containing the elements of the list without its last element.
1817
*
19-
* @param Traversable|array $collection
20-
* @return array
18+
* @template T
19+
*
20+
* @param iterable<T> $collection
21+
*
22+
* @return iterable<T>
23+
*
2124
* @no-named-arguments
2225
*/
2326
function but_last($collection)

src/Functional/Capture.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
namespace Functional;
1212

1313
/**
14-
* Return a new function that captures the return value of $callback in $result and returns the callbacks return value
14+
* Return a new function that captures the return value of $callback in $result and returns the callback's return value
15+
*
16+
* @template T
17+
*
18+
* @param callable():T $callback
19+
* @param T $result
20+
*
21+
* @return callable():T
1522
*
16-
* @param callable $callback
17-
* @param mixed $result
18-
* @return callable
1923
* @no-named-arguments
2024
*/
2125
function capture(callable $callback, &$result)

src/Functional/CompareObjectHashOn.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
/**
1414
* Returns a comparison function that can be used with e.g. `usort()`
1515
*
16-
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
17-
* @param callable $keyFunction A function that takes an argument and returns the value that should be compared
18-
* @return callable
16+
* @template V
17+
* @template R of int
18+
*
19+
* @param callable(string,string):R $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
20+
* @param null|callable(V):string $keyFunction A function that takes an argument and returns the value that should be compared
21+
*
22+
* @return callable(object,object):R
23+
*
1924
* @no-named-arguments
2025
*/
2126
function compare_object_hash_on(callable $comparison, callable $keyFunction = null)

src/Functional/CompareOn.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
/**
1414
* Returns a comparison function that can be used with e.g. `usort()`
1515
*
16-
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
17-
* @param callable $reducer A function that takes an argument and returns the value that should be compared
18-
* @return callable
16+
* @template V
17+
* @template V2
18+
* @template R of int
19+
*
20+
* @param callable(V,V):R $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
21+
* @param null|callable(V2):V $reducer A function that takes an argument and returns the value that should be compared
22+
*
23+
* @return ($reducer is null ? callable(V,V):R : callable(V2,V2):R)
24+
*
1925
* @no-named-arguments
2026
*/
2127
function compare_on(callable $comparison, callable $reducer = null)

src/Functional/Concat.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
/**
1414
* Concatenates zero or more strings
1515
*
16-
* @param string[] ...$strings
16+
* @param string ...$strings
17+
*
1718
* @return string
19+
*
1820
* @no-named-arguments
1921
*/
2022
function concat(string ...$strings)

src/Functional/ConstFunction.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
/**
1414
* Wrap value within a function, which will return it, without any modifications.
1515
*
16-
* @param mixed $value
17-
* @return callable
16+
* @template V
17+
*
18+
* @param V $value
19+
*
20+
* @return callable():V
21+
*
1822
* @no-named-arguments
1923
*/
2024
function const_function($value)

src/Functional/Contains.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
namespace Functional;
1212

1313
use Functional\Exceptions\InvalidArgumentException;
14-
use Traversable;
1514

1615
/**
1716
* Returns true if the collection contains the given value. If the third parameter is
1817
* true values will be compared in strict mode
1918
*
20-
* @param Traversable|array $collection
21-
* @param mixed $value
19+
* @template V
20+
* @template V2 of V
21+
*
22+
* @param iterable<V> $collection
23+
* @param V2 $value
2224
* @param bool $strict
25+
*
2326
* @return bool
27+
*
2428
* @no-named-arguments
2529
*/
2630
function contains($collection, $value, $strict = true)

0 commit comments

Comments
 (0)