Skip to content

automatically generate _Getters_ and _Setters_ for your php classes in the atom.io editor

License

Notifications You must be signed in to change notification settings

francodacosta/atom-php-getters-setters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Getters and Setters

With PHP Getters and Setters you can automatically generate Getters and Setters for your php classes.

The code produced is PSR compatible

Features:

  • Generate Getters, Setters or Both
  • Select all variables or just some via UI
  • Control method scope via a DocBlock tag
  • intelligent guessing of variable names, if you use descriptive variable names you do not need to provide a description, the method comment will set accordingly
  • supports _ in property names

Example PHP Code

class test
{
    /**
     * foo container
     *
     * @var AbcClass
     */
    private $foo;
}

Example class after generating Getters and Setters

class test
{
    /**
     * foo container
     *
     * @var AbcClass
     */
    private $foo;

    /**
     * Gets the foo container.
     *
     * @return AbcClass
     */
    public function getFoo()
    {
        return $this->foo;
    }

    /**
     * Sets the foo container.
     *
     * @param AbcClass $foo the foo
     *
     * @return self
     */
    public function setFoo(AbcClass $foo)
    {
        $this->foo = $foo;

        return $this;
    }
}

As you can see if get to trouble of commenting your variables, the generated functions can be used without modification.

This is an huge time saver!

Special DocBlock tags

@internal: getter and setter will be private

@private: getter and setter will be private

@protected: getter and setter will be protected

@read-only private|protected: getter will be public, setter will be private or protected (defaults to private)

Settings:

doNotTypeHint: an array of items that when present in @type or @var declarations are ignored and not used as type hint

camelCasedMethodNames: method names will follow PSR rules PSR states that all method names must be camel cased, if set to false method names won't be Camel Cased

getterTemplate: the template for the getter

setterTemplate: the template for the setter

Default templates

A rudimentary template editor is available at Packages -> PHP Getters and Setters -> Template Editor

Getter

\ \ \ \ /**\n
\ \ \ \ * Get the value of %description% \n
\ \ \ \ * \n
\ \ \ \ * @return %type%\n
\ \ \ \ */\n
\ \ \ %scope% function %methodName%()\n
\ \ \ {\n
\ \ \ \ \ \ \ return $this->%variable%;\n
\ \ \ }\n
\n

Setter

\ \ \ \ /** \n
\ \ \ \ * Set the value of %description% \n
\ \ \ \ * \n
\ \ \ \ * @param %type% %variable%\n
\ \ \ \ * \n
\ \ \ \ * @return self\n
\ \ \ \ */\n
\ \ \ %scope% function %methodName%(%typeHint%$%variable%)\n
\ \ \ {\n
\ \ \ \ \ \ \ $this->%variable% = $%variable%;\n
\n
\ \ \ \ \ \ \ return $this;\n
\ \ \ }\n
\n