Skip to content

CodeByZach/sublime_php_getters_setters

 
 

Repository files navigation

PHP Getters and Setters

Latest Release

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

Features:

  • Generate Getters, Setters or Both.
  • Can be applied to all class properties or just to a single one.
  • Description, Type and Type Hinting automatically discovered from the variable docblock.
  • Fully customizable templates.

Usage Instruction:

  1. Generate PHP code
    class test
    {
        /**
         * foo container
         *
         * @var AbcClass
         */
        private $foo;
    }
  1. Go to Tools -> PHP Getters and Setters
  2. Getter and Setter will be generated:
    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
         */
        private function _setFoo(AbcClass $foo)
        {
            $this->foo = $foo;

            return $this;
        }
    }

As you can see, it saves you the trouble of having to comment your variables.

This is an huge time saver!

Usage

Commands available are:

  • Generate Getters and Setters
  • Generate Getter
  • Generate Setter
  • Generate Getter for...
  • Generate Setter for...

These can be accesed via the context menu (right click on the source of any open PHP file) or the command pallette. The currently open file must be a PHP file.

Settings Reference

ignore_visibility
type: boolean
default: false
description: Ignore visibilty for setters generation.

setter_before_getter
type: boolean
default: false
description: Set to true to generate Setters before Getters.

type_hint_ignore (requires restart)
type: list of strings
ignorable types: ["unknown", "self", "array", "callable", "bool", "float", "int", "string", "iterable", "object"]
default: []
description: Should a variable type match any of the following, it will NOT be used for type hinting. (The "unknown" option is a wild-card)

register_templates (requires restart)
type: array
default: []
description: Additional user-defined templates to load.

template (requires restart)
type: string
built-in options: "PSR2", "camelCase", "camelCaseFluent", "snakeCase", "snakeCaseFluent"
default: "PSR2"
description: The selected template.

Creating your own template

[package-dir] is your package directory.

  1. Make a directory called [package-dir]/PHP Getters and Setters.
  2. Put the following in a file at [package-dir]/PHP Getters and Setters/user_templates.py.
class myTemplate(object):
    name = "myTemplate"
    style = 'camelCase' # can also be snakeCase
    getter = """
    /**
    * Gets the %(description)s.
    *
    * @return %(type)s
    */
    public function %(getterPrefix)s%(normalizedName)s()
    {
        return $this->%(name)s;
    }
"""

    setter = """
    /**
    * Sets the %(description)s.
    *
    * @param %(type)s $%(param)s
    *
    * @return self
    */
    %(visibility)s function %(visibilityPrefix)s%(setterPrefix)s%(normalizedName)s(%(typeHint)s $%(param)s)
    {
        $this->%(name)s = $%(param)s;
    }
"""
  1. Edit the parts between getter and setter how you want.
  2. Edit your user settings for this package. On macOS that's Preferences > Package Settings > PHP Getters and Setters > Settings - User.
  3. Add the following settings:
    // Additional user-defined templates to load.
    "register_templates" : [ "myTemplate" ],

    // The selected template.
    "template" : "myTemplate"

About

Generate getters and setters for PHP classes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%