Skip to content

Commit 9d9dcd8

Browse files
authored
use conditional parameter for is_a() via stub
1 parent f23f7e6 commit 9d9dcd8

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

src/Analyser/DirectScopeFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
class DirectScopeFactory implements ScopeFactory
2424
{
2525

26+
/**
27+
* @param class-string $scopeClass
28+
*/
2629
public function __construct(
2730
private string $scopeClass,
2831
private ReflectionProvider $reflectionProvider,

src/Analyser/LazyScopeFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class LazyScopeFactory implements ScopeFactory
2424

2525
private bool $explicitMixedInUnknownGenericNew;
2626

27+
/**
28+
* @param class-string $scopeClass
29+
*/
2730
public function __construct(
2831
private string $scopeClass,
2932
private Container $container,

stubs/core.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* @param mixed $object_or_class
4+
* @param ($allow_string is false ? object : object|string) $object_or_class
55
* @param class-string $class
66
* @param bool $allow_string
77
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,34 @@ public function testBug7017(): void
949949
$this->analyse([__DIR__ . '/data/bug-7017.php'], $errors);
950950
}
951951

952+
public function testBug4371(): void
953+
{
954+
$errors = [
955+
[
956+
'Parameter #1 $object_or_class of function is_a expects object, string given.',
957+
14,
958+
],
959+
[
960+
'Parameter #1 $object_or_class of function is_a expects object, string given.',
961+
22,
962+
],
963+
];
964+
965+
if (PHP_VERSION_ID < 80000) {
966+
// php 7.x had different parameter names
967+
$errors = [
968+
[
969+
'Parameter #1 $object_or_string of function is_a expects object, string given.',
970+
14,
971+
],
972+
[
973+
'Parameter #1 $object_or_string of function is_a expects object, string given.',
974+
22,
975+
],
976+
];
977+
}
978+
979+
$this->analyse([__DIR__ . '/data/bug-4371.php'], $errors);
980+
}
981+
952982
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug4371;
4+
5+
class Foo {
6+
}
7+
8+
class Bar extends Foo {
9+
10+
}
11+
12+
class Hello {
13+
public function foo() {
14+
if(is_a(Bar::class, Foo::class)) { // should error
15+
echo "This will never be true";
16+
} else {
17+
echo "NO";
18+
}
19+
}
20+
21+
public function bar() {
22+
if(is_a(Bar::class, Foo::class, false)) { // should error
23+
echo "This will never be true";
24+
} else {
25+
echo "NO";
26+
}
27+
}
28+
29+
public function allFine() {
30+
if(is_a(Bar::class, Foo::class, true)) { // no error
31+
echo "All good";
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)