Skip to content
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

Improve class_alias robustness #315

Merged
merged 3 commits into from
Feb 3, 2020
Merged

Improve class_alias robustness #315

merged 3 commits into from
Feb 3, 2020

Conversation

guilliamxavier
Copy link
Contributor

  • Use ::class rather than string literal, to avoid problems like Remove space in class_alias #313
  • Use distinct alias names (concatenate current file class + external class) rather than class_exists, to avoid conflicts without triggering autoload looking for nonexistent files like "src/EventListener/UserContextRequestEvent.php"

@guilliamxavier
Copy link
Contributor Author

I will try to make PHPStan happy (after meal)...

@guilliamxavier
Copy link
Contributor Author

After searching and trying many things, there appears to be a limitation of PHPStan with ::class on undefined classes (even with class_exists / class_alias), e.g. the following snippet:

<?php
namespace My\Ns;
class Foo {}
if (!class_exists(Bar::class)) {
    class_alias(Foo::class, Bar::class);
}
function f(Bar $x): void {
    var_dump($x);
}
f(new Foo());

is valid and will output (https://3v4l.org/pPO3n)

object(My\Ns\Foo)#1 (0) {
}

but PHPStan complains (https://phpstan.org/r/b7443e0a-20b9-4e77-8710-51538dfbea57)

+--------------------------------------------------------------------------------+
| Line | test.php                                                                |
+--------------------------------------------------------------------------------+
| 4    | Class My\Ns\Bar not found.                                              |
| 5    | Class My\Ns\Bar not found.                                              |
| 7    | Parameter $x of function My\Ns\f() has invalid typehint type My\Ns\Bar. |
| 10   | Parameter #1 $x of function My\Ns\f expects My\Ns\Bar, My\Ns\Foo given. |
+--------------------------------------------------------------------------------+

 [ERROR] Found 4 errors 

So I ended up ignoring the errors of https://travis-ci.org/getsentry/sentry-symfony/jobs/643821562#L644

@Jean85 Jean85 merged commit 486c4cd into getsentry:master Feb 3, 2020
@guilliamxavier guilliamxavier deleted the patch-1 branch February 3, 2020 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants