vi: Getter und Setter automatisch generieren
in PHP, Tipp | Tags: Code, Generieren, Getter, PHP, Setter, Stub, viIch habe bisher im vi nicht wirklich vermisst, dass Code generiert wird.
Aber seit dem ich es habe, finde ich es nur noch cool. 🙂 Das möchte ich Euch auf jeden Fall vorstellen.
Und zwar heißt das tolle Script php_getset.vim. Zur Installation kopiert man es einfach in den Ordner ftplugin im share-Verzeichnis des vim. Das ist bei mir zur Zeit /usr/share/vim/vim72/ftplugin/. Es generiert in einer PHP-Klassendefinition automatisch Getter und Setter-Funktionen für eine Property (eine Klasseneigenschaft). Zum Beispiel in
1 2 3 4 | class Test { protected $active; } |
class Test { protected $active; }
möchte eigentlich jeder die beiden Methode isActive() und setActive($active) implementieren – wohl zu 99% Wahrscheinlichkeit. Mit diesem Skript (Download unter http://www.vim.org/scripts/script.php?script_id=1707) geht das wie folgt. Man geht auf die Zeile
1 | protected $active |
protected $active
und gibt in dem vi-Kommando-Modus ein:
1 | :InsertBothGetterSetter |
:InsertBothGetterSetter
und schon erscheint folgendes Gerüst
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Test { protected $active; /** * get active */ function getActive() { return $this->active; } /** * set active */ function setActive($active) { $this->active = $active; } } |
class Test { protected $active; /** * get active */ function getActive() { return $this->active; } /** * set active */ function setActive($active) { $this->active = $active; } }
Super-cool. Die Templates für die Funktionen lassen sich in der ~/.vimrc anpassen. Die Variablen %varname% und %funcname% kann man also auch beliebig einbauen, oder die eigene Tab-Weite anpassen. Damit sollte das Plugin soweit fit für den professionellen Einsatz sein und der Tipp- und Doku-Aufwand hat sich merklich verringert. Das ist richtig wohltuend.
getypte Variablen
Allerdings benutze ich gerne “getypte” Variablennamen, also $bActive oder $sURL. Dann würde die Funktion leider immer getSURL() oder getBActive() heißen – das ist natürlich nicht das Gelbe vom Ei. Für diesen Fall habe ich das Plugin ein klein wenig angepasst und ihr bekommt es hier http://www.lieber-linux.de/php_getset.vim zum Download. Es ist kompatibel zum Vorgänger-Script und erkennt camelCased-typisierte Property-Namen mit den Präfixen b (bool), n (int), s (string), f (float), dt (datetime), a (array) und r (ressource). Und das beste daran ist: Jetzt stehen zwei weitere Template-Variablen bereit: %property% und %vartype%. Dadurch lässt sich das Ganze noch mit einem geschickten Casting versehen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | let b:phpgetset_getterTemplate = \ "\n" . \ "\t/**\n" . \ "\t* Get %property%.\n" . \ "\t*\n" . \ "\t* @return %vartype%\n" . \ "\t* @author Mathias Lieber\n" . \ "\t*/\n" . \ "\tpublic function %funcname%()\n" . \ "\t{\n" . \ "\t\treturn (%vartype%) $this->%varname%;\n" . \ "\t}\n\n" let b:phpgetset_setterTemplate = \ "\n" . \ "\t/**\n" . \ "\t* Set %property%.\n" . \ "\t*\n" . \ "\t* @param %vartype% $%varname%\n" . \ "\t* @return bool\n" . \ "\t* @author Mathias Lieber\n" . \ "\t*/\n" . \ "\tpublic function %funcname%($%varname%)\n" . \ "\t{\n" . \ "\t\t$this->%varname% = (%vartype%) $%varname%;\n" . \ "\t\treturn TRUE;\n" . \ "\t}\n\n" |
let b:phpgetset_getterTemplate = \ "\n" . \ "\t/**\n" . \ "\t* Get %property%.\n" . \ "\t*\n" . \ "\t* @return %vartype%\n" . \ "\t* @author Mathias Lieber\n" . \ "\t*/\n" . \ "\tpublic function %funcname%()\n" . \ "\t{\n" . \ "\t\treturn (%vartype%) $this->%varname%;\n" . \ "\t}\n\n" let b:phpgetset_setterTemplate = \ "\n" . \ "\t/**\n" . \ "\t* Set %property%.\n" . \ "\t*\n" . \ "\t* @param %vartype% $%varname%\n" . \ "\t* @return bool\n" . \ "\t* @author Mathias Lieber\n" . \ "\t*/\n" . \ "\tpublic function %funcname%($%varname%)\n" . \ "\t{\n" . \ "\t\t$this->%varname% = (%vartype%) $%varname%;\n" . \ "\t\treturn TRUE;\n" . \ "\t}\n\n"
Am Ende würde das Klassen-Stub also folgendermaßen getypt aussehen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Test { protected $bActive; /** * Get Active. * * @return bool * @author Mathias Lieber */ public function getActive() { return (bool) $this->bActive; } /** * Set Active. * * @param bool $bActive * @return bool * @author Mathias Lieber */ public function setActive($bActive) { $this->bActive = (bool) $bActive; return TRUE; } } |
class Test { protected $bActive; /** * Get Active. * * @return bool * @author Mathias Lieber */ public function getActive() { return (bool) $this->bActive; } /** * Set Active. * * @param bool $bActive * @return bool * @author Mathias Lieber */ public function setActive($bActive) { $this->bActive = (bool) $bActive; return TRUE; } }
Und das finde ich nun richtig gut. Wer mag, benennt noch getActive() in isActive() um. 🙂
Leave a Reply