Skip to content

use conditional parameter for is_a() via stub #1306

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
use conditional parameter for is_a() via stub
  • Loading branch information
clxmstaab authored and staabm committed May 13, 2022
commit 6bbb2a6b4c6374e54e1411f180184a1cd3e2295c
7 changes: 7 additions & 0 deletions stubs/core.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stub taken 1:1 from psalm

Copy link
Contributor Author

@staabm staabm May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: psalm was missing the type for $allow_string, which will be added with vimeo/psalm#7951

* @param mixed $object_or_class
* @param class-string $class
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
*/
function is_a($object_or_class, string $class, $allow_string = false): bool{}

/**
* @param mixed $var
* @param bool $return
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/pr-1244.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144-composer-integration.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4371.php');
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4371.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Bug4371;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param Exception $exception
* @param class-string $classString
*/
public function sayHello(Exception $exception, $classString, string $s): void
{
assertType('bool', is_a($exception, $classString));
assertType('bool', is_a($exception, $classString, true));
assertType('bool', is_a($exception, $classString, false));

$exceptionClass = get_class($exception);
assertType('false', is_a($exceptionClass, $classString));
assertType('bool', is_a($exceptionClass, $classString, true));
assertType('false', is_a($exceptionClass, $classString, false));
Copy link
Contributor Author

@staabm staabm May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with 3e955af I dropped the conditional return type stub again, as I am no longer sure its worth to report about string vs. class-string type errors for $class.. might be tedious.

since we already have extensions for is_a in place the stub itself was nevertheless only partly usefull, I think.


assertType('false', is_a($exceptionClass, $s));
assertType('bool', is_a($exceptionClass, $s, true));
assertType('false', is_a($exceptionClass, $s, false));
}
}