Code Actions

The editor is provided with so-called Code Actions - Quick Fixes and refactorings for the text currently under the caret. The available code action is annotated with a light bulb next to the text.

Light Bulb in VS Code

Available code actions are listed by clicking on the light bulb or pressing an associated keyboard shortcut (by default) Ctrl+..

Certain errors have also Quick Fix available. This provides the same list of code actions that should resolve the issue.

Quick Fix in VS Code

Getters and Setters

Generating getters and/or setters for private and protected properties can be done using the following code action. This will add the function get and set for the selected property at the end of the current class.

Add getters and/or setters

The inserted code contains all the available type annotations. In case the current version of PHP is 7 or higher, also the return type is added. For PHP 7.4 and higher, the property's type hint is taken into account as well.

Add getters and/or setters

The code action supports adding getter only, setter only, or both. In case more properties are selected, the code action allows to generate getters/setters for all of them at once.

Add getters/setters for more properties

Doc Block Settings

Generated Doc Block above __get/__set is configurable using "php.docblock.getterSetterSnippet" setting.

Disable doc block above getters and setters:

"php.docblock.getterSetterSnippet": false

Custom tags in doc block with placeholders:

"php.docblock.getterSetterSnippet": {
    "summary": false,
    "@author": "${name:John}"
}

Missing abstract functions

Error PHP2414 indicates that the non-abstract class has some functions missing. The quick fix or code action automatically implements the missing abstract functions, including all the available documentation and type information. The generated code is inserted at the end of the class.

Class is missing implementation of abstract functions

The code action resolves the complete class hierarchy, taking into account interfaces and classes. Generated code contains PHPDoc annotations and base implementation of methods if applicable.

Implement missing functions

Import Name

Error PHP0413 indicates that the type does not exist in the current namespace context. If possible, a code action with all available fixes is provided.

Resolve missing namespace

The code action allows you to add use declaration or fully qualify the name in place. Code actions include all the possible names that are found within the current workspace.

Unused use fix

Any use, group of use, or a single alias that is not needed in the code is immediately dimmed.

remove unused `use`

Code action Remove unnecessary 'use' quickly cleanups the block of uses.

Remove and Sort Uses on Save

To automatically organize uses (remove unused ones and sort the rest), open your settings.json, and add one of the following:

"editor.codeActionsOnSave": {
    "source.organizeImports": "explicit",      // sort uses
    "source.sortImports": "explicit",          // sort uses (same as "source.organizeImports"
    "source.sortAndRemoveImports": "explicit", // removes unused uses and sorts them 
}

Specify either "explicit" (the action will be called when file is explicitly saved) or "always" (the action will be called whenever the window gets focus).

Fix typo in PHPDoc keyword

Error PHP6503 suggests that there is a typo in the PHPDoc keyword and offers a quick fix.

Fix common typos in PHPDoc

Add missing $this->

If applicable, a code action suggesting to add $this-> is provided. This fixes a common issue when an instance function is called without specifying the $this object.

Fix missing $this->

See also