PhpStorm 2017.3.2; plugin version: 2.3.15.1; PHP Language Level is set to 7.0
Got new suggestions after upgrading this plugin today. Generates wrong code that PhpStorm complains about straight away.
<?php
class SomeClass
{
protected $someVar;
/**
* Some clever method
*
* @return $this
*/
public function doSomething()
{
$this->doThat();
$this->someVar++;
return $this;
}
protected function doThat()
{}
}
As of today the inspection suggest me that
': static' can be declared as return type hint

If I use proposed quick fix it generates the wrong code that PhpStorm complains straight way:
public function doSomething(): static

@kalessil It seems that https://github.com/kalessil/phpinspectionsea/blob/C-2.3.15.1/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/languageConstructions/ReturnTypeCanBeDeclaredInspector.java#L53 shouldn't be there as it is not a valid return type.
That's some magic inside IDE types resolving, seems it resolved $this as static and I need fix type hint generation.
I'll make a fix, the short-term solution is to disable an option saying to respect return-annotation.
That's some magic inside IDE types resolving, seems it resolved $this as static
Pretty much -- I can use it instead $this in @return tags and the effect will be the same in terms of code completion behavior regardless if it's a static or normal method.
Since it seems related I'll mention it can suggest : void as a return type for getters, like in this code:
<?php
class TestClass
{
protected $something;
public function mayChangeIt(): void
{
$this->something = 'Now has a value';
}
public function getSomething()
{
return $this->something;
}
}
@Logiar : more or less related, I'll check what why the inspection doesn't suggest void there.
@kalessil in case it was misunderstood, it suggests that getSomething() should be getSomething(): void.
@Logiar : I see now, thank you for clarification.
@bazzik : fixed
@Logiar : fixed
@kalessil
May I ask how it was fixed?
I was kind of expecting to see ": self" offered instead of invalid ": static" .. but inspection does not offer anything.
If I change @return $this to be @return self then inspection offers adding ": self".
Am I misunderstanding how it meant to work (actual PHP code)? It seem to wok OK here:
<?php
declare(strict_types=1);
class SomeClass
{
protected $someVar;
/**
* @return $this
*/
public function doSomething(): self
{
$this->doThat();
$this->someVar++;
return $this;
}
protected function doThat()
{}
}
class AnotherClass extends SomeClass {
public function sayYes(): self
{
//TODO: Implement sayYes() method
echo "Yes!\n";
return $this;
}
}
(new AnotherClass())->doSomething()->sayYes()->sayYes();
@bazzik : I added logic filtering $this and static in PhpDoc when suggesting self as return type.
I can translate $this and static into self - will it be correct?
@kalessil
Based on current PHP 7.1/Laravel 5.5 project + plus taking that code sample above I say: offer : self if you see @return $this -- it seem to work OK. Maybe introduce a check box for that inspection if desired (but make it enabled by default).
I would leave @return static cases aside for the moment (as it really meant to be used in static context only ... despite the fact that it works the same as @return $this in PhpStorm (at least in all of my experiments in this regard)). But check box with disabled by default state may also be introduced.
That's if you can differentiate between such @return values (not sure if you can work with PHPDoc directly or only what IDE provides you).
@bazzik : thank you for checking, we are good to go with #761 then (@return $this -> self)