The best setting.json for PHP development with Visual Studio Code
Tags: php, vscode, webdev
Published: 2019-12-30 02:24:21

## Overview

PHP is a dynamic object-oriented language that brings the developer the power of OOP along with the flexibility of dynamic typing. [Type annotations](https://www.php.net/manual/en/language.oop5.typehinting.php) are supported in PHP which allows you the ability to define the runtime type of function parameters. 

IDE’s can be used to increase one’s productivity since most of them have built-in auto-completion and other useful features such as “Go to definition”, and “Find all references”. 
One popular IDE is [PHPStorm](https://www.jetbrains.com/phpstorm/), but today we will highlight which Visual Studio Code extensions transform Visual Studio Code into a full-fledged IDE alternative for PHP.

With great power, comes great responsibility, and because Visual Studio Code is a powerful text editor that brings an extremely powerful extensibility API, it’s easy to find yourself with a nonsensical settings.json file. At the end of this post, your visual studio code will have these productivity features:

- Errors, warnings, and other introspection such as unused variables.
- Code completion
- Function/Method signature parameter hinting.
- Go to definition
- DotEnv syntax highlighting
- Find all references
- Documentation when hovering over methods
- Path autocompletion, for string literal file paths.
- PSR code sniffing emitting errors, and warnings in real time.
- Automatic PHP DocBlocks.

These features will be implemented by installing these extensions:
- [bmewburn.vscode-intelephense-client](https://github.com/bmewburn/vscode-intelephense)
- [mikestead.dotenv](https://marketplace.visualstudio.com/items?itemName=mikestead.dotenv)
- [ikappas.phpcs](https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs)
- [neilbrayfield.php-docblocker](https://marketplace.visualstudio.com/items?itemName=neilbrayfield.php-docblocker)


TLDR; [the full settings file is on GitHub.](https://github.com/amezmo/vscode-settings)

##Step 1.

The first thing we need to do is disable default PHP support. This can be accomplished with two configuration items in your .vscode/settings.json file. The snippets in this post are extracted from [the full file](https://github.com/amezmo/vscode-settings/blob/master/.vscode/settings.json). 


```javascript
{
 "php.validate.enable": false,
 "php.suggest.basic": false,
}
```

This will allow our extensions to be used to their fullest extent and prevent any functional collisions.

##Step 2.

By default, the php-redis extension is not included in the default configuration. So that means you will not have auto-complete for the [Redis](https://github.com/phpredis/phpredis) class. Now, let’s configure optimal Intelephense settings. Quick tip, when you’re editing a setting.json file in vscode, you can see the default value for the configuration item by typing the colon character, : and then clicking the tooltip. 

```javascript
"intelephense.stubs": [
    "apache",
    "redis",
    "bcmath",
    "bz2",
    "calendar",
    "com_dotnet",
    "Core",
    "ctype",
    "curl",
    "date",
    "dba",
    "dom",
    "enchant",
    "exif",
    "fileinfo",
    "filter",
    "fpm",
    "ftp",
    "gd",
    "hash",
    "iconv",
    "imap",
    "interbase",
    "intl",
    "json",
    "ldap",
    "libxml",
    "mbstring",
    "mcrypt",
    "meta",
    "mssql",
    "mysqli",
    "oci8",
    "odbc",
    "openssl",
    "pcntl",
    "pcre",
    "PDO",
    "pdo_ibm",
    "pdo_mysql",
    "pdo_pgsql",
    "pdo_sqlite",
    "pgsql",
    "Phar",
    "posix",
    "pspell",
    "readline",
    "recode",
    "Reflection",
    "regex",
    "session",
    "shmop",
    "SimpleXML",
    "snmp",
    "soap",
    "sockets",
    "sodium",
    "SPL",
    "sqlite3",
    "standard",
    "superglobals",
    "sybase",
    "sysvmsg",
    "sysvsem",
    "sysvshm",
    "tidy",
    "tokenizer",
    "wddx",
    "xml",
    "xmlreader",
    "xmlrpc",
    "xmlwriter",
    "Zend OPcache",
    "zip",
    "zlib"
],
"intelephense.telemetry.enabled": false,
"intelephense.format.enable": true,
"intelephense.completion.triggerParameterHints": true,
"intelephense.completion.insertUseDeclaration": true,
"intelephense.completion.fullyQualifyGlobalConstantsAndFunctions": true,
"intelephense.trace.server": "messages",
"intelephense.files.exclude": [
    "**/.git/**",
    "**/.svn/**",
    "**/.hg/**",
    "**/CVS/**",
    "**/.DS_Store/**",
    "**/node_modules/**",
    "**/bower_components/**",
    "**/storage",
    "**/storage/**",
    "**/tests/**",
    "**/resources/views/**",
    "**/database/migrations/**",
    "**/storage/framework/views/**",
    "_ide_helper.php",
    "_ide_helper_models"
]
```


## Step 3 PHP Code Sniffer settings

```javascript
"phpcs.executablePath": "vendor/bin/phpcs",
"phpcs.enable": true,
"phpcs.autoConfigSearch": true,
"phpcs.standard": "./phpcs.xml",
"phpcs.showWarnings": true,
"phpcs.showSources": true,
"phpcs.ignorePatterns": [
    "**/.git/**",
    "**/.svn/**",
    "**/.hg/**",
    "**/CVS/**",
    "**/.DS_Store/**",
    "**/node_modules/**",
    "**/bower_components/**",
    "**/storage/**",
    "**/resources/views/**",
    "**/database/migrations/**",
    "**/tests/**",
    "**/storage/framework/views/**",
    "**/vendor",
    "_ide_helper.php",
    "_ide_helper_models"
]
```

## Step 4. PHPDoc Block settings

```javascript
"php-docblocker.returnGap": true,
"php-docblocker.qualifyClassNames": true,
"php-docblocker.author": {
    "name": "Ryan McCullagh", 
    "email": "ryan@amezmo.com"
}
```


##Step 5. Optional settings.

These settings are more of a more personal flavor, but I’ve shared them here because I believe they are amazing.

```javascript
"search.collapseResults": "alwaysCollapse",
"search.exclude": {
    "**/node_modules": true,
    "**/bower_components": true,
    "**/.git": true,
    "**/storage": true,
    "**/tests": true,
    "_ide_helper.php": true,
    "_ide_helper_models.php": true,
    "package-lock.json": true
},
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"files.associations": {
    "*.blade.php": "html",
    "*.php": "php",
    "*.vue": "vue",
    "*.php-cs": "php"
},
"editor.fontSize": 14,
"editor.letterSpacing": 0.5,
"editor.lineHeight": 24,
"editor.cursorBlinking": "smooth",
"editor.fontLigatures": false,
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.autoIndent": "advanced",
"git.showPushSuccessNotification": true,
"editor.gotoLocation.multipleDefinitions": "goto",
"editor.snippetSuggestions": "bottom",
"workbench.editor.enablePreview": false,
"workbench.editor.enablePreviewFromQuickOpen": false,
```

The last 2 are great because the editor will stay open when you’ve navigated to a file from a find all reference, or go to definition click.