Skip to content

Commit

Permalink
Merge pull request #411 from VincentLanglet/repeatClosure
Browse files Browse the repository at this point in the history
Repeat closure type in Collection
  • Loading branch information
greg0ire committed Apr 18, 2024
2 parents 9b9c38a + 5dec3fd commit d8af7f2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: 8
paths:
- src
- tests/StaticAnalysis

ignoreErrors:
-
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
>
<projectFiles>
<directory name="src" />
<directory name="tests/StaticAnalysis" />
<ignoreFiles>
<directory name="vendor" />
<directory name="src/Expr"/>
Expand Down
2 changes: 2 additions & 0 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ public function reduce(Closure $func, $initial = null)
/**
* {@inheritDoc}
*
* @psalm-param Closure(T, TKey):bool $p
*
* @return static
* @psalm-return static<TKey,T>
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,18 @@ public function map(Closure $func);
/**
* {@inheritDoc}
*
* @psalm-param Closure(T, TKey):bool $p
*
* @return Collection<mixed> A collection with the results of the filter operation.
* @psalm-return Collection<TKey, T>
*/
public function filter(Closure $p);

/**
* {@inheritDoc}
*
* @psalm-param Closure(TKey, T):bool $p
*
* @return Collection<mixed>[] An array with two elements. The first element contains the collection
* of elements where the predicate returned TRUE, the second element
* contains the collection of elements where the predicate returned FALSE.
Expand Down
46 changes: 46 additions & 0 deletions tests/StaticAnalysis/CustomCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Collections\StaticAnalysis;

use Closure;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

/**
* @phpstan-template TKey of array-key
* @phpstan-template T of object
* @phpstan-implements Collection<TKey, T>
*/
abstract class CustomCollection implements Collection
{
/** @var ArrayCollection<TKey, T> */
private ArrayCollection $collection;

/** @param ArrayCollection<TKey, T> $arrayCollection */
public function __construct(ArrayCollection $arrayCollection)
{
$this->collection = $arrayCollection;
}

/**
* @psalm-param Closure(T, TKey):bool $p
*
* @return Collection<TKey, T>
*/
public function filter(Closure $p)
{
return $this->collection->filter($p);
}

/**
* @psalm-param Closure(TKey, T):bool $p
*
* @psalm-return array{0: Collection<TKey, T>, 1: Collection<TKey, T>}
*/
public function partition(Closure $p)
{
return $this->collection->partition($p);
}
}

0 comments on commit d8af7f2

Please sign in to comment.