Code Completion

The code completion (or word completion) is feature that predicts what can be typed at specific place at specific context. Usually it displays a list of suggestions, pre-selects the best one and automatically completes the word if possible.

The feature learns from user's code, integrates PHP manual, performs type analysis and provides suggestions based on all this information.

Code Completion

Triggering Completion

The completion list is triggered automatically as user types, or can be forced by a keyboard shortcut. The list is popuped after several triggering tokens, if completion list is applicable:

  • when user types first letter of a word
  • after ->, \, ::, $
  • after use, extends, implements, global, instanceof, new, function
  • after @ within PHPDoc

Once the list is triggered, subsequent typing filters the list to closest matches. The list can be closed with Esc key.

The completion list can be opened again with keyboard shortcut Ctrl+Space or Alt+Right Arrow. If there is only single unique suggestion, it is inserted into the text immediately without showing the completion list.

Completing Variable Names

Since PHP does not have a variable declaration statement, PHP Editor cannot complete variables automatically as user types. Otherwise it would cause frequent false completions when user wants to type new variable name.

This is handled by the PHP Editor in several ways. This behavior can be changed in Tools | Options | Text Editor | PHP | IntelliSense, by ticking the option Pre-select variables if '$' is typed.

IntelliSense Member Selection Options

  • If typed word starts with $, it is treated as a new variable, and completion list does not pre-select any member.
  • If typed word does not start with $, and the word matches a variable from the list, the variable is pre-selected. If there are better items matching the word, variable is prioritized if its name fully matches the typed word.

Completing Object Members

After -> code completion suggests available object members. This features works only if type of the expression before -> is correctly determined by the editor.

Completing Type Names

Possible type names (classes, interfaces and traits) are listed in the completion list as user types. In case the caret is after a context keyword like new, extends, implements, instanceof and others, only meaningful types are suggested.

Listed names are filtered for current namespace context. When the suggested type name consists of a namespace part and a name part (e.g. A\B\C), its parts are suggested subsequently after typing \ character.

In case user writes a name of a type that is not visible in the current namespace context, the editor marks such type with an exclamation mark.

IntelliSense Invisible Types

If such inaccessible type is used in code, the editor provides additional suggestions that automatically inserts use or expands the name to a fully qualified name or expands the name to its shortest form using existing aliases. For more information about suggestions see the documentation page.

Suggestions for type

Improving Completion

Code completion is heavily based on type analysis and user's behavior. To improve code completion after ->, make sure the editor can resolve type of the expression before. You can check the type of variables at specific place by hovering mouse over it and checking its tooltip. If the type is not resolved by editor correctly, or displays only mixed or object, additional PHPDoc comments may be provided to improve the completion.

PHPDoc can be placed above functions to provide type hints for function parameters (using @param tag) and function return type (using @return tag). Additionally you can write PHPDoc blocks right in the code specifying variable types (e.g. /** @var MyClass $x */).

Completing Array Keys

Note: Array key IntelliSense is not supported yet.

Committing Code Completion

Once the completion list is opened, there are specific characters that commits the completion from the actively selected item in the list. In addition to characters specified in menu Tools | Options | Text Editor | PHP | IntelliSense, there are following commit keys:

  • Enter
  • Space
  • \, [, ( and ; commit the selected item and inserts the character.

Note: Right after a keyword supporting snippets (e.g. for, class, if or try), Tab key inserts whole code snippet corresponding to the keyword.

Recent Member Select

The completion list remembers recently commited items in specific context. By learing from this recent usages of completion list, next time the completion is triggered, best matching recently used item is pre-selected.

The feature can be disabled in menu Tools | Options | Text Editor | PHP | IntelliSense.