Komodo IDE auto-generating setters/getters for PHP

I’ve got about thirty auto-generated PHP Doctrine models that are missing their required Java style setters/getters. At class number three of typing in these accessory methods I snapped and said “There has to be a better way” and magically the universe smacked me upside the head and reminded me that I’m using Komodo IDE which just so happens to have a disgustingly powerful Python powered macro system.

Four minutes later, out came the copy & paste hacked together monstrosity below. It’s not perfect but it doesn’t need to be, just has to work well enough to save my sanity and my client’s time.

To use, follow Komodo’s help documentation for creating a new python Macro then copy and paste this code into the macro window OR a slightly easier way, make the macro then click the “edit macro” context menu property to open the macro source file as a new view in Komodo.

The macro uses some very simple rules. It’s only looking for private properties in a format of “^\s*private \$[a-zA-Z0-9_]$”, it collects all of these variable names and then appends them through the setter and getter templates to a string buffer. Finally the buffer is
inserted into the current document at the position of the cursor. The output is coherent but not whitespace friendly which isn’t too big of a deal to re-format. Note, the macro has no concept of PHP syntax, so if there is more then one class in a file, the results will not be desirable.

 
from xpcom import components
import re
 
viewSvc = components.classes["@activestate.com/koViewService;1"]\
    .getService(components.interfaces.koIViewService)
view = viewSvc.currentView.queryInterface(components.interfaces.koIScintillaView)
 
sm = view.scimoz
sm.currentPos   # current position in the editor
sm.text         # editor text
sm.selText      # the selected text
#sm.text = "Hello World!"
 
output = u"\n"
 
setterTemplate = """
    function set%s($value){
        $this->%s = $value;
    }
"""
 
getterTemplate = """
    /**
    *@return string
    */
    function get%s(){
        return $this->%s;
    }
"""
 
propertyTemplate = """
%s
 
%s
"""
 
prefixSize = len(u"private $")
 
def formalName(rawName):
    return u"%s" % "".join([part.title() for part in rawName.split("_")])
 
 
 
 
#todo find a better way to split lines, what if its Mac or Windows format?
for line in sm.text.split("\n"):
    if line.strip().startswith("private $"):
        #trim of the private $ and trailing semi-colon
        realName = line.strip()[prefixSize:-1]        
        output += propertyTemplate % ( setterTemplate %(formalName(realName), realName), getterTemplate % (formalName(realName), realName))        
 
 
 
sm.insertText(sm.currentPos, output)