### Install Intelephense Server Globally Source: https://github.com/bmewburn/vscode-intelephense/wiki/Installation Install the Intelephense language server globally using npm. Ensure Node.js 20+ is installed. ```bash npm i intelephense -g ``` -------------------------------- ### Index Access Type Example Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Demonstrates using `key-of` and index access types to map container item types based on a generic key. The return type `TMap[TKey]` correctly infers the type of the accessed item. ```php * @param TKey $name * @return TMap[TKey] */ function get($name): mixed {} } $container = new MyContainer(); $item = $container->get('item'); //$item is MyContainerItem ``` -------------------------------- ### PHP Interface and Concrete Class Example Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Illustrates a common scenario where a framework returns an interface, but the actual runtime object is a concrete type with additional methods not declared on the interface, leading to 'undefined method' errors in Intelephense. ```php customViewMethod(); //undefined method 😭 ``` -------------------------------- ### PHP Declared Type Example Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Demonstrates the use of declared types in standard PHP function signatures. This improves performance and quality of language intelligence. ```php function toString(object $obj): string {} ``` -------------------------------- ### PHP Integer Literal Type Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Example of an integer literal used as a type in PHPDoc. ```php 1 ``` -------------------------------- ### PHP Strict Types Directive Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Example of the 'strict_types' directive in PHP. When used, scalar types cannot be assigned to each other unless they are identical. ```php declare(strict_types=1); ``` -------------------------------- ### Configure Document Root for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Set the directory of the application's entry point (e.g., index.php). Defaults to the first workspace folder and is used for resolving script inclusions. ```json { "type": "string", "description": "The directory of the entry point to the application (index.php).Defaults to the first workspace folder. Used for resolving script inclusion.", "scope": "window" } ``` -------------------------------- ### Configure File Associations for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Associates non-standard PHP file extensions with PHP for Intelephense to index them correctly. Include all relevant extensions for your project. ```json { "intelephense.files.associations": ["*.php", "*.phtml", "*.inc", "*.module"] } ``` -------------------------------- ### PHP String Literal Type Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Example of a string literal used as a type in PHPDoc. String literals are enclosed in single quotes. ```php 'myString' ``` -------------------------------- ### Configure PHP Stubs for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Define stub files for built-in PHP symbols and common extensions. The default includes PHP core and bundled extensions. ```json { "intelephense.environment.stubs": [ "Reflection", "session", "shmop", "SimpleXML", "snmp", "soap", "sockets", "sodium", "SPL", "sqlite3", "standard", "superglobals", "sysvmsg", "sysvsem", "sysvshm", "tidy", "tokenizer", "xml", "xmlreader", "xmlrpc", "xmlwriter", "xsl", "Zend OPcache", "zip", "zlib" ], "description": "Configure stub files for built in symbols and common extensions.The default setting includes PHP core and all bundled extensions.", "scope": "window" } ``` -------------------------------- ### Configure Maximum File Size for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Sets the maximum file size in bytes that Intelephense will index. Files larger than this limit will be skipped by default. ```json { "intelephense.files.maxSize": 5000000 } ``` -------------------------------- ### Configure File Associations in VS Code Source: https://github.com/bmewburn/vscode-intelephense/wiki/Installation Add glob patterns for non-standard PHP file extensions to the `files.associations` setting in VS Code. ```json "files.associations": { "*.module": "php" } ``` -------------------------------- ### Run Intelephense Server with Transport Source: https://github.com/bmewburn/vscode-intelephense/wiki/Installation Execute the Intelephense server, specifying the desired transport protocol. Supported transports include `--node-ipc`, `--stdio`, `--socket={number}`, and `--pipe={string}`. ```bash intelephense {transport} ``` -------------------------------- ### Configure Short Open Tag for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Enable or disable the parsing of '', ':', '\\', '/', '\'', '"', //phpdoc '*', // html/js '.', '<' ], resolveProvider: true }, signatureHelpProvider: { triggerCharacters: ['(', ',', ':'] }, definitionProvider: true, referencesProvider: true, hoverProvider: true, documentFormattingProvider: true, //Dynamic registration if available. documentRangeFormattingProvider: true, //Dynamic registration if available. documentHighlightProvider: true, workspace: { workspaceFolders: { supported: true, changeNotifications: true } }, foldingRangeProvider: true, //With licence key only. implementationProvider: true, //With licence key only. declarationProvider: true, //With licence key only. renameProvider: { prepareProvider: true }, //With licence key only. typeDefinitionProvider: true, //With licence key only. selectionRangeProvider: true, //With licence key only. codeActionProvider: true, //With licence key only. executeCommandProvider: { commands: ['intelephense.import.symbol', 'intelephense.implement.abstract.method.all', 'intelephense.phpdoc.add'] }, //With licence key only. typeHierarchyProvider: true, //With licence key only. codeLensProvider: {resolveProvider: true} //With licence key only. } ``` -------------------------------- ### PHP Function with Type Declarations and PHPDoc Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Demonstrates providing type information using both PHPDoc annotations and native PHP type declarations. PHPDoc annotations are given precedence for richer type information. ```php `ArrayAccessOrTraversable`.", "scope": "window" }, "intelephense.compatibility.preferPsalmPhpstanPrefixedAnnotations": { "type": "boolean", "default": false, "description": "Prefer `@psalm-` and `@phpstan-` prefixed `@return`, `@var`, `@param` tags when determining symbol types.", "scope": "window" }, "intelephense.files.maxSize": { "type": "number", "default": 1000000, "description": "Maximum file size in bytes.", "scope": "window" }, "intelephense.files.associations": { "type": "array", "default": [ "*.php", "*.phtml" ], "description": "Configure glob patterns to make files available for language server features. Inherits from files.associations.", "scope": "window" }, "intelephense.files.exclude": { "type": "array", "items": { "type": "string" }, "default": [ "**/.git/**", "**/.svn/**", "**/.hg/**", "**/CVS/**", "**/.DS_Store/**", "**/node_modules/**", "**/bower_components/**", "**/vendor/**/{Tests,tests}/**", "**/.history/**", "**/vendor/**/vendor/**" ], "description": "Configure glob patterns to exclude certain files and folders from all language server features. Inherits from files.exclude.", "scope": "resource" }, "intelephense.stubs": { "type": "array", "items": { "type": "string", "enum": [ "aerospike", "amqp", "apache", "apcu", "ast", "bcmath", "blackfire", "bz2", "calendar", "cassandra", "com_dotnet", "Core", "couchbase", "couchbase_v2", "crypto", "ctype", "cubrid", "curl", "date", "dba", "decimal", "dio", "dom", "ds", "eio", "elastic_apm", "enchant", "Ev", "event", "exif", "expect", "fann", "FFI", "ffmpeg", "fileinfo", "filter", "fpm", "ftp", "gd", "gearman", "geoip", "geos", "gettext", "gmagick", "gmp", "gnupg", "grpc", "hash", "http", "ibm_db2", "iconv", "igbinary", "imagick", "imap", "inotify", "interbase", "intl", "json", "judy", "ldap", "leveldb", "libevent", "libsodium", "libvirt-php", "libxml", "lua", "LuaSandbox", "lzf", "mailparse", "mapscript", "mbstring", "mcrypt", "memcache", "memcached", "meminfo", "meta", "ming", "mongo", "mongodb", "mosquitto-php", "mqseries", "msgpack", "mssql", "mysql", "mysql_xdevapi", "mysqli", "ncurses", "newrelic", "oauth", "oci8", "odbc", "openssl", "opentelemetry", "pam" ] }, "description": "Configure stubs to enable language server features for built-in PHP functions and extensions.", "scope": "window" } } ``` -------------------------------- ### Configure Files to Exclude from Intelephense Indexing Source: https://github.com/bmewburn/vscode-intelephense/wiki/Getting-Started Specifies patterns for files and directories that should be excluded from Intelephense indexing. This helps to avoid polluting suggestion lists and degrading performance in large projects. ```json { "intelephense.files.exclude": [ "**/.git/**", "**/.svn/**", "**/.hg/**", "**/CVS/**", "**/.DS_Store/**", "**/node_modules/**", "**/bower_components/**", "**/vendor/**/{Tests,tests}/**", "**/.history/**", "**/vendor/**/vendor/**" ] } ``` -------------------------------- ### PHP Helper File for Intelephense Type Override Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Declares a modified function signature in a non-executable helper file to override the default return type for Intelephense, improving type analysis without changing project code. ```php //Declare a different signature for the view function. One that declares the concrete return type. function view(): CustomView {} ``` -------------------------------- ### PHP Fully Qualified Class Type Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Demonstrates using a fully qualified class name as a type hint in PHPDoc. Standard PHP name resolution rules apply if not fully qualified. ```php \MyNs\MyClass ``` -------------------------------- ### PHP Interface Modification for Intelephense Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Adds a method declaration to an interface in a non-executable helper file to satisfy Intelephense's analysis when the concrete implementation has additional methods. ```php //Or add the undefined method to the interface instead. interface View { function customViewMethod(); } ``` -------------------------------- ### Array and Object Shapes via Annotations Source: https://github.com/bmewburn/vscode-intelephense/blob/master/CHANGELOG.md Use annotations to define array and object shapes for function parameters and return types. This improves type safety and code clarity. ```php /** @param array{foo:string, bar:int} $arrayShape */ ``` ```php /** @return object{foo:string, bar:int} */ ``` -------------------------------- ### PHP Type Narrowing with Variable Assignment Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Workaround for Intelephense issues by assigning the return value of a function to a variable and explicitly checking its type, allowing for method calls on the concrete type. ```php customViewMethod(); ``` -------------------------------- ### Intelephense No Mixed Type Check Configuration Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Configuration option to disable the behavior where 'mixed' can be assigned to any other type constraint. Set to 'false' for stricter 'mixed' type checking. ```json "intelephense.diagnostics.noMixedTypeCheck": false ``` -------------------------------- ### PHP Type Narrowing with PHPDoc Annotation Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Uses a PHPDoc annotation to inform Intelephense about the expected concrete type of a variable, enabling correct method suggestions without altering code execution. ```php //Or with an annotation. //This won't alter the execution of the code but still involves modifying the executable code. /** @var CustomView $view */ $view = view(); $view->customViewMethod(); ``` -------------------------------- ### Intelephense Relaxed Type Check Configuration Source: https://github.com/bmewburn/vscode-intelephense/wiki/Type-System Configuration option to disable the default behavior where supertypes can be assigned to subtype constraints. Set to 'false' for more robust type checking. ```json "intelephense.diagnostics.relaxedTypeCheck": false ``` -------------------------------- ### PHP Custom Helper Function for Type Narrowing Source: https://github.com/bmewburn/vscode-intelephense/wiki/PHP-frameworks-and-libraries Defines a custom helper function that calls the original function and asserts the return type, providing a narrowed type for subsequent method calls. ```php //A custom helper could also be created and called instead to narrow the type function customView(): CustomView { $view = view(); assert($view instanceof CustomView); return $view; } customView()->customViewMethod(); ```