Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding phpcbf support #28

Open
sataris opened this issue Feb 1, 2017 · 19 comments
Open

adding phpcbf support #28

sataris opened this issue Feb 1, 2017 · 19 comments

Comments

@sataris
Copy link

sataris commented Feb 1, 2017

Are there any plans to implement phpcbf support?

@ikappas
Copy link
Owner

ikappas commented Feb 1, 2017

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

@tradzero
Copy link

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

@jenswegar
Copy link

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

@phayes
Copy link

phayes commented Aug 30, 2017

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

@WraithKenny
Copy link

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.

@ikappas ikappas mentioned this issue Nov 2, 2017
@WraithKenny
Copy link

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.

@soderlind
Copy link

I just released vscode-phpcbf

@WraithKenny
Copy link

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.

@ikappas
Copy link
Owner

ikappas commented Feb 21, 2018

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

@soderlind
Copy link

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

@ikappas
Copy link
Owner

ikappas commented Feb 21, 2018

@soderlind Care to do an MR?

@soderlind
Copy link

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

@WraithKenny
Copy link

@soderlind Yes, I'm pretty busy too, but I can take a stab at this I think! (Probably over the next weekend or two.)

@ikappas
Copy link
Owner

ikappas commented Mar 13, 2018

@WraithKenny Any progress in this feature?

@WraithKenny
Copy link

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

@ahmadawais
Copy link

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

@coccoinomane
Copy link

The following VS Code extension has both the linter (phpcs) and the fixer (phpcbf):

The fixer is run every time you use the "Format document" and "Format selection" commands.

Cheers
Guido

@bekopharm
Copy link

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

@mbomb007
Copy link

@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}",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests