Skip to content

use conditional parameter for is_a() via stub #1311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 13, 2022
Merged
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
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";
}
}
}