A Visual Studio Code extension that provides real-time diagnostics and formatting for PHP files using php-cs-fixer.
- Real-time Diagnostics: Display php-cs-fixer errors and warnings directly in your code as you type
- Auto-formatting: Format your PHP files using php-cs-fixer when you save or manually format
- Configurable: Customize php-cs-fixer settings through VS Code settings
- Commands: Manual commands to check or fix individual files
- php-cs-fixer must be installed and accessible
- PHP must be installed on your system
You can install php-cs-fixer globally using composer:
composer global require friendsofphp/php-cs-fixerOr install it locally in your project:
composer require --dev friendsofphp/php-cs-fixerThis extension contributes the following settings:
advancedPhpCsFixer.executablePath: Path to php-cs-fixer executable (default:php-cs-fixer)advancedPhpCsFixer.configPath: Path to .php-cs-fixer.php config file (optional)advancedPhpCsFixer.enableDiagnostics: Enable/disable diagnostics (default:true)advancedPhpCsFixer.enableFormatting: Enable/disable formatting (default:true)advancedPhpCsFixer.allowRisky: Allow risky rules (default:false)advancedPhpCsFixer.rules: Custom php-cs-fixer rules (overrides config file)
Add this to your VS Code settings.json:
{
"advancedPhpCsFixer.executablePath": "vendor/bin/php-cs-fixer",
"advancedPhpCsFixer.configPath": "${workspaceFolder}/.php-cs-fixer.php",
"advancedPhpCsFixer.enableDiagnostics": true,
"advancedPhpCsFixer.enableFormatting": true,
"advancedPhpCsFixer.allowRisky": true
}The extension provides the following commands:
PHP CS Fixer: Fix Current File: Apply php-cs-fixer to the current PHP filePHP CS Fixer: Check Current File: Check the current file for code style issues
You can access these commands through the Command Palette (Cmd+Shift+P on macOS or Ctrl+Shift+P on Windows/Linux).
Once the extension is installed and configured:
- Open any PHP file
- The extension will automatically analyze your code
- Errors and warnings will appear as squiggly lines in your editor
- Hover over them to see details about the code style issues
To format a PHP file:
- Open a PHP file
- Use the format document command:
- macOS:
Shift+Option+F - Windows/Linux:
Shift+Alt+F
- macOS:
- Or right-click and select "Format Document"
- The extension will apply php-cs-fixer fixes to your file
You can also enable format on save by adding this to your settings:
{
"[php]": {
"editor.defaultFormatter": "ghostlexly.advanced-php-cs-fixer",
"editor.formatOnSave": true
}
}You can create a .php-cs-fixer.php file in your project root to configure php-cs-fixer rules:
<?php
$config = new PhpCsFixer\Config();
return $config
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
);Then reference it in your VS Code settings:
{
"advancedPhpCsFixer.configPath": "${workspaceFolder}/.php-cs-fixer.php"
}-
Verify that php-cs-fixer is installed:
php-cs-fixer --version
-
Check the executable path in your settings:
- If installed globally:
php-cs-fixer - If installed locally:
vendor/bin/php-cs-fixer
- If installed globally:
-
Check the VS Code Output panel:
- Open View > Output
- Select "Advanced php-cs-fixer" from the dropdown
- Ensure
advancedPhpCsFixer.enableDiagnosticsis set totrue - Check that the file is saved (diagnostics work best on saved files)
- Verify your php-cs-fixer configuration is correct
- Ensure
advancedPhpCsFixer.enableFormattingis set totrue - Make sure the file is saved before formatting
- Check that you have write permissions to the file
- The extension requires files to be saved to disk before formatting
- Large files may take a moment to analyze
- Some php-cs-fixer rules may not show detailed diagnostics
Initial release:
- Real-time diagnostics for php-cs-fixer
- Document formatting support
- Configurable settings
- Manual fix and check commands
Found a bug or have a feature request? Please open an issue on the GitHub repository.
MIT