Documentation
Getting Started
About
Intelephense is a high performance, cross platform, cross editor PHP language server adhering to the Language Server Protocol (LSP).
When paired with an LSP capable editor it provides an essential set of code tools, making for a productive and rich PHP coding experience.
The Intelephense server is proprietary software released to end users under a "freemium" model. Many of the features are provided free of charge. Access to premium features can be obtained by purchasing a licence key.
Installation
Visual Studio Code
Visual Studio Code users should install the Intelephense extension from within the extensions view or download it from the VSCode marketplace.
The built-in VSCode PHP Language Features extension can cause excessive completion suggestions that are out of context and is best disabled. Go to the Extensions UI and search for PHP Language Features to disable it. Alternatively, you can disable parts of it via it's configuration settings. Other third party extensions that provide similar functionality to Intelephense may also need to be disabled for best results.
Optionally purchase and enter your licence key by opening the command palette (Ctrl+Shift+P) and searching for Enter licence key.
Other Editors
Intelephense requires a Node.js runtime environment. It is recommended that you use a current LTS version of Node.js. To install Intelephense server you can use npm.
npm i intelephense -g
Intelephense needs an LSP compliant client to communicate with and integrate features into the editor. A list of editors and clients that support the LSP can be found here. Please follow the setup guide of the relevant tool. The information below may help in configuring the client.
To start the intelephense server:
intelephense {transport}
Where {transport} is one of:
--node-ipc--stdio--socket={number}--pipe={string}
If your LSP client exposes initializationOptions, then the following values are accepted:
interface InitialisationOptions {
// Optional absolute path to storage directory for workspace specific data.
storagePath?: string;
// Optional absolute path to a global storage directory for global data.
globalStoragePath?: string;
//Optional licence key or absolute path to a text file containing the licence key.
licenceKey?: string;
//Optional flag to clear server state.
//State can also be cleared by deleting {storagePath}/intelephense
clearCache?: boolean;
}
When initializationOptions properties are not provided by the client, the following defaults are used:
| OS | Property | Path | Fallback |
|---|---|---|---|
| *nix | storagePath |
$XDG_CONFIG_HOME/intelephense/workspace/ |
$HOME/.config/intelephense/workspace/ |
| *nix | globalStoragePath |
$XDG_CONFIG_HOME/intelephense/global/ |
$HOME/.config/intelephense/global/ |
| *nix | licenceKey |
{globalStoragePath}/licence.txt |
{globalStoragePath}/license.txt |
| Windows | storagePath |
%AppData%/intelephense/workspace/ |
%UserProfile%/intelephense/workspace/ |
| Windows | globalStoragePath |
%AppData%/intelephense/global/ |
%UserProfile%/intelephense/global/ |
| Windows | licenceKey |
{globalStoragePath}/licence.txt |
{globalStoragePath}/license.txt |
If your LSP client does not expose initializationOptions then a licence key can
be provided by placing (only) the key in a text file at the default licenceKey path listed above.
Configuration
Please see the VSCode client package.json
configuration property for a full list of configuration options and associated JSON schema.
Note that the configuration keys are given in dot notation.
As an example, the equivalent JSON object for intelephense.files.exclude would be {"intelephense": {"files": {"exclude": []}}}.
Intelephense attempts to provide reasonable defaults for all settings. Some of the more important settings to consider when getting started include:
intelephense.files.associations- File globs that identify PHP files. Defaults to standard PHP file extensions e.g.*.php.intelephense.files.maxSize- Maximum file size in bytes to index and provide analysis for. Defaults to1000000(1MB).intelephense.environment.phpVersion- PHP version to use for analysis. Defaults to the most recent stable PHP version.intelephense.stubs- List of stubs to include. Defaults to core symbols and extensions that are bundled with PHP. If you are getting undefined symbols for built-in or PECL extensions, you may need to modify this list.
In VSCode, the settings UI can be used to modify the configuration values.
For other LSP clients, please see the client documentation on how to modify these values.
Intelephense supports the LSP
workspace/didChangeConfiguration and
workspace/configuration
methods as a way of supplying configuration values to the server.
If neither of the methods above are supported by the client, then configuration values can be supplied via an intelephense.config.json file placed in the workspace folder.
The JSON schema for this file is the same as the one used for the VSCode client. The top level intelephense property is not required in this file.
For Intelephense to work effectively it must have access to the definitions of the symbols used in your code.
Opening a project folder (LSP InitializeParams rootUri or workspaceFolders) rather than individual files enables these symbols to be discovered by Intelephense via indexing the PHP files in the folder.
Large workspaces require more system resources.
Consider opening a smaller workspace or exclude unnecessary files via the intelephense.files.exclude setting to reduce resource usage.
If you need to include files from outside of the workspace folder, then add the paths to these files to the intelephense.environment.includePaths setting.
When configuring a multi-root workspace, Intelephense will presume that the folders in the workspace are separate projects
and will not provide cross folder symbols unless you link the dependency between the projects via the intelephense.environment.includePaths setting.
Depending on the framework or library you use, you may find you need additional configuration to provide method declarations or override existing ones. Please see the Frameworks and Libraries section in the appendix for more information on this.
Type System
Providing type information in your PHP code will result in a better experience when using Intelephense. Type information can be provided via coded type declarations or PHPDoc type annotations. Where both have been provided, PHPDoc type annotations are given precedence as they can provide more detailed type information.
<?php
/**
* @param string $s <- A phpdoc parameter type annotation for $s
* @return string[] <- A phpdoc return type annotation specifying the array element type
**/
function foo(string $s): array {} // <- type declarations for $s (string) and function return (array)
Intelephense will also compute inferred types when a declared or documented type is not found or during control flow analysis.
When a type is inferred it may be reduced to it's minimal representation.
For example, MyClass|object would become object because MyClass is a sub-type of object.
Intelephense provides limited support for PHPStorm metadata as a way of overriding or supplementing type information. It is recommended to use PHPDoc type annotations instead of PHPStorm metadata where possible as they are more widely supported across different tools. Support for PHPStorm metadata may be removed in future releases. Please see the PHPDoc Instead of PHPStorm Metadata/Attributes section in the appendix for more information.
Type Narrowing
Intelephense performs type narrowing of variables during control flow analysis.
Type narrowing expressions include built-in type assertions such as is_string, custom type assertions annotated with @assert,
instanceof, and equality expressions.
The example below demonstrates type narrowing.
<?php
class Foo {}
function example(string|array|Foo|null $input): void
{
if (!$input) {
// $input is narrowed to string|array|null in this block
// empty strings, empty arrays and null are all falsey
} else {
// $input is narrowed to string|array|Foo in this block
if ($input instanceof Foo) {
// $input is narrowed to Foo in this block
} else if (is_string($input)) {
// $input is narrowed to string in this block
} else {
// $input is narrowed to array in this block
}
}
}
Type Evolving
Type evolving is the change in a variable's type after an assignment expression. Simple variables and parameters always change to the type of the assigned expression regardless of initial assignments, type declarations or annotations.
Properties with no type declaration or annotation will also change to the type of the assigned expression. Otherwise they will only widen or narrow according to the bounds of the initial type they have been declared or annotated with.
Intelephense will type evolve array types when mutated only if they are declared with an empty array initialiser. Otherwise they are considered to retain their initial declared, annotated or inferred type. The example below demonstrates type evolving.
<?php
function example(int $a): void
{
$a = "string"; // $a is now type string
$b = []; // $b is type array and flagged as evolving
$b[] = "string"; // $b is now type string[]
$b[] = 9; //$b is now (string|int)[]
$c = [1, 2]; // $c is type int[] and NOT flagged as evolving
$c[] = "string"; // $c is still type int[]
}
Supported Types
In the list of supported types below, some can only be used in PHPDoc as documented types. Please see the PHP type system documentation if you are unfamiliar with the standard PHP types. PHPDoc only, or internal types, are flagged with an asterisk.
Additional types used in other static analysis engines that are not listed here are not fully supported. Intelephense attempts to fallback to an appropriate alternative in this situation.
Top Type
mixed
The super-type of all types.
Any other type can be assigned to a type constraint of mixed.
If intelephense cannot determine a more specific type for a symbol or expression then this is the type it is given.
Because of this, Intelephense also allows mixed to be assigned to any other type constraint as well,
effectively turning off type checking for that instance.
To switch off this behaviour you can set both intelephense.diagnostics.relaxedTypeCheck and
intelephense.diagnostics.noMixedTypeCheck to false.
Bottom Type
never
The sub-type of all types. This type can be assigned to any other type constraint. It is used to represent an impossibility in the code and can be used as the return type of a function that exits or always throws an exception.
Scalar Types
Any of these types can be assigned to the other unless the declare(strict_types=1) directive is used in the file or intelephense.diagnostics.strictTypes is true.
intfloatboolstring
Unit Types
voidnulltruefalseunset