Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active December 15, 2021 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soderlind/33eca92ffd22420681518e4abaef1849 to your computer and use it in GitHub Desktop.
Save soderlind/33eca92ffd22420681518e4abaef1849 to your computer and use it in GitHub Desktop.
VS Code: PHP Code Beautifier and Fixer (phpcbf) and Short Array Syntax Converter task runners.

PHP Code Beautifier and Fixer (phpcbf)

phpcbf is included when you install PHP_CodeSniffer. Tom McFarlin has written a nice guide on how to install PHP_CodeSniffer and the WordPress Coding Standards Rules for VSCode. If you follow this guide, phpcbf will be installed in the project root, in ./vendor/bin/phpcbf

PHP 5.4 Short Array Syntax Converter

You'll find the convert.php script at GitHub

Adding and running the tasks runners

Add the content in tasks.json to ./.vscode/tasks.json. Make sure the paths are correct.

When you want to beautify PHP, or convert array() to [], on a Mac, hit Shift+Cmd+B and select the task runner.

Credits

I learned about the phpcbf task runner from WraithKenny

{
"version": "2.0.0",
"tasks": [
{
"group": "build",
"label": "Beautify/Fix PHP",
"type": "shell",
"command": "./vendor/bin/phpcbf ${file} --standard=WordPress-Core"
},
{
"group": "build",
"label": "PHP 5.4 Short Array Syntax Converter",
"type": "shell",
"command": "/usr/local/bin/php /Users/per/scripts/convert.php -w ${file}"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment