Psalm
Get started
Documentation
Paid support
Plugins
Blog
GitHub
<?php /** * @template T * @psalm-consistent-constructor * @psalm-consistent-templates */ class ArrayCollection { /** @var list<T> */ private $elements; /** * @param list<T> $elements */ public function __construct(array $elements) { $this->elements = $elements; } /** * @template U * @param callable(T=):U $callback * @return static<U> */ public function map(callable $callback) { /** @psalm-suppress RedundantFunctionCall */ return new static(array_values(array_map($callback, $this->elements))); } } /** @return ($s is "string" ? string : int) */ function foo(string $s) { if ($s === "string") { return "hello"; } return 5; } $_res1 = (new ArrayCollection(["a", "bc"]))->map("strlen"); /** @psalm-trace $_res1 */; $_res2 = (new ArrayCollection(["a", "bc"]))->map("foo"); /** @psalm-trace $_res2 */;
Snippet created on April 7 2023 at 11:41 UTC
Settings
Get link