|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug4510; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class HelloWorld |
| 8 | +{ |
| 9 | + public function existingMethod(): void {} |
| 10 | + |
| 11 | + public function doSomething(string $method): void { |
| 12 | + if (!method_exists($this, $method)) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + [$this, $method](); // error - method_exists doesn't imply callable |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function testMethodExists(string $method): void { |
| 21 | + $instance = new HelloWorld(); |
| 22 | + if (!method_exists($instance, $method)) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + assertType('array{Bug4510\HelloWorld, string}', [$instance, $method]); |
| 27 | + [$instance, $method](); // error - method_exists doesn't imply callable |
| 28 | +} |
| 29 | + |
| 30 | +function testIsCallableInlineArray(string $method): void { |
| 31 | + $instance = new HelloWorld(); |
| 32 | + if (!is_callable([$instance, $method])) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + assertType('list{Bug4510\HelloWorld, string}&callable(): mixed', [$instance, $method]); |
| 37 | + [$instance, $method](); // ok - is_callable verifies callability |
| 38 | +} |
| 39 | + |
| 40 | +function testMethodExistsWithClassString(string $method): void { |
| 41 | + if (!method_exists(HelloWorld::class, $method)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + assertType("array{'Bug4510\\\\HelloWorld', string}", [HelloWorld::class, $method]); |
| 46 | + [HelloWorld::class, $method](); // error - method_exists doesn't imply callable |
| 47 | +} |
| 48 | + |
| 49 | +function testIsCallableWithClassString(string $method): void { |
| 50 | + if (!is_callable([HelloWorld::class, $method])) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + assertType("list{'Bug4510\\\\HelloWorld', string}&callable(): mixed", [HelloWorld::class, $method]); |
| 55 | + [HelloWorld::class, $method](); // ok - is_callable verifies callability |
| 56 | +} |
| 57 | + |
| 58 | +function testIsCallableExplicitKeys(string $method): void { |
| 59 | + $instance = new HelloWorld(); |
| 60 | + if (!is_callable([0 => $instance, 1 => $method])) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + assertType('list{Bug4510\HelloWorld, string}&callable(): mixed', [0 => $instance, 1 => $method]); |
| 65 | + [0 => $instance, 1 => $method](); // ok - is_callable verifies callability |
| 66 | +} |
| 67 | + |
| 68 | +function testIsCallableExplicitKeysWithClassString(string $method): void { |
| 69 | + if (!is_callable([0 => HelloWorld::class, 1 => $method])) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + assertType("list{'Bug4510\\\\HelloWorld', string}&callable(): mixed", [0 => HelloWorld::class, 1 => $method]); |
| 74 | + [0 => HelloWorld::class, 1 => $method](); // ok - is_callable verifies callability |
| 75 | +} |
| 76 | + |
| 77 | +function testWithDynamicMethodExistsAndVariable(string $method): void { |
| 78 | + $instance = new HelloWorld(); |
| 79 | + $callable = [$instance, $method]; |
| 80 | + if (!is_callable($callable)) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $callable(); // ok - is_callable on variable already worked |
| 85 | +} |
| 86 | + |
| 87 | +function testMethodExistsInElseBranch(string $method): void { |
| 88 | + $instance = new HelloWorld(); |
| 89 | + if (method_exists($instance, $method)) { |
| 90 | + [$instance, $method](); // error - method_exists doesn't imply callable |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function testIsCallableInElseBranch(string $method): void { |
| 95 | + $instance = new HelloWorld(); |
| 96 | + if (is_callable([$instance, $method])) { |
| 97 | + [$instance, $method](); // ok - is_callable verifies callability |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function testIsCallableNamedArg(string $method): void { |
| 102 | + $instance = new HelloWorld(); |
| 103 | + if (!is_callable(value: [$instance, $method])) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + assertType('list{Bug4510\HelloWorld, string}&callable(): mixed', [$instance, $method]); |
| 108 | + [$instance, $method](); // ok - is_callable verifies callability |
| 109 | +} |
| 110 | + |
| 111 | +function testMethodExistsNamedArgs(string $method): void { |
| 112 | + $instance = new HelloWorld(); |
| 113 | + if (!method_exists(object_or_class: $instance, method: $method)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + assertType('array{Bug4510\HelloWorld, string}', [$instance, $method]); |
| 118 | + [$instance, $method](); // error - method_exists doesn't imply callable |
| 119 | +} |
| 120 | + |
| 121 | +function testNoMethodExists(string $method): void { |
| 122 | + $instance = new HelloWorld(); |
| 123 | + assertType('array{Bug4510\HelloWorld, string}', [$instance, $method]); |
| 124 | +} |
| 125 | + |
| 126 | +function testIsCallableFalseBranch(string $method): void { |
| 127 | + $instance = new HelloWorld(); |
| 128 | + if (is_callable([$instance, $method])) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + assertType('array{Bug4510\HelloWorld, string}', [$instance, $method]); |
| 133 | + [$instance, $method](); // error - is_callable was false |
| 134 | +} |
0 commit comments