|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace AssertArglessCallTemplateDefault; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class User |
| 8 | +{ |
| 9 | + |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * @template TKey of array-key |
| 14 | + * @template-covariant TValue |
| 15 | + */ |
| 16 | +class Collection |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @template TFirstDefault |
| 21 | + * |
| 22 | + * @param (callable(TValue, TKey): bool)|null $callback |
| 23 | + * @param TFirstDefault|(\Closure(): TFirstDefault) $default |
| 24 | + * @return TValue|TFirstDefault |
| 25 | + */ |
| 26 | + public function first(?callable $callback = null, $default = null) |
| 27 | + { |
| 28 | + throw new \LogicException(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @template TLastDefault |
| 33 | + * |
| 34 | + * @param (callable(TValue, TKey): bool)|null $callback |
| 35 | + * @param TLastDefault|(\Closure(): TLastDefault) $default |
| 36 | + * @return TValue|TLastDefault |
| 37 | + */ |
| 38 | + public function last(?callable $callback = null, $default = null) |
| 39 | + { |
| 40 | + throw new \LogicException(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @phpstan-assert-if-true null $this->first() |
| 45 | + * @phpstan-assert-if-true null $this->last() |
| 46 | + * |
| 47 | + * @phpstan-assert-if-false TValue $this->first() |
| 48 | + * @phpstan-assert-if-false TValue $this->last() |
| 49 | + * |
| 50 | + * @return bool |
| 51 | + */ |
| 52 | + public function isEmpty() |
| 53 | + { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @phpstan-assert-if-true TValue $this->first() |
| 59 | + * @phpstan-assert-if-true TValue $this->last() |
| 60 | + * |
| 61 | + * @phpstan-assert-if-false null $this->first() |
| 62 | + * @phpstan-assert-if-false null $this->last() |
| 63 | + * |
| 64 | + * @return bool |
| 65 | + */ |
| 66 | + public function isNotEmpty() |
| 67 | + { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @param Collection<int, User> $collection |
| 75 | + */ |
| 76 | +function test(Collection $collection): void |
| 77 | +{ |
| 78 | + assertType('AssertArglessCallTemplateDefault\User|null', $collection->first()); |
| 79 | + if ($collection->isNotEmpty()) { |
| 80 | + assertType('AssertArglessCallTemplateDefault\User', $collection->first()); |
| 81 | + assertType("'foo'|AssertArglessCallTemplateDefault\User", $collection->first(null, 'foo')); |
| 82 | + } else { |
| 83 | + assertType('null', $collection->first()); |
| 84 | + assertType("'foo'|AssertArglessCallTemplateDefault\User", $collection->first(null, 'foo')); |
| 85 | + } |
| 86 | + if ($collection->isEmpty()) { |
| 87 | + assertType('null', $collection->first()); |
| 88 | + } else { |
| 89 | + assertType('AssertArglessCallTemplateDefault\User', $collection->first()); |
| 90 | + } |
| 91 | + |
| 92 | + assertType('AssertArglessCallTemplateDefault\User|null', $collection->last()); |
| 93 | +} |
0 commit comments