Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ parameters:
- ../stubs/Redis.stub
- ../stubs/ReflectionAttribute.stub
- ../stubs/ReflectionClassConstant.stub
- ../stubs/ReflectionFunction.stub
- ../stubs/ReflectionFunctionAbstract.stub
- ../stubs/ReflectionMethod.stub
- ../stubs/ReflectionParameter.stub
Expand Down
16 changes: 16 additions & 0 deletions stubs/ReflectionFunction.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class ReflectionFunction
{

/**
* @throws \Throwable
*/
public function invoke(mixed ...$args): mixed {}

/**
* @throws \Throwable
*/
public function invokeArgs(array $args): mixed {}

}
10 changes: 10 additions & 0 deletions stubs/ReflectionMethod.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ class ReflectionMethod
*/
public $name;

/**
* @throws \Throwable
*/
public function invoke(?object $object, mixed ...$args): mixed {}

/**
* @throws \Throwable
*/
public function invokeArgs(?object $object, array $args): mixed {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ public function testBug9568(): void
$this->analyse([__DIR__ . '/data/bug-9568.php'], []);
}

public function testBug7719(): void
{
$this->analyse([__DIR__ . '/data/bug-7719.php'], []);
}

public function testBug9267(): void
{
$this->analyse([__DIR__ . '/data/bug-9267.php'], []);
}

#[RequiresPhp('>= 8.4')]
public function testPropertyHooks(): void
{
Expand Down
56 changes: 56 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-7719.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);

namespace Bug7719;

class Endpoint {
public function run(int $id): int {
return $id;
}
}

class HelloWorld
{
public function sayHello(Endpoint $endpoint, string $methodName): void
{
try {
$methodResponse = (new \ReflectionMethod($endpoint, $methodName))->invokeArgs($endpoint, ['id' => 2]);
} catch (\RuntimeException $e) {
echo $e->getMessage();
die;
}
var_dump($methodResponse);
}

public function sayHelloWithInvoke(Endpoint $endpoint, string $methodName): void
{
try {
$methodResponse = (new \ReflectionMethod($endpoint, $methodName))->invoke($endpoint, 2);
} catch (\RuntimeException $e) {
echo $e->getMessage();
die;
}
var_dump($methodResponse);
}

public function sayHelloWithFunction(string $functionName): void
{
try {
$result = (new \ReflectionFunction($functionName))->invokeArgs([1, 2]);
} catch (\RuntimeException $e) {
echo $e->getMessage();
die;
}
var_dump($result);
}

public function sayHelloWithFunctionInvoke(string $functionName): void
{
try {
$result = (new \ReflectionFunction($functionName))->invoke(1, 2);
} catch (\RuntimeException $e) {
echo $e->getMessage();
die;
}
var_dump($result);
}
}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9267.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug9267;

class FooException extends \Exception {}

class C {
/** @return never */
public function test(): never {
throw new FooException("");
}
}

function bar(\ReflectionMethod $r): void {
try {
$r->invokeArgs(new C, array());
}
catch (FooException $e) {
print "CAUGHT FOO!\n";
}
}

function baz(\ReflectionMethod $r): void {
try {
$r->invoke(new C);
}
catch (FooException $e) {
print "CAUGHT FOO!\n";
}
}
Loading