Type hinting from setters
I would like to know if Phpstorm could determine the class of a field from a setter, like it does it with the contructor.
Example:
class MyClass
{
private
$pony,
$burger;
public function __construct()
{
$this->pony = new Pony();
}
public function setBurger()
{
$this->burger = new Burger();
}
public function doSomething()
{
$this->pony->kill(); //With type hinting
$this->burger->eat(); //Without type hinting
}
}
I would like that Phpstorm "know" that my $burger is a Burger withtout the use of phpdoc.
请先登录再写评论。
Hi there,
>I would like to know if Phpstorm could determine the class of a field from a setter, like it does it with the contructor.
No -- only constructors have such special treatment.
>withtout the use of phpdoc.
Any particular reason for disliking PHPDoc? It's very handy: it helps IDE where it cannot deduct the type automatically; plus helps others to read your code (no need to guess what this field is about (what type) ... until the right part of code (setter method in your case) will be in view).
Hi, thanks for the answer !
The prohibition of PHPDoc is part of the company policy.
Did you know if this feature could be part of future release or if it can be added with a plugin ?
>The prohibition of PHPDoc is part of the company policy.
o_O interesting policy... My only guess -- to not to deal with payment/performance review based on loc (lines of code) numbers. (well ... whatever -- I was just curious .. as it looks pretty odd nowadays to prohibit code self-documentation: http://www.phptherightway.com/#documenting)
>Did you know if this feature could be part of future release
I dot not represent JetBrains in any way so do not treat this as definite answer .. but very unlikely.
That's what main dev said few years ago: https://youtrack.jetbrains.com/issue/WI-13862#comment=27-789284
>or if it can be added with a plugin?
Custom plugin -- sure, why not -- they can do much more complex things. You just have to write it in Java or Kotlin.
Thanks again for your answers !
The main reason invoqued to prohib the PHPDoc is that "code did not lie, but comments can". You can't really test the PHPDoc, so you could mess up the comments and still pass the continuous integration. It's a bit odd but it will not change soon.
If the policy does not change, I will try to look up to create a pluggin. Thanks for your time !