[PhpStorm] Possibility to generate "adder" / "remover" php functions on a class property

I very often use adder/remover on class array properties for instance like thihs :
<?php
class foo {
    /**
     * @var array
     */      
    protected $friends;
    
    public function addFriend($friend)
    {
     // can be customized
        $this->friends[] = $friend;
        return $this;
    }

     public function removeFriend($friend)
    {
       // custom template
    }
}

The class context menu in PhpStorm has a "Generate.." entry to create getters/setters and it cruelly lacks this feature...
Templates could be customized like getters/setters etc.
Any idea to achieve this ?

Thank you !

0
2 comments

Hi there,

If you want them as separate options in "Generate" .. then it can be implemented via custom plugin.

Without coding your own plugin .. the only thing you may want to try is to add templates code for "add" and "remove" into the setter/getter code templates (so you will have set+add and get+remove .. or whatever). Yes, it's very messy and, obviously, generating getter for any property (in any class/project) should produce getXXX() as well as removeXXX() methods. Extremely likely that you would not like to do that (I would not -- I would prefer adding such methods manually).

0

Check this plugin https://plugins.jetbrains.com/plugin/8132
Must be working on PhpStorm 9 and 10.

0

Please sign in to leave a comment.