Skip to content

use conditional return type for is_a() via stub #1310

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 3 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
8 changes: 8 additions & 0 deletions stubs/core.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/**
* @param mixed $object_or_class
Copy link
Contributor Author

Choose a reason for hiding this comment

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

couldn't we even use a conditional parameter type here?

Suggested change
* @param mixed $object_or_class
* @param ($allow_string is false ? object : object|class-string) $object_or_class

Copy link
Member

Choose a reason for hiding this comment

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

Try it after this PR is merged :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh my.. now I get what you was telling me in #1306 (comment) .. will send a 2nd PR ..

* @param class-string $class
* @param bool $allow_string
* @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
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4371.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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): 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));
}
}