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

Add missing instanceof check #4401

Merged
merged 1 commit into from Nov 3, 2020
Merged

Add missing instanceof check #4401

merged 1 commit into from Nov 3, 2020

Conversation

BenMorel
Copy link
Contributor

@BenMorel BenMorel commented Nov 1, 2020

Q A
Type improvement
BC Break no
Fixed issues none

Summary

The $expectedClass variable is currently unused. It should be used for an instanceof check.

@BenMorel
Copy link
Contributor Author

BenMorel commented Nov 2, 2020

Psalm complains that the condition is redundant:

ERROR: RedundantCondition - tests/Driver/API/ExceptionConverterTest.php:54:15 - Type Doctrine\DBAL\Exception\DriverException for $dbalException is always Doctrine\DBAL\Exception\DriverException (see https://psalm.dev/122)
self::assertInstanceOf($expectedClass, $dbalException);

Should we mute Psalm on this one, or should we remove $expectedClass altogether, trusting that Psalm will always check types ahead of time?

@greg0ire
Copy link
Member

greg0ire commented Nov 2, 2020

I think if there was a possibility of returning something else, Psalm would complain, so I would remove $expectedClass.

@BenMorel
Copy link
Contributor Author

BenMorel commented Nov 2, 2020

Actually, unless I'm mistaken, there may be a problem with Psalm's analysis here:

  • testConvertsException() accepts class-string<DriverException> $expectedClass
  • ExceptionConverter::convert() returns DriverException

And the test is:

self::assertInstanceOf($expectedClass, $dbalException);

But the sample data passes, for example, ForeignKeyConstraintViolationException as a class-string. So while $dbalException is a DriverException, there is no guarantee that it passes the instanceof check for the subclass of DriverException.

Is this a bug in Psalm?

@greg0ire
Copy link
Member

greg0ire commented Nov 3, 2020

I think it might indeed be a bug, but I cannot reproduce it: https://psalm.dev/r/7cfea887fe (or maybe it should in fact complain here)?
I reproduced the issue locally, and upgrading Psalm does not help.

UPD: my reproducer is wrong, assert is often used to coerce a type when there is uncertainty.
But even if I try if ('a' === 'a'), I don't get an error from Psalm 😅

@BenMorel
Copy link
Contributor Author

BenMorel commented Nov 3, 2020

Suprising! Not sure how to report it then. Should we suppress the Psalm error in the meantime?

@greg0ire
Copy link
Member

greg0ire commented Nov 3, 2020

Not sure how to report it then.

Well I can reproduce it locally, so a reproducer based on git clone and so on should be fine. Please report the issue, and add a link to the issue in a comment above the ignore rule you want to add.

@BenMorel
Copy link
Contributor Author

BenMorel commented Nov 3, 2020

@greg0ire Done!

@BenMorel
Copy link
Contributor Author

BenMorel commented Nov 3, 2020

sebastianbergmann/phpunit#4509 has already been merged, so the fix should land in the next PHPUnit 8.5 release. Until then, @psalm-suppress will do!

@greg0ire greg0ire requested a review from morozov November 3, 2020 21:28
@morozov
Copy link
Member

morozov commented Nov 3, 2020

Thanks for taking care of this and identifying the upstream issue. I also noticed this issue while merging these changes up but never prioritized fixing it.

<errorLevel type="suppress">
<!--
Requires a release of
https://github.com/sebastianbergmann/phpunit/commit/9c60d7d9fd3bfa80fa4aeab7090e1bbe0830dbcd
Copy link
Member

Choose a reason for hiding this comment

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

In the future, I believe it's better to identify the issue by the GitHub issue rather than a commit. Otherwise, it requires extra navigation which may not always work (e.g. if commit gets rebased or processed in some other way).

Copy link
Member

Choose a reason for hiding this comment

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

I advise this because the commit view allows to see which tags/branches it is on. I wonder if that works in case of "Rebase and merge" / "squash"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can always navigate from issue to commit, not sure the opposite is true?

Copy link
Member

Choose a reason for hiding this comment

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

In most cases you can, I'm not sure if there are exceptions. Anyway, the PR does not contain information about branches and tags, only the commit does AFAIK

Copy link
Member

@morozov morozov Nov 4, 2020

Choose a reason for hiding this comment

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

The exception is when a PR gets rebased (effectively, excluded from the tree). If the navigation works from the commit to the PR, it will work the other way around as well. At the same time, a PR or a commit not always exists (it can be just an issue), so from the consistency standpoint, it's better to link to issues and PRs instead of issues and commits. Additionally, issues usually contain more context, discussions, labels, etc.

@morozov morozov added this to the 3.0.0 milestone Nov 3, 2020
@morozov morozov merged commit 1a9da6d into doctrine:3.0.x Nov 3, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants