Skip to content

Commit

Permalink
Fix initial errors (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Dec 19, 2019
1 parent df0e516 commit dff3d19
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor/
composer.lock
phpcs.xml
phpunit.xml
/lsp/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ install:
script:
- composer tests
- composer coding-style
- composer types
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
}
],
"require": {
"php": "~7"
"php": "~7",
"vimeo/psalm": "^3.7"
},
"require-dev": {
"phpunit/phpunit": "~7",
Expand Down Expand Up @@ -123,6 +124,7 @@
},
"scripts": {
"tests": "vendor/bin/phpunit",
"types": "vendor/bin/psalm",
"coding-style": "vendor/bin/phpcs && vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php_cs.dist",
"clear": "rm -rf vendor/"
}
Expand Down
55 changes: 55 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedFunction errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />

<InternalMethod errorLevel="info" />
<InternalProperty errorLevel="info" />
<InternalClass errorLevel="info" />

<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />
<MisplacedRequiredParam errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />

<RedundantCondition errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />

<RawObjectIteration errorLevel="info" />

<InvalidStringClass errorLevel="info" />
</issueHandlers>
</psalm>
4 changes: 2 additions & 2 deletions src/Functional/Average.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Returns the average of all numeric values in the array or null if no numeric value was found
*
* @param Traversable|array $collection
* @return null|float|int
* @return numeric|null
*/
function average($collection)
{
Expand All @@ -28,7 +28,7 @@ function average($collection)

foreach ($collection as $element) {
if (\is_numeric($element)) {
$sum += $element;
$sum = ($sum === null) ? $element : $sum + $element;
++$divisor;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Concat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Concatenates zero or more strings
*
* @param string[] ...$strings
* @param array<array-key, string> $strings
* @return string
*/
function concat(string ...$strings)
Expand Down
3 changes: 3 additions & 0 deletions src/Functional/Curry.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*/
function curry(callable $function, $required = true)
{
/** @psalm-suppress ArgumentTypeCoercion */
if (\method_exists('Closure', 'fromCallable')) {
// Closure::fromCallable was introduced in PHP 7.1
/** @psalm-suppress InvalidArgument */
$reflection = new ReflectionFunction(Closure::fromCallable($function));
} else {
if (\is_string($function) && \strpos($function, '::', 1) !== false) {
Expand All @@ -35,6 +37,7 @@ function curry(callable $function, $required = true)
} elseif (\is_object($function) && \method_exists($function, '__invoke')) {
$reflection = new ReflectionMethod($function, '__invoke');
} else {
/** @psalm-suppress InvalidArgument */
$reflection = new ReflectionFunction($function);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Takes a collection and returns the difference of all elements
*
* @param Traversable|array $collection
* @param integer|float $initial
* @return integer|float
* @param numeric $initial
* @return numeric
*/
function difference($collection, $initial = 0)
{
Expand Down
30 changes: 15 additions & 15 deletions src/Functional/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function assertCallback($callback, $callee, $parameterPosition)
{
if (!\is_callable($callback)) {
if (!\is_array($callback) && !\is_string($callback)) {
throw new static(
throw new self(
\sprintf(
'%s() expected parameter %d to be a valid callback, no array, string, closure or functor given',
$callee,
Expand All @@ -43,15 +43,15 @@ public static function assertCallback($callback, $callee, $parameterPosition)
$sep = '->';
}

$callback = \implode($callback, $sep);
$callback = \implode($sep, $callback);
break;

default:
$type = 'function';
break;
}

throw new static(
throw new self(
\sprintf(
"%s() expects parameter %d to be a valid callback, %s '%s' not found or invalid %s name",
$callee,
Expand All @@ -77,7 +77,7 @@ public static function assertArrayAccess($collection, $callee, $parameterPositio
public static function assertMethodName($methodName, $callee, $parameterPosition)
{
if (!\is_string($methodName)) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be string, %s given',
$callee,
Expand All @@ -102,7 +102,7 @@ public static function assertPropertyName($propertyName, $callee, $parameterPosi
!\is_float($propertyName) &&
!\is_null($propertyName)
) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be a valid property name or array index, %s given',
$callee,
Expand All @@ -119,7 +119,7 @@ public static function assertPositiveInteger($value, $callee, $parameterPosition
$type = self::getType($value);
$type = $type === 'integer' ? 'negative integer' : $type;

throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be positive integer, %s given',
$callee,
Expand All @@ -133,7 +133,7 @@ public static function assertPositiveInteger($value, $callee, $parameterPosition
/**
* @param mixed $key
* @param string $callee
* @throws static
* @throws InvalidArgumentException
*/
public static function assertValidArrayKey($key, $callee)
{
Expand All @@ -142,7 +142,7 @@ public static function assertValidArrayKey($key, $callee)
$keyType = \gettype($key);

if (!\in_array($keyType, $keyTypes, true)) {
throw new static(
throw new self(
\sprintf(
'%s(): callback returned invalid array key of type "%s". Expected %4$s or %3$s',
$callee,
Expand All @@ -157,7 +157,7 @@ public static function assertValidArrayKey($key, $callee)
public static function assertArrayKeyExists($collection, $key, $callee)
{
if (!isset($collection[$key])) {
throw new static(
throw new self(
\sprintf(
'%s(): unknown key "%s"',
$callee,
Expand All @@ -176,7 +176,7 @@ public static function assertArrayKeyExists($collection, $key, $callee)
public static function assertBoolean($value, $callee, $parameterPosition)
{
if (!\is_bool($value)) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be boolean, %s given',
$callee,
Expand All @@ -196,7 +196,7 @@ public static function assertBoolean($value, $callee, $parameterPosition)
public static function assertInteger($value, $callee, $parameterPosition)
{
if (!\is_int($value)) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be integer, %s given',
$callee,
Expand All @@ -217,7 +217,7 @@ public static function assertInteger($value, $callee, $parameterPosition)
public static function assertIntegerGreaterThanOrEqual($value, $limit, $callee, $parameterPosition)
{
if (!\is_int($value) || $value < $limit) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be an integer greater than or equal to %d',
$callee,
Expand All @@ -238,7 +238,7 @@ public static function assertIntegerGreaterThanOrEqual($value, $limit, $callee,
public static function assertIntegerLessThanOrEqual($value, $limit, $callee, $parameterPosition)
{
if (!\is_int($value) || $value > $limit) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be an integer less than or equal to %d',
$callee,
Expand All @@ -252,7 +252,7 @@ public static function assertIntegerLessThanOrEqual($value, $limit, $callee, $pa
public static function assertResolvablePlaceholder(array $args, $position)
{
if (\count($args) === 0) {
throw new static(
throw new self(
\sprintf('Cannot resolve parameter placeholder at position %d. Parameter stack is empty.', $position)
);
}
Expand All @@ -268,7 +268,7 @@ public static function assertResolvablePlaceholder(array $args, $position)
private static function assertCollectionAlike($collection, $className, $callee, $parameterPosition)
{
if (!\is_array($collection) && !$collection instanceof $className) {
throw new static(
throw new self(
\sprintf(
'%s() expects parameter %d to be array or instance of %s, %s given',
$callee,
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @param array $conditions the conditions to check against
*
* @return callable|null the function that calls the callable of the first truthy condition
* @return callable the function that calls the callable of the first truthy condition
*/
function match(array $conditions)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Maximum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Returns the maximum value of a collection
*
* @param Traversable|array $collection
* @return integer|float
* @return numeric|null
*/
function maximum($collection)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Minimum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Returns the minimum value of a collection
*
* @param Traversable|array $collection
* @return integer|float
* @return numeric|null
*/
function minimum($collection)
{
Expand Down
1 change: 1 addition & 0 deletions src/Functional/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function poll(callable $callback, $timeout, Traversable $delaySequence = null)

$delays = new AppendIterator();
if ($delaySequence) {
/** @psalm-suppress ArgumentTypeCoercion */
$delays->append(new InfiniteIterator($delaySequence));
}
$delays->append(new InfiniteIterator(new ArrayIterator([0])));
Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Takes a collection and returns the product of all elements
*
* @param Traversable|array $collection
* @param integer|float $initial
* @return integer|float
* @param numeric $initial
* @return numeric
*/
function product($collection, $initial = 1)
{
Expand Down
1 change: 1 addition & 0 deletions src/Functional/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function retry(callable $callback, $retries, Traversable $delaySequence = null)

if ($delaySequence) {
$delays = new AppendIterator();
/** @psalm-suppress ArgumentTypeCoercion */
$delays->append(new InfiniteIterator($delaySequence));
$delays->append(new InfiniteIterator(new ArrayIterator([0])));
$delays = new LimitIterator($delays, $retries);
Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Takes a collection and returns the sum of the elements
*
* @param Traversable|array $collection
* @param integer|float $initial
* @return integer|float
* @param numeric $initial
* @return numeric
*/
function sum($collection, $initial = 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/Functional/ZipAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function zip_all(...$args)
/** @var callable|null $callback */
$callback = null;
if (\is_callable(\end($args))) {
/** @var callable $callback */
$callback = \array_pop($args);
}

Expand Down
Loading

0 comments on commit dff3d19

Please sign in to comment.