Improved Sublime Text 2 PHP getter and setter generation
As I’ve been using Sublime Text 2 for all coding for the last year, I’ve noticed a significant problem with my simple ‘getset‘ snippet that I created last year: it doesn’t handle underscores in property names correctly.
I’ve finally got around to fix it!
As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. This snippet creates a getXxx() and setXxx() method from a property name.
To create a snippet, go to Tools->New Snippet... and replace the code example provided with this:
<snippet> <content><![CDATA[/** * Getter for ${1:$SELECTION} * * @return mixed */ public function get${1/(?:^|_)(w)/U$1$2/g$1}() { return $this->$1; } /** * Setter for $1 * * @param mixed $${1/_(w)/U$1/g$1} Value to set * @return self */ public function set${1/(?:^|_)(w)/U$1$2/g$1}($${1/_(w)/U$1/g$1}) { $this->$1 = $${1/_(w)/U$1/g$1}; return $this; } ]]></content> <!-- Optional: Tab trigger to activate the snippet --> <tabTrigger>gs</tabTrigger> <!-- Optional: Scope the tab trigger will be active in --> <scope>source.php</scope> <!-- Optional: Description to show in the menu --> <description>Create getter and setter methods</description> </snippet>
Save the file as getset.sublime-snippet and you’re done.
To use, simply type gs followed by tab and it will automatically expand and you can then type your underscore_separated property name and the correct method names will be created.
Alternatively, select some text and press shift+cmd+p and then type gs to automatically replace the selected text with the get and set methods completed for the text that was selected.
As an example, typing gs {tab} date_last_updated will produce:
/**
* Getter for date_last_updated
*
* @return mixed
*/
public function getDateLastUpdated()
{
return $this->date_last_updated;
}
/**
* Setter for date_last_updated
*
* @param mixed $dateLastUpdated Value to set
* @return self
*/
public function setDateLastUpdated($dateLastUpdated)
{
$this->date_last_updated = $dateLastUpdated;
return $this;
}
I’ve also updated the snippet in my sublime-akrabat package on github.
This is an awesome snippet I really use a lot during development. The one modification I made is the option to type hint setters.
The ($${1/_(w)/U$1/g$1}) becomes ($2$${1/_(w)/U$1/g$1}).
First normal example:
gs {tab} date_last_updated {tab}{tab}
Shows above code
However this:
gs {tab} date_last_updated {tab} DateTime {tab}
Will create this setter:
public function setDateLastUpdated(DateTime $dateLastUpdated)
{
$this->date_last_updated = $dateLastUpdated;
return $this;
}
Or… just use IDE :)
Hi Rob,
Concerning this subject, I've published a personal opinion on underscores in properties of ZF2 classes. I had the same problem… And decides to swith to camelCase properties :P
Let me know what you think about this really big philosophical concern (if you have enough time ;)
http://www.adfab.fr/psr-0-1-2-and-now-no_more_underscores/
Thx,
Greg