Skip to content

Commit

Permalink
use conditional parameter for is_a() via stub
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored May 13, 2022
1 parent f23f7e6 commit 9d9dcd8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Analyser/DirectScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
class DirectScopeFactory implements ScopeFactory
{

/**
* @param class-string $scopeClass
*/
public function __construct(
private string $scopeClass,
private ReflectionProvider $reflectionProvider,
Expand Down
3 changes: 3 additions & 0 deletions src/Analyser/LazyScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class LazyScopeFactory implements ScopeFactory

private bool $explicitMixedInUnknownGenericNew;

/**
* @param class-string $scopeClass
*/
public function __construct(
private string $scopeClass,
private Container $container,
Expand Down
2 changes: 1 addition & 1 deletion stubs/core.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @param mixed $object_or_class
* @param ($allow_string is false ? object : object|string) $object_or_class
* @param class-string $class
* @param bool $allow_string
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,34 @@ public function testBug7017(): void
$this->analyse([__DIR__ . '/data/bug-7017.php'], $errors);
}

public function testBug4371(): void
{
$errors = [
[
'Parameter #1 $object_or_class of function is_a expects object, string given.',
14,
],
[
'Parameter #1 $object_or_class of function is_a expects object, string given.',
22,
],
];

if (PHP_VERSION_ID < 80000) {
// php 7.x had different parameter names
$errors = [
[
'Parameter #1 $object_or_string of function is_a expects object, string given.',
14,
],
[
'Parameter #1 $object_or_string of function is_a expects object, string given.',
22,
],
];
}

$this->analyse([__DIR__ . '/data/bug-4371.php'], $errors);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-4371.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Bug4371;

class Foo {
}

class Bar extends Foo {

}

class Hello {
public function foo() {
if(is_a(Bar::class, Foo::class)) { // should error
echo "This will never be true";
} else {
echo "NO";
}
}

public function bar() {
if(is_a(Bar::class, Foo::class, false)) { // should error
echo "This will never be true";
} else {
echo "NO";
}
}

public function allFine() {
if(is_a(Bar::class, Foo::class, true)) { // no error
echo "All good";
}
}
}

0 comments on commit 9d9dcd8

Please sign in to comment.