Coda Plugin: PHP Getter/Setter

After being busy busy busy i had some time to write a little plugin for Coda by Panic which i felt missing for those of us writing PHP classes all day long.
Getter/Setter is inspired by the function that is included in Zend Studio for Eclipse and creates public getVariable and setVariable methods from the member variables you select.
Here’s a example class:

class SomeClass extends SomeOtherClass {

public $name = 'SomeClass';

protected $_my_integer = 0;

protected $_my_boolean = false;

protected $_my_constant = THIS_IS_MY_CONST;

private $__someEmptyString = "";

// this is a $test
static public $prop;
}

You simply select a bunch of member variables (like those above) in your class and hit the command CTRL + SHIFT + S (i know, probably there are better ones) to insert the methods.
Example output with just public $name selected:

/**
* Get value of $this->name
*
* @access public
* @return string
*/
public function getName() {
return $this->name;
}

/**
* Set the value of $this->name
*
* @param string $value
* @access public
*/
public function setName($value) {
$this->name = $value;
}

There is some type detection in place for the PHPDoc tags aswell as recognition of static variables.
If i do the same command with protected $_my_integer:

/**
* Get value of $this->_my_integer
*
* @access public
* @return integer
*/

The return type is now “integer” as expected, since that’s the default value for this member variable.
For static variables the assignement is changed from $this to self:: and the methods also get a static flag in front of them.

/**
* Get value of self::$prop
*
* @access public
* @return mixed
*/
static public function getProp() {
return self::$prop;
}

/**
* Set the value of self::$prop
*
* @param mixed $value
* @access public
*/
static public function setProp($value) {
self::$prop = $value;
}

It works with single lines, aswell with multiple of course. Default values that are constants are always typed “mixed” as i find that definiton appropiate. Constants may contain string, bool, int.. so best guess is mixed. Also “null” is treated as “mixed” since it’s .. yeah.. null.
Of course there is some odd behavior which i couldn’t prevent but given the benefit you have these are acceptable.
Known Issues:

  • Code is generated directly after the selected lines. Not at the end of the class or anywhere else fancy. This kinda sucks, but a quick copy paste after insertion will do.. There is no way for me to discover the code level (closing brace, etc.) and the Coda API doesn’t tell either.
  • If you double click a member variable the semicolon is not selected (Coda issue/feature). Since the plugin outputs directly after what has been selected the semicolon is pushed down (after the generated methods). Prevent this by selecting the whole line, or include semicolon.

I think it’s cool anyhow and maybe i get a hang on it and publish some more plugins geared towards CakePHP.
I hope you enjoy this little helper plugin!
DownloadWith Coda installed the plugin should add and activate itself after you double click the package. If not, you can place it in ~/Library/Application Support/Coda/Plug-Ins/. To uninstall just delete the package and restart Coda. If you want to have another shortcut just create one using MacOSX Shortcut Menu in your System Preference Panel.