Setting up Visual Studio Code to work with PHP
VS code is a tool mostly used by front-end developers. It’s highly optimized to work with JavaScript and TypeScript, featuring smart autocompletion, auto-imports, refactors, and other goodies. Although no match to IDEs like PHPStorm, Visual Studio Code can be customized to have better support when working PHP.
The most important thing you can do
- Within Visual Studio Code, go to the “Extensions” tab and search PHP.
- Uninstall PHP IntelliSense (it will be installed by default)
- Install PHP Intelephense.
Note: I’m not being payed by intelephense to write this!
You will want to disable the default PHP suggestions integrated into VS code. To acheive this, add this line to your user settings:
"php.suggest.basic": false,
(To open your settings, open the console with Cmd+Shift+P and search the key “Preferences: Open user settings (JSON)”)
After changing this, your PHP code will start behaving a little big “smarter”.
If you’re done this, you have it. 90% of the functions you wanted from PHPStorm are already in VS code.
Here are some of those features you now have:
- Highlight syntax, type errors and other static analysis tools
- Autodetect installed libraries and bring them in suggestions based on your composer.json
- Detect unused imports, unused private functions or unused variables.
- Automatically creates phpdoc annotations with configurable format, and reads them when writting method or function calls.
Some other extensions you might want to install
Here are some other extensions that might be handy to cover that missing 10% of features that are not bundled with Intelephense.
- PHP Dockblocker enhances the capabilities of Intelephense regarding the creation of phpdoc type comments.
- PHP Namespace Resolver Allows to automatically import a class using Ctrl+Opt+I and automatically expand the class (a.k.a.: import it inline, to get \Path\To\Class ) using Ctrl+Opt+E. If there are name conflicts, a prompt shows all the options.
- laravel-blade is an extension to highlight syntax in Blade files. Laravel goto view allows to cmd+click in @import()s and other blade tags.
Extensions for speficic tasks
There are a lot of extensions to add some little extra functionalities you might need. Like this extension to add getter and setter methods, and this one or this one to generate constructor properties.
Third party integrations
Integrations with Docker and various SQL database flavours are also available as plug-ins. A personal favourite is GitLens, that complements the Git support of the editor with some cool, advanced functions (at the expense of a little performance loss).
Conclusion
There are a lot more extensions in the market and a million more posts that curate them, so I will leave this one kinda open and just drop a link to the VS code marketplace for you to take a look .
VS code is not an IDE, but a code editor. Yet, by using extensions we can make it look and behave kind of like an IDE. It’s no match against PHPStorm in it’s advanced features, but it can provide the most used features with some simple customizations.
Visual Studio Code
Visual Studio Code (VSCode) is a source code editor developed by Microsoft for Windows, Linux and MacOS (including M1 Macs). It includes support for debugging, embedded Git control and GitHub, syntax highlighting, intelligent code completion, snippets, and code refactoring.
The main characteristic of the editor is that it is extensible and it has a huge collection of extensions maintained by users and by Microsoft itself. See details at the VSCode Extensions for Visual Studio Code website.
As a general introduction, this is a presentation about the topic Joomla Development with Visual Studio Code, by René Kreijveld, Joomla developer Destiny B.V. Joomla User Group London, 16th march 2021.
On this page, we detail a list of recommended extensions to configure VSCode to support PHP and Joomla.
This is a list of extensions to support PHP on VSCode:
- PHP Debugfelixfbecker.php-debug. Debug support for PHP with Xdebug.
- phpcsikappas.phpcs. PHP CodeSniffer for Visual Studio Code.
- PHP Intelephensebmewburn.vscode-intelephense-client. PHP code intelligence for Visual Studio Code. Alternative: PHP IntelliSensefelixfbecker.php-intellisense
- PHPUnitemallin.phpunit. Run PHPUnit tests from VSCode.
- phpfmtkokororin.vscode-phpfmt. The missing phpfmt extension for Visual Studio Code. The phpfmt formatter can parse and format even a PHP file version 4 if needed.
- php cs fixerjunstyle.php-cs-fixer: In case of opting for the PSR-2: Coding Style Guide). PHP CS Fixer extension for VS Code, PHP formatter, PHP code beautify tool, format HTML.
- PHP Namespace ResolverMehediDracula.php-namespace-resolver: PHP Namespace Resolver can import and expand your class. You can also sort your imported classes by line length or in alphabetical order.
- PHP Phan (Analyzer)tysonandre.php-phan. Phan — static analyzer for PHP, minimizing false positives.
- phpmdlinyang95.phpmd. VS Code extension for PHP, using PHPMD.
The Git protocol is natively supported on VSCode. However, there are extensions to improve code and repository management. This is a list of featured extensions for this task:
- Git Historydonjayamanne.githistory: Git History, Search and More (including git log).
- GitHubKnisterPeter.vscode-github: This VSCode extension integrates with GitHubExtensions for Visual Studio Code
- GitHub Pull Requests and IssuesGitHub.vscode-pull-request-github: Review and manage your GitHub pull requests and issues directly in VS Code
- gitignorecodezombiech.gitignore: A extension for Visual Studio Code that assists you in working with .gitignore files.
- Visual Studio Code Commitizen SupportKnisterPeter.vscode-commitizen: This VSCode extension adds commitizen support.
This is a list of extensions to support Joomla on VSCode:
- Joomla Snippetsanibalsanchez.vs-code-joomla-snippets. Snippets for Joomla including Joomla 3.x and Joomla 4 Snippets.
- PHP Getters & Settersphproberto.vscode-php-getters-setters. Create PHP getters and setters from class properties.
This is a list of extensions to support remote SSH development:
- Remote — SSHms-vscode-remote.remote-ssh: The Remote — SSH extension lets you use any remote machine with an SSH server as your development environment. This can greatly simplify development and troubleshooting in a wide variety of situations. For more information: Remote development over SSH
- Live ShareMS-vsliveshare.vsliveshare: Visual Studio Live Share enables you to collaboratively edit and debug with others in real-time, regardless of what programming languages you’re using or app types you’re building. It allows you to instantly (and securely) share your current project, and then as needed, share debugging sessions, terminal instances, localhost web apps, voice calls, and more!
This is a list of handy extensions highly recommended to ease the development:
- EditorConfig for VS Codeeditorconfig.editorconfig EditorConfig Support for Visual Studio Code.
- change-casewmaurer.change-case: Quickly change the case (camelCase, CONSTANT_CASE, snake_case, etc) of the current selection or current word.
- Project Manageralefragnani.project-manager: It helps you to easily access your projects, no matter where they are located. Don’t miss those important projects anymore.
- 1 PHP Debug Configuration
- 1.1 Xdebug 3 Configuration
PHP Debug Configuration [ править ]
The extension supports the most common configurations of PHP Xdebug. It fully integrates all VSCode features to debug PHP scripts.
To configure the extension, visit the official documentation.
As a sample configuration for VSCode debugging, this is a common launch.json:
< // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations "version": "0.2.0", "configurations": [ < "name": "Listen for Xdebug", "type": "php", "request": "launch", // Server Remote Xdebug Port - 9003 is the default Xdebug port "port": 9003, // Server Remote Path ->Local Project Path "pathMappings": < "/app/www": "$/www" > >, < "name": "Launch currently open script", "type": "php", "request": "launch", "program": "$", "cwd": "$", // Local Xdebug Port - 9003 is the default Xdebug port "port": 9003 > ] >
Related to the launch.json configuration that felixfbecker/vscode-php-debug requires, it is the configuration of PHP Xdebug itself.
Your development environment should have Xdebug 3. (Xdebug 2 is no longer supported.) To configure PHP Xdebug, these are the most common settings for php.ini:
Xdebug 3 Configuration [ править ]
[Xdebug] ;;;;;;;;;;;;;;;;;;; ; xdebug-3.0.0 ; https://xdebug.org/docs/upgrade_guide ;;;;;;;;;;;;;;;;;;; xdebug.mode = debug xdebug.start_with_request = yes xdebug.discover_client_host = true ; xdebug.client_port = 9003
To know more about how Xdebug works internally to connect to the Editor, check the Xdebug 3 — Documentation.
phpcs Configuration [ править ]
To configure the extension, visit the official documentation.
The extension supports the most common configurations of PHP_CodeSniffer.
By default, PHP_CodeSniffer comes with several coding standards. Once the extension is installed, it works with PSR2.
"phpcs.standard": "PSR2",
To configure the extension to apply the Joomla! Coding Standards, install the rules following the official guide.
Once Joomla! Coding Standards is installed, configure the User Settings following this sample configuration:
"phpcs.standard": "/home/YOUR-USER/. /Joomla",
Configuration of PHP Intelephense [ править ]
To configure the extension, visit the official documentation.
The extension does not require additional configuration.
VSCode comes with basic support of the PHP language. It is a good idea to disable the default support to avoid conflicts:
// Disable basic suggestions "php.suggest.basic": false, // To activate code suggestions in comments "editor.quickSuggestions": < "comments": true >,
As an alternative, it is also a popular extension: PHP IntelliSense felixfbecker.php-intellisense Advanced Autocompletion and Refactoring support for PHP.
PHPUnit Configuration [ править ]
To configure the extension, visit the official documentation.
The extension integrates PHP Unit with VSCode, so all features of PHP Unit are integrated with VSCode and can also be parameterized for further integration with the user interface.
This is a sample configuration of the extension:
"phpunit.preferRunClassTestOverQuickPickWindow": true, "phpunit.driverPriority": [ "Path", "Command", "Composer", "Phar", "Ssh", "GlobalPhpUnit" ],
PHP CS Fixer Configuration (Only for PSR2) [ править ]
To configure the extension, visit the official documentation.
The extension integrates PHP CS Fixer with VSCode, so all features of PHP CS Fixer are integrated with VSCode and can also be parameterized for further integration with the user interface.
To automatically apply PHP CS Fixer to PHP files, configure the user settings in this way:
"[php]": < "editor.defaultFormatter": "junstyle.php-cs-fixer", >, "php-cs-fixer.onsave": true,
PHP Phan (Analyzer) Configuration [ править ]
To configure the extension, visit the official documentation.
This is a sample configuration of the extension:
"phan.phpExecutablePath": "/usr/bin/php", "phan.analyzedFileExtensions": [ "php" ],
phpmd Configuration [ править ]
To configure the extension, visit the official documentation.
The extension does not require additional configuration.
This is a sample configuration of the extension:
"phpmd.command": "php /home/YOUR-USER/. /phpmd/phpmd.phar", "phpmd.rules": "/home/YOUR-USER/. /phpmd-rulesets/phpmd_config.xml",
Configure EditorConfig for VS Code [ править ]
EditorConfig is a file format and collection of text editor plugins for maintaining consistent coding styles between different editors and IDEs.
To configure the editor according to your preferences, create a file .editorconfig:
; EditorConfig helps developers define and maintain consistent ; coding styles between different editors and IDEs. ; For more visit https://editorconfig.org/ root = true ; Choose between lf or rf on "end_of_line" property [*] charset = utf-8 end_of_line = lf indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true # editorconfig-tools is unable to ignore longs strings or URLs max_line_length = null [*.html] indent_style = tab # PSR-2 # [*.php] # indent_size = 4 # indent_style = space # Joomla! Coding Standards [*.php] indent_size = 4 indent_style = tab [*.md] trim_trailing_whitespace = false [*.yml] indent_style = space indent_size = 2
- Pages with syntax highlighting errors
- Development
- Beginner Development
- Server configurations
- Server setup local
- Bug Squad
- IDE (Integrated development environment)
