Skip to content

Add phpdoc for transformation methods into other Collection stubs #622

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

Merged
merged 2 commits into from
Oct 29, 2024
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
1 change: 0 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ parameters:
- stubs/Persistence/ObjectRepository.stub
- stubs/RepositoryFactory.stub
- stubs/Collections/ArrayCollection.stub
- stubs/Collections/ReadableCollection.stub
- stubs/Collections/Selectable.stub
- stubs/ORM/AbstractQuery.stub
- stubs/ORM/Exception/ORMException.stub
Expand Down
2 changes: 2 additions & 0 deletions src/Stubs/Doctrine/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function getFiles(): array
$collectionVersion = null;
}
if ($collectionVersion !== null && strpos($collectionVersion, '1.') === 0) {
$files[] = $stubsDir . '/Collections/ReadableCollection1.stub';
$files[] = $stubsDir . '/Collections/Collection1.stub';
} else {
$files[] = $stubsDir . '/Collections/ReadableCollection.stub';
$files[] = $stubsDir . '/Collections/Collection.stub';
}

Expand Down
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);

}
86 changes: 86 additions & 0 deletions stubs/Collections/ReadableCollection1.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Doctrine\Common\Collections;

use Closure;
use Countable;
use IteratorAggregate;

/**
* @template TKey of array-key
* @template-covariant T
* @extends IteratorAggregate<TKey, T>
*/
interface ReadableCollection extends Countable, IteratorAggregate
{

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

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(T, TKey):bool $p
*
* @return ReadableCollection<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: ReadableCollection<TKey, T>, 1: ReadableCollection<TKey, T>}
*/
public function partition(Closure $p);

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(TKey, T):bool $p
*
* @return bool TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
*/
public function forAll(Closure $p);

/**
* @param-immediately-invoked-callable $p
*
* @param Closure(TKey, T):bool $p
*
* @return T|null
*/
public function findFirst(Closure $p);

/**
* @param-immediately-invoked-callable $func
*
* @param Closure(TReturn|TInitial, T):TReturn $func
* @param TInitial $initial
*
* @return TReturn|TInitial
*
* @template TReturn
* @template TInitial
*/
public function reduce(Closure $func, mixed $initial = null);

}
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
{

}