|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +// The native flavour of every call-like handler: MethodCallHandler, |
| 4 | +// StaticCallHandler, FuncCallHandler (named, and a callable value) and |
| 5 | +// NewHandler. |
| 6 | + |
| 7 | +namespace Bug15169Calls; |
| 8 | + |
| 9 | +use function PHPStan\Testing\assertNativeType; |
| 10 | +use function PHPStan\Testing\assertType; |
| 11 | + |
| 12 | +class C |
| 13 | +{ |
| 14 | + |
| 15 | + /** @return array{a: int} */ |
| 16 | + public function info(): array |
| 17 | + { |
| 18 | + return ['a' => 1]; |
| 19 | + } |
| 20 | + |
| 21 | + /** @return array{a: int} */ |
| 22 | + public static function staticInfo(): array |
| 23 | + { |
| 24 | + return ['a' => 1]; |
| 25 | + } |
| 26 | + |
| 27 | + /** @return array{a: int} */ |
| 28 | + public function __invoke(): array |
| 29 | + { |
| 30 | + return ['a' => 1]; |
| 31 | + } |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +/** @return array{a: int} */ |
| 36 | +function info(): array |
| 37 | +{ |
| 38 | + return ['a' => 1]; |
| 39 | +} |
| 40 | + |
| 41 | +/** @template T */ |
| 42 | +class Generic |
| 43 | +{ |
| 44 | + |
| 45 | + /** @var T */ |
| 46 | + public $value; |
| 47 | + |
| 48 | + /** @param T $value */ |
| 49 | + public function __construct($value) |
| 50 | + { |
| 51 | + $this->value = $value; |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +function methodCall(C $c): void |
| 57 | +{ |
| 58 | + assertType('array{a: int}', $c->info()); |
| 59 | + assertNativeType('array', $c->info()); |
| 60 | + |
| 61 | + $name = 'info'; |
| 62 | + assertType('array{a: int}', $c->$name()); |
| 63 | + assertNativeType('array', $c->$name()); |
| 64 | +} |
| 65 | + |
| 66 | +function staticCall(): void |
| 67 | +{ |
| 68 | + assertType('array{a: int}', C::staticInfo()); |
| 69 | + assertNativeType('array', C::staticInfo()); |
| 70 | + |
| 71 | + $name = 'staticInfo'; |
| 72 | + assertType('array{a: int}', C::$name()); |
| 73 | + assertNativeType('array', C::$name()); |
| 74 | +} |
| 75 | + |
| 76 | +function funcCall(C $c): void |
| 77 | +{ |
| 78 | + assertType('array{a: int}', info()); |
| 79 | + assertNativeType('array', info()); |
| 80 | + |
| 81 | + // a call on a callable value goes through FuncCallHandler's |
| 82 | + // $expr->name instanceof Expr branch: __invoke, a callable string and a |
| 83 | + // callable array |
| 84 | + assertType('array{a: int}', $c()); |
| 85 | + assertNativeType('array', $c()); |
| 86 | + |
| 87 | + $callableString = 'Bug15169Calls\info'; |
| 88 | + assertType('array{a: int}', $callableString()); |
| 89 | + assertNativeType('array', $callableString()); |
| 90 | + |
| 91 | + $callableArray = [$c, 'info']; |
| 92 | + assertType('array{a: int}', $callableArray()); |
| 93 | + assertNativeType('array', $callableArray()); |
| 94 | +} |
| 95 | + |
| 96 | +function instantiation(): void |
| 97 | +{ |
| 98 | + // the template argument is inferred from the phpdoc @param, so it stays |
| 99 | + // unresolved in the native flavour |
| 100 | + assertType('Bug15169Calls\Generic<int>', new Generic(1)); |
| 101 | + assertNativeType('Bug15169Calls\Generic<mixed>', new Generic(1)); |
| 102 | + |
| 103 | + assertType('int', (new Generic(1))->value); |
| 104 | + assertNativeType('mixed', (new Generic(1))->value); |
| 105 | +} |
| 106 | + |
| 107 | +function issetOverThem(C $c): void |
| 108 | +{ |
| 109 | + assertType('false', isset($c->info()['zzz'])); |
| 110 | + assertNativeType('bool', isset($c->info()['zzz'])); |
| 111 | + |
| 112 | + assertType('false', isset(C::staticInfo()['zzz'])); |
| 113 | + assertNativeType('bool', isset(C::staticInfo()['zzz'])); |
| 114 | + |
| 115 | + assertType('false', isset(info()['zzz'])); |
| 116 | + assertNativeType('bool', isset(info()['zzz'])); |
| 117 | + |
| 118 | + assertType('false', isset($c()['zzz'])); |
| 119 | + assertNativeType('bool', isset($c()['zzz'])); |
| 120 | +} |
0 commit comments