fix psalm issues (#163)

This commit is contained in:
Saif Eddin Gmati 2021-03-23 22:58:56 +01:00 committed by GitHub
parent 5482786dac
commit 2f65c9f878
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 28 additions and 59 deletions

View File

@ -41,7 +41,7 @@
- [map](./../../src/Psl/Arr/map.php#L34) ( deprecated )
- [map_keys](./../../src/Psl/Arr/map_keys.php#L34) ( deprecated )
- [map_with_key](./../../src/Psl/Arr/map_with_key.php#L34) ( deprecated )
- [merge](./../../src/Psl/Arr/merge.php#L32) ( deprecated )
- [merge](./../../src/Psl/Arr/merge.php#L34) ( deprecated )
- [partition](./../../src/Psl/Arr/partition.php#L22) ( deprecated )
- [random](./../../src/Psl/Arr/random.php#L25) ( deprecated )
- [select_keys](./../../src/Psl/Arr/select_keys.php#L25) ( deprecated )

View File

@ -34,7 +34,7 @@
- [map](./../../src/Psl/Dict/map.php#L29)
- [map_keys](./../../src/Psl/Dict/map_keys.php#L29)
- [map_with_key](./../../src/Psl/Dict/map_with_key.php#L29)
- [merge](./../../src/Psl/Dict/merge.php#L19)
- [merge](./../../src/Psl/Dict/merge.php#L21)
- [partition](./../../src/Psl/Dict/partition.php#L19)
- [partition_with_key](./../../src/Psl/Dict/partition_with_key.php#L19)
- [pull](./../../src/Psl/Dict/pull.php#L35)

View File

@ -17,7 +17,7 @@
- [current_exec](./../../src/Psl/Env/current_exec.php#L12)
- [get_var](./../../src/Psl/Env/get_var.php#L18)
- [get_vars](./../../src/Psl/Env/get_vars.php#L14)
- [join_paths](./../../src/Psl/Env/join_paths.php#L16)
- [join_paths](./../../src/Psl/Env/join_paths.php#L18)
- [remove_var](./../../src/Psl/Env/remove_var.php#L18)
- [set_current_dir](./../../src/Psl/Env/set_current_dir.php#L16)
- [set_var](./../../src/Psl/Env/set_var.php#L19)

View File

@ -43,7 +43,7 @@
- [map](./../../src/Psl/Iter/map.php#L35) ( deprecated )
- [map_keys](./../../src/Psl/Iter/map_keys.php#L35) ( deprecated )
- [map_with_key](./../../src/Psl/Iter/map_with_key.php#L33) ( deprecated )
- [merge](./../../src/Psl/Iter/merge.php#L30) ( deprecated )
- [merge](./../../src/Psl/Iter/merge.php#L32) ( deprecated )
- [product](./../../src/Psl/Iter/product.php#L34) ( deprecated )
- [pull](./../../src/Psl/Iter/pull.php#L40) ( deprecated )
- [pull_with_key](./../../src/Psl/Iter/pull_with_key.php#L41) ( deprecated )

View File

@ -59,6 +59,10 @@
<RedundantCondition errorLevel="suppress"/>
<RedundantCast errorLevel="suppress"/>
<RedundantCastGivenDocblockType errorLevel="suppress"/>
<!-- Not using the result of pure functions is common within PSL -->
<!-- e.g: with and Psl\invariant_violations() -->
<UnusedFunctionCall errorLevel="suppress" />
</issueHandlers>
<plugins>

View File

@ -24,7 +24,6 @@ use Psl\Iter;
function first(array $array)
{
/**
* @var Tk|null $first
* @psalm-suppress DeprecatedFunction
*/
$first = first_key($array);

View File

@ -28,7 +28,6 @@ use Psl\Iter;
function firstx(array $array)
{
/**
* @var Tk|null $first
* @psalm-suppress DeprecatedFunction
*/
$first = first_key($array);

View File

@ -24,7 +24,6 @@ use Psl\Iter;
function last(array $array)
{
/**
* @var Tk|null $last
* @psalm-suppress DeprecatedFunction
*/
$last = last_key($array);

View File

@ -28,7 +28,6 @@ use Psl\Iter;
function lastx(array $array)
{
/**
* @var Tk|null $last
* @psalm-suppress DeprecatedFunction
*/
$last = last_key($array);

View File

@ -28,6 +28,8 @@ use Psl\Dict;
*
* @deprecated use `Dict\merge` instead.
* @see Dict\merge()
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): array
{

View File

@ -378,8 +378,6 @@ final class Map implements MapInterface
foreach ($this->elements as $k => $v) {
/**
* @psalm-suppress ImpureFunctionCall - conditionally pure
*
* @var Tu|null $u
*/
$u = Iter\first($array);
if (null === $u) {

View File

@ -372,7 +372,6 @@ final class MutableMap implements MutableMapInterface
foreach ($this->elements as $k => $v) {
/**
* @var Tu|null $u
* @psalm-suppress ImpureFunctionCall - conditionally pure
*/
$u = Iter\first($array);

View File

@ -47,7 +47,6 @@ final class PriorityQueue implements PriorityQueueInterface
return null;
}
/** @var list<int> $keys */
$keys = Vec\keys($this->queue);
// Retrieve the highest priority.
@ -133,7 +132,7 @@ final class PriorityQueue implements PriorityQueueInterface
public function count(): int
{
$count = 0;
foreach ($this->queue as $priority => $list) {
foreach ($this->queue as $_priority => $list) {
$count += Iter\count($list);
}

View File

@ -29,7 +29,6 @@ function count_values(iterable $values): array
gettype($value)
);
/** @var int $count */
$count = $result[$value] ?? 0;
/** @var T $value */
$result[$value] = $count + 1;

View File

@ -35,10 +35,10 @@ function filter_with_key(iterable $iterable, ?callable $predicate = null): array
{
$predicate = $predicate ??
/**
* @param Tk $k
* @param Tk $_k
* @param Tv $v
*/
static fn ($k, $v): bool => Psl\Internal\boolean($v);
static fn ($_k, $v): bool => Psl\Internal\boolean($v);
/** @var array<Tk, Tv> $result */
$result = [];

View File

@ -15,6 +15,8 @@ namespace Psl\Dict;
* @param iterable<Tk, Tv> ...$rest
*
* @return array<Tk, Tv>
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): array
{

View File

@ -45,9 +45,6 @@ function sort_by(iterable $iterable, callable $scalar_func, ?callable $comparato
$tuples[$k] = [$scalar_func($v), $v];
}
/**
* @var array<Tk, array{0: Ts, 1: Tv}> $sorted
*/
$sorted = namespace\sort($tuples, $tuple_comparator);
/** @var array<Tk, Tv> $result */

View File

@ -28,7 +28,6 @@ function unique_by(iterable $iterable, callable $scalar_func): array
$original_values = [];
foreach ($iterable as $k => $v) {
$original_values[$k] = $v;
/** @var Ts $scalar */
$scalar = $scalar_func($v);
if (!Iter\contains($unique, $scalar)) {

View File

@ -22,7 +22,6 @@ function get_var(string $key): ?string
'Invalid environment variable key provided.'
);
/** @var false|string $value */
$value = getenv($key);
return false === $value ? null : $value;

View File

@ -12,6 +12,8 @@ use const PATH_SEPARATOR;
* Joins a collection of paths appropriately for the PATH environment variable.
*
* @param string ...$paths
*
* @no-named-arguments
*/
function join_paths(string ...$paths): string
{

View File

@ -87,7 +87,6 @@ final class Context
*/
public function update(string $data): Context
{
/** @var HashContext $internal_context */
$internal_context = hash_copy($this->internalContext);
hash_update($internal_context, $data);
@ -101,7 +100,6 @@ final class Context
*/
public function finalize(): string
{
/** @var HashContext $internal_context */
$internal_context = hash_copy($this->internalContext);
return hash_final($internal_context, false);

View File

@ -15,7 +15,7 @@ namespace Psl\Iter;
*/
function contains_key(iterable $iterable, $key): bool
{
foreach ($iterable as $k => $v) {
foreach ($iterable as $k => $_v) {
if ($key === $k) {
return true;
}

View File

@ -33,7 +33,6 @@ function count(iterable $iterable): int
}
$count = 0;
/** @var T $_ */
foreach ($iterable as $_) {
++$count;
}

View File

@ -41,12 +41,12 @@ function filter_with_key(iterable $iterable, ?callable $predicate = null): Itera
return Iterator::from(static function () use ($iterable, $predicate): Generator {
$predicate = $predicate ??
/**
* @param Tk $k
* @param Tk $_k
* @param Tv $v
*
* @return bool
*/
static fn ($k, $v) => Psl\Internal\boolean($v);
static fn ($_k, $v) => Psl\Internal\boolean($v);
foreach ($iterable as $k => $v) {
if ($predicate($k, $v)) {

View File

@ -26,6 +26,8 @@ use Psl\Dict;
*
* @deprecated use `Dict\merge` instead.
* @see Dict\merge()
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): Iterator
{

View File

@ -31,7 +31,6 @@ function random(iterable $iterable)
Psl\invariant(0 !== $size, 'Expected a non-empty iterable.');
/** @var list<Tv> $shuffled */
$shuffled = Vec\shuffle($values);
if (1 === $size) {

View File

@ -33,7 +33,7 @@ namespace Psl\Iter;
function reduce_keys(iterable $iterable, callable $function, $initial = null)
{
$accumulator = $initial;
foreach ($iterable as $k => $v) {
foreach ($iterable as $k => $_v) {
$accumulator = $function($accumulator, $k);
}

View File

@ -42,7 +42,6 @@ function zip(iterable ...$iterables): Iterator
return;
}
/** @var list<Iterator<Tk, Tv>> $iterators */
$iterators = Vec\values(Dict\map(
$iterables,
/**
@ -55,7 +54,6 @@ function zip(iterable ...$iterables): Iterator
apply($iterators, static fn (Iterator $iterator) => $iterator->rewind());
while (all($iterators, static fn (Iterator $iterator) => $iterator->valid())) {
/** @var list<Tk> $keys */
$keys = Vec\values(Dict\map(
$iterators,
/**
@ -66,7 +64,6 @@ function zip(iterable ...$iterables): Iterator
static fn (Iterator $iterator) => $iterator->key(),
));
/** @var list<Tv> $values */
$values = Vec\values(Dict\map(
$iterators,
/**

View File

@ -49,15 +49,13 @@ function base_convert(string $value, int $from_base, int $to_base): string
$from_alphabet = Byte\slice(Str\ALPHABET_ALPHANUMERIC, 0, $from_base);
/** @var numeric-string $result_decimal */
$result_decimal = '0';
/** @var numeric-string $place_value */
$place_value = bcpow((string)$from_base, (string)(Byte\length($value) - 1));
/** @var string $digit */
foreach (Byte\chunk($value) as $digit) {
Psl\invariant(is_numeric($place_value), 'Unexpected error.');
$digit_numeric = Byte\search_ci($from_alphabet, $digit);
Psl\invariant(null !== $digit_numeric, 'Invalid digit %s in base %d', $digit, $from_base);
$result_decimal = bcadd($result_decimal, bcmul((string)$digit_numeric, $place_value));
Psl\invariant(is_numeric($result_decimal), 'Unexpected error.');
/** @var numeric-string $place_value */
$place_value = bcdiv($place_value, (string)$from_base);
}

View File

@ -36,7 +36,6 @@ function from_base(string $number, int $from_base): int
/** @psalm-suppress MissingThrowsDocblock */
$limit = div(INT64_MAX, $from_base);
$result = 0;
/** @var string $digit */
foreach (Byte\chunk($number) as $digit) {
$oval = Byte\ord($digit);
// Branches sorted by guesstimated frequency of use. */

View File

@ -22,7 +22,6 @@ function maxva($first, $second, ...$rest)
$max = $first > $second ? $first : $second;
foreach ($rest as $number) {
if ($number > $max) {
/** @var T $max */
$max = $number;
}
}

View File

@ -10,13 +10,11 @@ use Psl\Vec;
/**
* Returns the median of the given numbers.
*
* @pslam-param iterable<int|float> $numbers
* @param iterable<int|float> $numbers
*/
function median(iterable $numbers): ?float
{
/** @var list<int|float> $numbers */
$numbers = Vec\values($numbers);
/** @var list<int|float> $numbers */
$numbers = Vec\sort($numbers);
$count = Iter\count($numbers);
if (0 === $count) {

View File

@ -12,7 +12,6 @@ use Psl\Math;
function float(): float
{
/**
* @var float|int $result
* @psalm-suppress MissingThrowsDocblock $max is always > than $min
*/
$result = namespace\int(0, Math\INT53_MAX) / Math\INT53_MAX;

View File

@ -14,7 +14,6 @@ use Psl\Math;
function float(): float
{
/**
* @var float|int $result
* @psalm-suppress MissingThrowsDocblock
*/
$result = namespace\int(0, Math\INT53_MAX) / Math\INT53_MAX;

View File

@ -26,7 +26,6 @@ use function mb_chr;
*/
function chr(int $codepoint, ?string $encoding = null): string
{
/** @var string|false $char */
$char = mb_chr($codepoint, Internal\internal_encoding($encoding));
/** @psalm-suppress MissingThrowsDocblock */

View File

@ -77,7 +77,6 @@ final class IntersectionType extends Type
}
try {
/** @var Tl $value */
$value = $this->left_type->coerce($value);
/** @var Tl&Tr */
return $this->right_type->assert($value);
@ -86,7 +85,6 @@ final class IntersectionType extends Type
}
try {
/** @var Tr $value */
$value = $this->right_type->coerce($value);
/** @var Tr&Tl */
return $this->left_type->assert($value);
@ -109,7 +107,6 @@ final class IntersectionType extends Type
public function assert($value)
{
try {
/** @var Tl $value */
$value = $this->left_type->assert($value);
/** @var Tl&Tr */
return $this->right_type->assert($value);

View File

@ -90,10 +90,6 @@ final class IterableType extends Type\Type
/** @var iterable<Tk, Tv> */
return Iter\Iterator::from((static function () use ($entries) {
/**
* @var Tk $key
* @var Tv $value
*/
foreach ($entries as [$key, $value]) {
yield $key => $value;
}
@ -143,10 +139,6 @@ final class IterableType extends Type\Type
/** @var iterable<Tk, Tv> */
return Iter\Iterator::from((static function () use ($entries) {
/**
* @var Tk $key
* @var Tv $value
*/
foreach ($entries as [$key, $value]) {
yield $key => $value;
}

View File

@ -93,7 +93,6 @@ final class MutableMapType extends Type\Type
];
}
/** @var array<Tk, Tv> $dict */
$dict = Dict\from_entries($entries);
/** @var Collection\MutableMap<Tk, Tv> */
@ -144,7 +143,6 @@ final class MutableMapType extends Type\Type
];
}
/** @var array<Tk, Tv> $dict */
$dict = Dict\from_entries($entries);
/** @var Collection\MutableMap<Tk, Tv> */

View File

@ -35,10 +35,10 @@ function filter_with_key(iterable $iterable, ?callable $predicate = null): array
{
$predicate = $predicate ??
/**
* @param Tk $k
* @param Tk $_k
* @param Tv $v
*/
static fn ($k, $v): bool => Psl\Internal\boolean($v);
static fn ($_k, $v): bool => Psl\Internal\boolean($v);
$result = [];
foreach ($iterable as $k => $v) {

View File

@ -17,7 +17,7 @@ namespace Psl\Vec;
function keys(iterable $iterable): array
{
$result = [];
foreach ($iterable as $k => $v) {
foreach ($iterable as $k => $_v) {
$result[] = $k;
}