How can I place the auto-generated getter before the setter?

Hello,

I would like to generate my getters and setter placing the getter before the setter, does it is possible to? Currently, by default is exactly the opposite.

Thanks in advance,
Marcos

10 comments

I've saw that and I've read the article but I've no idea how change the method order. Can you help me?

0

Well ... this is simple basically EMPTY class

class OneOne {
    protected $meow = 1;
}


This is the result of generating both setter and getter (in one go)

class OneOne {
    protected $meow = 1;


    /**
     * @return int
     */
    public function getMeow()
    {
        return $this->meow;
    }


    /**
     * @param int $meow
     */
    public function setMeow($meow)
    {
        $this->meow = $meow;
    }
}


As you can see -- getter is placed BEFORE the setter method.

My current settings -- possibly you need to specify A-Z sorting order:
screen01.png

0

I understood what you said, but what I'm trying to do is configure the auto generator to generate the getters and setters methods in the same order of the properties but placing the getter before the setter. I believe it's possible, but I don't know how to achieve it (this interface is not intuitive).

0

I'm not 100% sure what you mean here. Could you please provide simple example (2-3 properties and their setters/getters) of how you want them to appear -- just to clarify the requirements?

0

I've this:

class ClassName {     protected $c;     protected $b;     protected $a; }


This is my goal:

class ClassName {     protected $c;     protected $b;     protected $a;     public function getC()     {         return $c;     }     public function setC($c)     {         $this->c = $c;     }     public function getB()     {         return $b;     }     public function setB($b)     {         $this->b = $b;     }     public function getA()     {         return $a;     }     public function setA($a)     {         $this->a = $a;     } }

0

Well ... I just reset those settings to their defaults .. and went to generate setters/getters.

If I do generate them one by one (first for $c, then for $b and then for $a), then they are kept in the same order .. but if I do bulk generation .. it gets screwed (setter before getter indeed and other side effects).

First for $c:

class ClassName
{
    protected $c;
    protected $b;
    protected $a;

    /**
     * @return mixed
     */
    public function getC()
    {
        return $this->c;
    }

    /**
     * @param mixed $c
     * @return ClassName
     */
    public function setC($c)
    {
        $this->c = $c;

        return $this;
    }
}


Then for $b:

class ClassName
{
    protected $c;
    protected $b;
    protected $a;


    /**
     * @return mixed
     */
    public function getC()
    {
        return $this->c;
    }


    /**
     * @param mixed $c
     * @return ClassName
     */
    public function setC($c)
    {
        $this->c = $c;


        return $this;
    }


    /**
     * @return mixed
     */
    public function getB()
    {
        return $this->b;
    }


    /**
     * @param mixed $b
     * @return ClassName
     */
    public function setB($b)
    {
        $this->b = $b;


        return $this;
    }
}


Please note, that it makes difference (for whatever reason) how you generate them (how you call that generate command).

If you use "Code | Generate ... |  Getters/Setters..." .. then result is a bit screwed.

But if you access it via Quick Fix menu (Alt+Enter on the field name) then it works fine:
screen01.png

There is a ticket or two about this -- hopefully it will be fixed in v7

0

Alt + Enter does not work on Mac (I believe the equivalent on Mac is [Command] + [N]). Anyway, there isn't a way to use the bulk generation keeping the property declaration order, right?

0
Alt + Enter does not work on Mac (I believe should be Command + Enter)

I do not know for sure what the shortcut is on Mac .. but you can check it (and modify if desired) yourself at "Preferences | Keymap" -- look for "Other | Show Intention Actions"

Anyway, there isn't a way to use the bulk generation keeping the property declaration order, right?

Well .. accordingly to the aforementioned ticket -- no (at least ATM as it simply does not work as intended right now). You can ask in that (or another, possibly more correct) ticket for clarification/details (I'm not a dev to tell you such details).

0

Ok, thanks so much for your time.

0

Please sign in to leave a comment.