Skip to content

Commit

Permalink
Improve return types for psalm (#321)
Browse files Browse the repository at this point in the history
Psalm cannot always infer the types when not defining the CollectionInterface<Key, Value>
type. This makes sure that psalm is aware of the implemented interface and the Collection
class.
  • Loading branch information
Matth-- authored Nov 25, 2023
1 parent 4374455 commit 81fa92d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.16.0@2897ba636551a8cb61601cc26f6ccfbba6c36591">
<file src="src/Collection.php">
<MixedArgument>
<code>(new Operation\Product())()(...$iterables)</code>
<code>(new Operation\Combinate())()($length)</code>
</MixedArgument>
<InvalidArgument>
<code>$callback</code>
<code>$callbacks</code>
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
<directory name="tests/unit/" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedArgument errorLevel="error"/>
</issueHandlers>
</psalm>
14 changes: 7 additions & 7 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function duplicate(?callable $comparatorCallback = null, ?callable $acces
* @template UKey
* @template U
*
* @return self<UKey, U>
* @return CollectionInterface<UKey, U>&self<UKey, U>
*/
public static function empty(): CollectionInterface
{
Expand Down Expand Up @@ -345,7 +345,7 @@ public function frequency(): CollectionInterface
* @param callable(mixed ...$parameters): iterable<NewTKey, NewT> $callable
* @param iterable<int, mixed> $parameters
*
* @return self<NewTKey, NewT>
* @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT>
*/
public static function fromCallable(callable $callable, iterable $parameters = []): CollectionInterface
{
Expand All @@ -355,7 +355,7 @@ public static function fromCallable(callable $callable, iterable $parameters = [
/**
* @param null|non-negative-int $length
*
* @return self<int, string>
* @return CollectionInterface<int, string>&self<int, string>
*/
public static function fromFile(string $filepath, ?int $length = 2): CollectionInterface
{
Expand All @@ -370,7 +370,7 @@ public static function fromFile(string $filepath, ?int $length = 2): CollectionI
*
* @param Generator<NewTKey, NewT> $generator
*
* @return self<NewTKey, NewT>
* @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT>
*/
public static function fromGenerator(Generator $generator): CollectionInterface
{
Expand All @@ -383,7 +383,7 @@ public static function fromGenerator(Generator $generator): CollectionInterface
*
* @param iterable<UKey, U> $iterable
*
* @return self<UKey, U>
* @return CollectionInterface<UKey, U>&self<UKey, U>
*/
public static function fromIterable(iterable $iterable): CollectionInterface
{
Expand All @@ -393,15 +393,15 @@ public static function fromIterable(iterable $iterable): CollectionInterface
/**
* @param resource $resource
*
* @return self<int, string>
* @return CollectionInterface<int, string>&self<int, string>
*/
public static function fromResource($resource): CollectionInterface
{
return new self(static fn (): Generator => yield from new ResourceIteratorAggregate($resource));
}

/**
* @return self<int, string>
* @return CollectionInterface<int, string>&self<int, string>
*/
public static function fromString(string $string, string $delimiter = ''): CollectionInterface
{
Expand Down
28 changes: 28 additions & 0 deletions tests/static-analysis/fromIterableAssociateAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;

enum fromIterableAssociateAll: int
{
case FIFTH = 5;
case FIRST = 1;
case FOURTH = 4;
case SECOND = 2;
case THIRD = 3;
}

/**
* @psalm-param array<string, int> $array
*
* @phpstan-param array<string, int> $array
*/
function checklist(array $array): void {}

checklist(Collection::fromIterable(fromIterableAssociateAll::cases())->associate(
static fn (int $_, fromIterableAssociateAll $item) => $item->value,
static fn (fromIterableAssociateAll $item, int $_) => $item->name,
)->flip()->all(false));

0 comments on commit 81fa92d

Please sign in to comment.