Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix psalm issues #163

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/component/arr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion docs/component/dict.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/component/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/component/iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Comment on lines +62 to +65
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this should be considered a bug in psalm or not.

  1. pure function are considered to have no side effects
  2. calling a function with no side effects without using it's results is useless
  3. psalm consider throwing an exception not a side effect
  4. hacklang considers throwing an exception a side effect ( https://github.com/facebook/hhvm/blob/master/hphp/hack/doc/HIPs/contexts_and_coeffects.md )
  5. if throwing an exception is a side effect, this is most likey to result in all functions being non pure, as 90% of calls result in an exception
  6. if throwing an exception is not a side effect, how can a pure function tell psalm that it's okay not to use it's result?

wdyt @muglug?

/cc @Ocramius

</issueHandlers>

<plugins>
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Arr/first.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
function first(array $array)
{
/**
* @var Tk|null $first
* @psalm-suppress DeprecatedFunction
*/
$first = first_key($array);
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Arr/firstx.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
function firstx(array $array)
{
/**
* @var Tk|null $first
* @psalm-suppress DeprecatedFunction
*/
$first = first_key($array);
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Arr/last.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
function last(array $array)
{
/**
* @var Tk|null $last
* @psalm-suppress DeprecatedFunction
*/
$last = last_key($array);
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Arr/lastx.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
function lastx(array $array)
{
/**
* @var Tk|null $last
* @psalm-suppress DeprecatedFunction
*/
$last = last_key($array);
Expand Down
2 changes: 2 additions & 0 deletions src/Psl/Arr/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*
* @deprecated use `Dict\merge` instead.
* @see Dict\merge()
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): array
{
Expand Down
2 changes: 0 additions & 2 deletions src/Psl/Collection/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,6 @@ public function zip(iterable $iterable): Map
foreach ($this->elements as $k => $v) {
/**
* @psalm-suppress ImpureFunctionCall - conditionally pure
*
* @var Tu|null $u
*/
$u = Iter\first($array);
if (null === $u) {
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Collection/MutableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ public function zip(iterable $iterable): MutableMap

foreach ($this->elements as $k => $v) {
/**
* @var Tu|null $u
* @psalm-suppress ImpureFunctionCall - conditionally pure
*/
$u = Iter\first($array);
Expand Down
3 changes: 1 addition & 2 deletions src/Psl/DataStructure/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function peek()
return null;
}

/** @var list<int> $keys */
$keys = Vec\keys($this->queue);

// Retrieve the highest priority.
Expand Down Expand Up @@ -133,7 +132,7 @@ private function drop(): void
public function count(): int
{
$count = 0;
foreach ($this->queue as $priority => $list) {
foreach ($this->queue as $_priority => $list) {
$count += Iter\count($list);
}

Expand Down
1 change: 0 additions & 1 deletion src/Psl/Dict/count_values.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Dict/filter_with_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
2 changes: 2 additions & 0 deletions src/Psl/Dict/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @param iterable<Tk, Tv> ...$rest
*
* @return array<Tk, Tv>
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): array
{
Expand Down
3 changes: 0 additions & 3 deletions src/Psl/Dict/sort_by.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Dict/unique_by.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Env/get_var.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Psl/Env/join_paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Joins a collection of paths appropriately for the PATH environment variable.
*
* @param string ...$paths
*
* @no-named-arguments
*/
function join_paths(string ...$paths): string
{
Expand Down
2 changes: 0 additions & 2 deletions src/Psl/Hash/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public static function hmac(string $algorithm, string $key): Context
*/
public function update(string $data): Context
{
/** @var HashContext $internal_context */
$internal_context = hash_copy($this->internalContext);
hash_update($internal_context, $data);

Expand All @@ -101,7 +100,6 @@ public function update(string $data): Context
*/
public function finalize(): string
{
/** @var HashContext $internal_context */
$internal_context = hash_copy($this->internalContext);

return hash_final($internal_context, false);
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Iter/contains_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
function contains_key(iterable $iterable, $key): bool
{
foreach ($iterable as $k => $v) {
foreach ($iterable as $k => $_v) {
if ($key === $k) {
return true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Iter/count.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function count(iterable $iterable): int
}

$count = 0;
/** @var T $_ */
foreach ($iterable as $_) {
++$count;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Iter/filter_with_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Psl/Iter/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*
* @deprecated use `Dict\merge` instead.
* @see Dict\merge()
*
* @no-named-arguments
*/
function merge(iterable $first, iterable ...$rest): Iterator
{
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Iter/random.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Iter/reduce_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
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);
}

Expand Down
3 changes: 0 additions & 3 deletions src/Psl/Iter/zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function zip(iterable ...$iterables): Iterator
return;
}

/** @var list<Iterator<Tk, Tv>> $iterators */
$iterators = Vec\values(Dict\map(
$iterables,
/**
Expand All @@ -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,
/**
Expand All @@ -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,
/**
Expand Down
4 changes: 1 addition & 3 deletions src/Psl/Math/base_convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/Psl/Math/from_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Math/maxva.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Psl/Math/median.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
/**
* 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) {
Expand Down
1 change: 0 additions & 1 deletion src/Psl/PseudoRandom/float.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand Down
1 change: 0 additions & 1 deletion src/Psl/SecureRandom/float.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
function float(): float
{
/**
* @var float|int $result
* @psalm-suppress MissingThrowsDocblock
*/
$result = namespace\int(0, Math\INT53_MAX) / Math\INT53_MAX;
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Str/chr.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*/
function chr(int $codepoint, ?string $encoding = null): string
{
/** @var string|false $char */
$char = mb_chr($codepoint, Internal\internal_encoding($encoding));

/** @psalm-suppress MissingThrowsDocblock */
Expand Down
3 changes: 0 additions & 3 deletions src/Psl/Type/Internal/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function coerce($value)
}

try {
/** @var Tl $value */
$value = $this->left_type->coerce($value);
/** @var Tl&Tr */
return $this->right_type->assert($value);
Expand All @@ -86,7 +85,6 @@ public function coerce($value)
}

try {
/** @var Tr $value */
$value = $this->right_type->coerce($value);
/** @var Tr&Tl */
return $this->left_type->assert($value);
Expand All @@ -109,7 +107,6 @@ public function coerce($value)
public function assert($value)
{
try {
/** @var Tl $value */
$value = $this->left_type->assert($value);
/** @var Tl&Tr */
return $this->right_type->assert($value);
Expand Down
Loading