Skip to content

Commit 1f38c6c

Browse files
authored
use conditional return type for is_a() via stub (#1310)
1 parent b3a6d39 commit 1f38c6c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

stubs/core.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?php
22

3+
/**
4+
* @param mixed $object_or_class
5+
* @param class-string $class
6+
* @param bool $allow_string
7+
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
8+
*/
9+
function is_a($object_or_class, string $class, $allow_string = false): bool{}
10+
311
/**
412
* @param mixed $var
513
* @param bool $return

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ public function dataFileAsserts(): iterable
885885
yield from $this->gatherAssertTypes(__DIR__ . '/data/pr-1244.php');
886886
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144.php');
887887
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144-composer-integration.php');
888+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4371.php');
888889
}
889890

890891
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug4371;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
/**
12+
* @param Exception $exception
13+
* @param class-string $classString
14+
*/
15+
public function sayHello(Exception $exception, $classString): void
16+
{
17+
assertType('bool', is_a($exception, $classString));
18+
assertType('bool', is_a($exception, $classString, true));
19+
assertType('bool', is_a($exception, $classString, false));
20+
21+
$exceptionClass = get_class($exception);
22+
assertType('false', is_a($exceptionClass, $classString));
23+
assertType('bool', is_a($exceptionClass, $classString, true));
24+
assertType('false', is_a($exceptionClass, $classString, false));
25+
}
26+
}

0 commit comments

Comments
 (0)