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 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, 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:
TLDR; the full settings file is on GitHub.
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.
{
"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 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.
"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
"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
"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.
"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.
Top comments (4)
I love your presentation as you said 'Optional Settings' because I don't like the cursor blinking and I love the font ligatures. It's my taste, and I don't want anyone to tell me strictly to do this. It's my taste 😉
If you are a macOS user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!
Hi, yes it works with Composer packages. This is one of my favorite features since I believe code is the best form of documentation.
dockbloker link is broken