vscode-phpcs icon indicating copy to clipboard operation
vscode-phpcs copied to clipboard

adding phpcbf support

Open sataris opened this issue 7 years ago • 19 comments

Are there any plans to implement phpcbf support?

sataris avatar Feb 01 '17 08:02 sataris

@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 avatar Feb 01 '17 09:02 ikappas

@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.

tradzero avatar Feb 15 '17 02:02 tradzero

+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.

jenswegar avatar Feb 16 '17 13:02 jenswegar

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.

image

phayes avatar Aug 30 '17 16:08 phayes

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.

WraithKenny avatar Nov 01 '17 14:11 WraithKenny

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.

WraithKenny avatar Nov 06 '17 15:11 WraithKenny

I just released vscode-phpcbf

soderlind avatar Jan 07 '18 12:01 soderlind

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.

WraithKenny avatar Jan 10 '18 18:01 WraithKenny

@soderlind Now that 1.0.0 is out we can merge phpcbf support in. What do you think?

ikappas avatar Feb 21 '18 11:02 ikappas

@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 avatar Feb 21 '18 11:02 soderlind

@soderlind Care to do an MR?

ikappas avatar Feb 21 '18 11:02 ikappas

@WraithKenny Care to do it ? I'm very busy atm.

soderlind avatar Feb 21 '18 11:02 soderlind

@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 avatar Feb 22 '18 16:02 WraithKenny

@WraithKenny Any progress in this feature?

ikappas avatar Mar 13 '18 13:03 ikappas

No, it kinda fell off my radar. Sorry about not that

WraithKenny avatar Mar 13 '18 18:03 WraithKenny

@soderlind sadly phpcbf extension doesn't work for me.

ahmadawais avatar May 30 '18 21:05 ahmadawais

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

coccoinomane avatar Feb 14 '19 16:02 coccoinomane

@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:

image

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": "*"
	}
}

bekopharm avatar Aug 01 '19 16:08 bekopharm

@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:

image

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": "*"
	}
}

Shouldn't you be able to use this?

        "--standard=${config:phpcs.standard}",
        "--ignore=${config:phpcs.ignorePatterns}",

mbomb007 avatar Jan 10 '20 20:01 mbomb007