Navigation Menu

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

Level 7: has* checks #534

Closed
hultberg opened this issue Oct 4, 2017 · 4 comments
Closed

Level 7: has* checks #534

hultberg opened this issue Oct 4, 2017 · 4 comments

Comments

@hultberg
Copy link

hultberg commented Oct 4, 2017

Consider the following code:

<?php declare(strict_types = 1);

class HelloWorld
{
	public $obj = null;
	
	public function getObj(): ?HelloWorld
	{
		return $this->obj;
	}
	
	public function hasObj(): bool
	{
		return !is_null($this->obj);
	}
}

$c = new HelloWorld();

if ($c->hasObj()) {
	$c->getObj()->getObj();
}

Here, phpstan will report the following error:

 ------ ------------------------------------------------------------------------- 
  Line   analyzed.php                                                             
 ------ ------------------------------------------------------------------------- 
  21     Calling method getObj() on possibly null value of type HelloWorld|null.  
 ------ ------------------------------------------------------------------------- 

I can understand this error as I have declared getObj to return null or HelloWorld instance, however, this can never occur because of my hasObj check, but phpstan does not seem to understand this. Is this a bug or feature request?

https://phpstan.org/r/2c5f7204f766e044cf3bb54192c84b2e

Cheers

@hultberg
Copy link
Author

hultberg commented Oct 4, 2017

I was unable to find duplicates while searching, so I reported this just to make sure it is reported. :-)

@ondrejmirtes ondrejmirtes added this to the Type-specifying extension milestone Oct 4, 2017
@JanTvrdik
Copy link
Contributor

@hultberg That's expected behavior. TypeScript would report the same error.

@ondrejmirtes
Copy link
Member

Hi, this would require a new type of an extension - "type-specifying extension" to help PHPStan make sure that something is of a specific type after a function or method is called.

But for now, I recommend you the following structure:

  • find methods that return nullable type
  • has methods (that you already have) that return bool
  • get methods that return the type without null (no ? in typehint)

If you ask has before calling get then your code is typesafe. I'm doing this (and know about other projects that do this as well) and it's great.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Apr 2, 2018

This is already possible on dev-master. The Type-specifying extension support will be released as part of 0.10 during Q2 2018. See some examples on how to write type-specifying extensions:

Let me know if you need any help with writing such an extension.

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

No branches or pull requests

3 participants