Psalm
Get started
Documentation
Paid support
Plugins
Blog
GitHub
<?php /** * @psalm-template TKey of array-key * @psalm-template T */ interface Collection {} /** * @psalm-template TKey as array-key * @psalm-template T */ interface Selectable {} /** * @psalm-template TKey of array-key * @psalm-template T * @template-implements Collection<TKey,T> * @template-implements Selectable<TKey,T> * @psalm-consistent-constructor */ class ArrayCollection implements Collection, Selectable { /** * An array containing the entries of this collection. * * @psalm-var array<TKey,T> * @var mixed[] */ private $elements; /** * Initializes a new ArrayCollection. * * @param array $elements * @psalm-param array<TKey,T> $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } } abstract class Entity {} class User extends Entity {} /** * @template TKey of int * @template T of Entity * @template-implements Collection<int, T> * @template-implements Selectable<int, T> */ abstract class EntityCollection implements Collection, Selectable { /** @psalm-var class-string<T> */ protected const ENTITY_CLASS = ''; /** * @var Collection<int, T>&Selectable<int, T> */ protected $entities; /** * @param Collection<int, T>&Selectable<int, T>|null $entities */ final public function __construct(Collection $entities = null){ $this->entities = $entities ?? new ArrayCollection(); } /** * @template Wat? * @param array<int, T> $entities * * @return static<TKey, T> */ public static function createFromArray(array $entities): EntityCollection{ $collection = new ArrayCollection($entities); return new static($collection); } } /** * @template-extends EntityCollection<int, User> */ class Users extends EntityCollection {}
Snippet created on January 25 2022 at 11:05 UTC
Settings
Get link