Skip to content

Commit

Permalink
Add phpdoc for transformation methods into other Collection stubs (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk authored and ondrejmirtes committed Oct 29, 2024
1 parent f61f963 commit 80f3b5f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
31 changes: 31 additions & 0 deletions stubs/Collections/ArrayCollection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\Common\Collections;

use Closure;

/**
* @template TKey of array-key
* @template T
Expand All @@ -11,4 +13,33 @@ namespace Doctrine\Common\Collections;
class ArrayCollection implements Collection, Selectable
{

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(T, TKey):bool $p
*
* @return static<TKey, T>
*/
public function filter(Closure $p);

/**
* @param-immediately-invoked-callable $func
*
* @param Closure(T):U $func
*
* @return static<TKey, U>
*
* @template U
*/
public function map(Closure $func);

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(TKey, T):bool $p
*
* @return array{0: static<TKey, T>, 1: static<TKey, T>}
*/
public function partition(Closure $p);

}
30 changes: 30 additions & 0 deletions stubs/Collections/Collection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Common\Collections;

use ArrayAccess;
use Closure;
use Countable;
use IteratorAggregate;

Expand Down Expand Up @@ -43,4 +44,33 @@ interface Collection extends Countable, IteratorAggregate, ArrayAccess, Readable
*/
public function removeElement($element) {}

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(T, TKey):bool $p
*
* @return Collection<TKey, T>
*/
public function filter(Closure $p);

/**
* @param-immediately-invoked-callable $func
*
* @param Closure(T):U $func
*
* @return Collection<TKey, U>
*
* @template U
*/
public function map(Closure $func);

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

}
30 changes: 30 additions & 0 deletions stubs/Collections/Collection1.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Common\Collections;

use ArrayAccess;
use Closure;
use Countable;
use IteratorAggregate;

Expand Down Expand Up @@ -43,4 +44,33 @@ interface Collection extends Countable, IteratorAggregate, ArrayAccess, Readable
*/
public function removeElement($element) {}

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(T, TKey):bool $p
*
* @return Collection<TKey, T>
*/
public function filter(Closure $p);

/**
* @param-immediately-invoked-callable $func
*
* @param Closure(T):U $func
*
* @return Collection<TKey, U>
*
* @template U
*/
public function map(Closure $func);

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

}
1 change: 1 addition & 0 deletions tests/DoctrineIntegration/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/getRepository.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/isEmpty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/Collection.php');
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/DoctrineIntegration/data/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Bug621;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use function PHPStan\Testing\assertType;

class Foo
{

/** @var Collection<int, Item> */
private $items;

public function __construct()
{
/** @var ArrayCollection<int, int> $numbers */
$numbers = new ArrayCollection([1, 2, 3]);

$filteredNumbers = $numbers->filter(function (int $number): bool {
return $number % 2 === 1;
});
assertType('Doctrine\Common\Collections\ArrayCollection<int, int>', $filteredNumbers);

$items = $filteredNumbers->map(static function (int $number): Item {
return new Item();
});
assertType('Doctrine\Common\Collections\ArrayCollection<int, Bug621\Item>', $items);

$this->items = $items;
}

public function removeOdd(): void
{
$this->items = $this->items->filter(function (Item $item, int $idx): bool {
return $idx % 2 === 1;
});
assertType('Doctrine\Common\Collections\Collection<int, Bug621\Item>', $this->items);
}

public function __clone()
{
$this->items = $this->items->map(
static function (Item $item): Item {
return clone $item;
}
);
assertType('Doctrine\Common\Collections\Collection<int, Bug621\Item>', $this->items);
}

}

class Item
{

}

0 comments on commit 80f3b5f

Please sign in to comment.