vscode-phpcs
vscode-phpcs copied to clipboard
adding phpcbf support
Are there any plans to implement phpcbf support?
@sataris I haven't considered adding phpcbf
support as it works differently from phpcs.
Our team uses it frequently from the command line before pushing code to the repository potentially fixing simple issues that we neglected.
We've actually incorporated phpcs
and phpcbf
support via composer scripts in order to use them in CI like:
"scripts": {
"run-phpcs": [
"@run-phpcs:stack",
"@run-phpcs:plugin",
"@run-phpcs:theme"
],
"run-phpcs:stack": [
"\"vendor/bin/phpcs\" --standard=ruleset.xml --extensions=php -p -n -s --colors ."
],
"run-phpcs:plugin": [
"\"vendor/bin/phpcs\" --standard=./web/app/plugins/demo/ruleset.xml --extensions=php -p -n -s --colors ./web/app/plugins/demo/"
],
"run-phpcs:theme": [
"\"vendor/bin/phpcs\" --standard=./web/app/themes/demo/ruleset.xml --extensions=php -p -n -s --colors ./web/app/themes/demo/"
],
"fix-phpcs": [
"@fix-phpcs:stack",
"@fix-phpcs:plugin",
"@fix-phpcs:theme"
],
"fix-phpcs:stack": [
"\"vendor/bin/phpcbf\" --standard=ruleset.xml --extensions=php -n ."
],
"fix-phpcs:plugin": [
"\"vendor/bin/phpcbf\" --standard=./web/app/plugins/demo/ruleset.xml --extensions=php -n ./web/app/plugins/demo/"
],
"fix-phpcs:theme": [
"\"vendor/bin/phpcbf\" --standard=./web/app/themes/demo/ruleset.xml --extensions=php -n ./web/app/themes/demo/"
]
},
So we can also easily run these tasks in the command line like:
$ composer fix-phpcs:plugin
But I've come across many cases where it failed to parse files so I don't know if implementing this in vs code would be consistent.
How do you use phpcbf
and how do you want this feature supported?
@ikappas in fact, i hope we can use phpcbf to fix the page i just edit, because sometime we found a page have many issues to fix, and i have to open terminal and copy the path to fix it, i think it's complex sometime, so i hope i can type some command like format code, and it can auto fix this page or this project problem.
+1 for phpcbf support. The way I would like to see it supported is that the plugin would run phpcbf on the currently open file in vscode when I save the file. That way it could quickly fix simple formatting errors I missed and then still review the changes.
Another way it could work is similar to the way a git merge or go-tests work, with "inline buttons" (not sure what the proper name of this feature is).
Here's an example of the way go does it with running tests. We could have something similar with phpcbf called "fix code" or something like that.
I was hoping that VSCode's "Format Document" command would simply respect the phpcs settings for a php file. That'd be incredible.
If it's not possible or desirable to hook into that command, perhaps a simple new command that would attempt phpcbf on the current file, named "Format Document with PHPCS" or some such.
A quick snippet for tasks.json
which works, but could use someone smarter than me to work out the problemMatcher
{
"version": "2.0.0",
"tasks": [
{
"group": "build",
"label": "Beautify/Fix PHP",
"type": "shell",
"command": "./vendor/bin/phpcbf ${file} --standard=WordPress"
}
]
}
Of course the feature request here would be the nicety of pulling "phpcs.standard": "WordPress"
value into the task automagically.
I just released vscode-phpcbf
The extension by @soderlind works well!
It's a working example of having the command fix the current file, and registering a formatter to hook into vscode's formatOnSave and right-click "format file" options.
It'd be nice to see that folded into this extension, so that the work of finding the executable, and setting configurations etc, could be shared work.
@soderlind Now that 1.0.0 is out we can merge phpcbf support in. What do you think?
@ikappas I you mean adding support for phpcbf in your plugin, please do. I'll discontinue my plugin if you add phpcbf to vscode-phpcs :)
@soderlind Care to do an MR?
@WraithKenny Care to do it ? I'm very busy atm.
@soderlind Yes, I'm pretty busy too, but I can take a stab at this I think! (Probably over the next weekend or two.)
@WraithKenny Any progress in this feature?
No, it kinda fell off my radar. Sorry about not that
@soderlind sadly phpcbf extension doesn't work for me.
The following VS Code extension has both the linter (phpcs) and the fixer (phpcbf):
- https://github.com/wongjn/vscode-php-sniffer
The fixer is run every time you use the "Format document" and "Format selection" commands.
Cheers Guido
@WraithKenny I took the burden on me (that was no fun at all) and brew something like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Check PHP 7.3 compatibility",
"type": "process",
"linux": {
"options": {
"env": {
"LANG": "C"
}
}
},
"group": "build",
"presentation": {
"revealProblems": "onProblem",
"echo": true,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
"command": "./vendor/bin/phpcs",
"args": [
"--standard=PHPCompatibilityWP",
"--runtime-set",
"testVersion",
"7.3",
"--report=checkstyle",
"${file}"
],
"isBackground": false,
"problemMatcher": [
{
"owner": "php",
"fileLocation": "absolute",
"severity": "error",
"pattern": [
{
"regexp": "^<file name=\"(.*)\">$",
"file": 1
},
{
"regexp": "<error line=\"(\\d*)\" column=\"(\\d*)\" severity=\"(error)\" message=\"(.*)\" source=\"(.*)(\"\\/>+)$",
"line": 1,
"column": 2,
"severity": 3,
"message": 4,
"code": 5,
"loop": true
}
]
},
{
"owner": "php",
"fileLocation": "absolute",
"severity": "warning",
"pattern": [{
"regexp": "^<file name=\"(.*)\">$",
"file": 1
},
{
"regexp": "<error line=\"(\\d*)\" column=\"(\\d*)\" severity=\"(warning)\" message=\"(.*)\" source=\"(.*)(\"\\/>+)$",
"line": 1,
"column": 2,
"severity": 3,
"message": 4,
"code": 5,
"loop": true
}
]
}
]
}
]
}
The most important part is argument --report=checkstyle
and the command
path, of course. That's why I post the whole thing. Your mileage may vary:
I wrote this vscode
problemMatcher for a Wordpress project with phpcs
that has plugins written in PHP 5.6 hence the arguments for PHPCompatibilityWP
. In case someone is interested this is my composer.json
{
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
"wp-coding-standards/wpcs": "^2.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"phpcompatibility/phpcompatibility-wp": "*"
}
}
@WraithKenny I took the burden on me (that was no fun at all) and brew something like this:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "Check PHP 7.3 compatibility", "type": "process", "linux": { "options": { "env": { "LANG": "C" } } }, "group": "build", "presentation": { "revealProblems": "onProblem", "echo": true, "reveal": "silent", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true }, "command": "./vendor/bin/phpcs", "args": [ "--standard=PHPCompatibilityWP", "--runtime-set", "testVersion", "7.3", "--report=checkstyle", "${file}" ], "isBackground": false, "problemMatcher": [ { "owner": "php", "fileLocation": "absolute", "severity": "error", "pattern": [ { "regexp": "^<file name=\"(.*)\">$", "file": 1 }, { "regexp": "<error line=\"(\\d*)\" column=\"(\\d*)\" severity=\"(error)\" message=\"(.*)\" source=\"(.*)(\"\\/>+)$", "line": 1, "column": 2, "severity": 3, "message": 4, "code": 5, "loop": true } ] }, { "owner": "php", "fileLocation": "absolute", "severity": "warning", "pattern": [{ "regexp": "^<file name=\"(.*)\">$", "file": 1 }, { "regexp": "<error line=\"(\\d*)\" column=\"(\\d*)\" severity=\"(warning)\" message=\"(.*)\" source=\"(.*)(\"\\/>+)$", "line": 1, "column": 2, "severity": 3, "message": 4, "code": 5, "loop": true } ] } ] } ] }
The most important part is argument
--report=checkstyle
and thecommand
path, of course. That's why I post the whole thing. Your mileage may vary:
I wrote this
vscode
problemMatcher for a Wordpress project withphpcs
that has plugins written in PHP 5.6 hence the arguments forPHPCompatibilityWP
. In case someone is interested this is mycomposer.json
{ "require-dev": { "squizlabs/php_codesniffer": "^3.4", "wp-coding-standards/wpcs": "^2.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", "phpcompatibility/phpcompatibility-wp": "*" } }
Shouldn't you be able to use this?
"--standard=${config:phpcs.standard}",
"--ignore=${config:phpcs.ignorePatterns}",