Skip to content

Commit

Permalink
Get ride of some phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KmeCnin committed Dec 10, 2023
1 parent b9edf65 commit 10da06d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Functional/Ary.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function ary(callable $func, int $count): callable
return function (...$args) use ($func, $count) {
if ($count > 0) {
return $func(...take_left($args, $count));
} else if ($count < 0) {
} else {
return $func(...take_right($args, -$count));
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/SequenceLinear.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Returns an infinite, traversable sequence that linearly grows by given amount
*
* @param int<0,max> $start
* @param non-negative-int $start
* @param int $amount
*
* @return LinearSequence
Expand Down
23 changes: 18 additions & 5 deletions src/Functional/Sequences/ExponentialSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@
use Functional\Exceptions\InvalidArgumentException;
use Iterator;

/** @internal */
/**
* @internal
*
* @implements Iterator<0,int>
*/
class ExponentialSequence implements Iterator
{
/** @var integer */
/** @var positive-int */
private $start;

/** @var integer */
/** @var int<1,100> */
private $percentage;

/** @var integer */
/** @var positive-int */
private $value;

/** @var integer */
/** @var non-negative-int */
private $times;

/**
* @param positive-int $start
* @param int<1,100> $percentage
*/
public function __construct($start, $percentage)
{
InvalidArgumentException::assertIntegerGreaterThanOrEqual($start, 1, __METHOD__, 1);
Expand All @@ -38,27 +46,32 @@ public function __construct($start, $percentage)
$this->percentage = $percentage;
}

/** @return positive-int */
public function current()
{
return $this->value;
}

/** @return void */
public function next()
{
$this->value = (int) \round(\pow($this->start * (1 + $this->percentage / 100), $this->times));
$this->times++;
}

/** @return null */
public function key()
{
return null;
}

/** @return bool */
public function valid()
{
return true;
}

/** @return void */
public function rewind()
{
$this->times = 1;
Expand Down
21 changes: 17 additions & 4 deletions src/Functional/Sequences/LinearSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@
use Functional\Exceptions\InvalidArgumentException;
use Iterator;

/** @internal */
/**
* @internal
*
* @implements Iterator<0,int>
*/
class LinearSequence implements Iterator
{
/** @var integer */
/** @var non-negative-int */
private $start;

/** @var integer */
/** @var int */
private $amount;

/** @var integer */
/** @var int */
private $value;

/**
* @param non-negative-int $start
* @param int $amount
*/
public function __construct($start, $amount)
{
InvalidArgumentException::assertIntegerGreaterThanOrEqual($start, 0, __METHOD__, 1);
Expand All @@ -34,26 +42,31 @@ public function __construct($start, $amount)
$this->amount = $amount;
}

/** @return int */
public function current()
{
return $this->value;
}

/** @return void */
public function next()
{
$this->value += $this->amount;
}

/** @return int */
public function key()
{
return 0;
}

/** @return bool */
public function valid()
{
return true;
}

/** @return void */
public function rewind()
{
$this->value = $this->start;
Expand Down
1 change: 1 addition & 0 deletions src/Functional/TailRecursion.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function tail_recursion(callable $fn): callable
return function (...$args) use (&$fn, &$underCall, &$queue) {
$result = null;
$queue[] = $args;
/** @var bool $underCall */
if (!$underCall) {
$underCall = true;
while ($head = \array_shift($queue)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Functional/ValueToKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use const PHP_VERSION_ID;

/**
* @param mixed ...$any
*
* @return string
*
* @no-named-arguments
*/
function value_to_key(...$any)
Expand Down Expand Up @@ -63,13 +67,21 @@ function value_to_key(...$any)
};
}

/** @var null|callable(mixed,null|string=):string $valueToRef */
static $valueToRef = null;
if (!$valueToRef) {
/**
* @param mixed $value
* @param null|string $key
*
* @return string
*/
$valueToRef = static function ($value, $key = null) use (&$valueToRef, $objectToRef) {
$type = \gettype($value);
if ($type === 'array') {
$ref = '[' . \implode(':', map($value, $valueToRef)) . ']';
} elseif ($value instanceof Traversable) {
/** @var iterable<mixed> $value */
$ref = $objectToRef($value) . '[' . \implode(':', map($value, $valueToRef)) . ']';
} elseif ($type === 'object') {
$ref = $objectToRef($value);
Expand Down
5 changes: 1 addition & 4 deletions src/Functional/ZipAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
function zip_all(...$args)
{
/** @var callable|null $callback */
$callback = null;
if (\is_callable(\end($args))) {
$callback = \array_pop($args);
}
$callback = \is_callable(\end($args)) ? \array_pop($args) : null;

foreach ($args as $position => $arg) {
InvalidArgumentException::assertCollection($arg, __FUNCTION__, $position + 1);
Expand Down

0 comments on commit 10da06d

Please sign in to comment.