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

abort_if not treated like if statements by type checker #116

Closed
keryja opened this issue Aug 9, 2018 · 4 comments · Fixed by #330
Closed

abort_if not treated like if statements by type checker #116

keryja opened this issue Aug 9, 2018 · 4 comments · Fixed by #330

Comments

@keryja
Copy link

keryja commented Aug 9, 2018

  • Larastan Version: 0.2.4
  • --level used: max

Description:

Currently abort_if does not seem to get treated similarly to if, anything designated as nullable is still considered nullable after abort_if

This results in an error when querying relations on the object:

Cannot call method theRelation() on mixed.

Workaround/example:

Current workaround for this is to use an if statement with a regular abort:

// Doesn't pass checks
$foo = ModelName::where(['foo' => 'bar'])->first();
abort_if(!$foo, 404);

print_r($foo->theRelation());

// Passes checks
$foo = ModelName::where(['foo' => 'bar'])->first();
if (!$foo) {
  abort(404);
}

print_r($foo->theRelation());
@ondrejmirtes
Copy link
Contributor

Well, first, this is a very weird error message that shouldn't happen: Cannot call method theRelation() on mixed.. I'm not sure how that can happen, because MixedType should pass this.

But to the original issue, Larastan should contain these two things:

abort_if should have a very similar type-specifying extension to assert that is in the core

  • abort should be an earlyTerminatingFunctionCall which is not yet possible to configure in PHPStan, just for methods. I'll welcome a PR to phpstan/phpstan that allows this.

@keryja
Copy link
Author

keryja commented Aug 9, 2018

On --level=max calling methods on mixed do trigger errors (I think?). But at least I know what's causing it now, and I'll work around it with the if statements. Thanks for the explanation.

I've whipped up a repository that reproduces the error, before reading your reply fully, anyway if it's ever needed keryja/larastan-error-examples#reproduce-abort-if.

 22/22 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ --------------------------------------------
  Line   app/Http/Controllers/TheController.php
 ------ --------------------------------------------
  16     Cannot call method theRelation() on mixed.
 ------ --------------------------------------------

 [ERROR] Found 1 error

@ondrejmirtes
Copy link
Contributor

On --level=max calling methods on mixed do trigger errors

They don't. The type there must be something else, not MixedType.

@nunomaduro
Copy link
Collaborator

@keryja This issue still exists on the latest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants