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

Specify encoding

Open weirdan opened this issue 8 years ago • 5 comments

By default, PHPCS assumes file encoding to be encoded in ISO-8859-1. Nowadays people generally use UTF-8, but some may use other multibyte charsets as well.

Not specifying encoding when running PHPCS on multibyte (including UTF-8, when using non-ascii characters) encoded files causes it to report wrong character offsets, and also causes character-width sensitive sniffs fail where they shouldn't. For example, this code trigger failures of line-length sniff (from PSR2 ruleset):

<?php
$q = "ЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖЖ";

even though it only contains 68 characters (and PSR2 requires line lengths to be under 120 chars).

See AtomLinter/linter-phpcs#228 and AtomLinter/linter-phpcs#233 for possible fix. Those PRs got eventually reverted, since Atom's linter feeds the text to PHPCS via stdin, but they are are relevant for your project because you pass file paths to PHPCS.

weirdan avatar Feb 11 '17 01:02 weirdan

@weirdan Thank you for reporting this issue. I am trying to find a way to access the document encoding from vscode but have not found a solution yet. I will open a ticket with the language server guys and hope to resolve the issue.

ikappas avatar Feb 11 '17 13:02 ikappas

And you're not alone, it seems: Microsoft/vscode#824

weirdan avatar Feb 11 '17 14:02 weirdan

It seems that this can not go forward unless Microsoft/vscode#824 is fixed.

ikappas avatar Feb 11 '17 17:02 ikappas

@ikappas Could not we just send an extra flag to phpcs? Something like:

settings.json

{
    ...
    "phpcs.encoding": "windows-1252"
    ...
}

command phpcs --encoding=$ENCODING ...

josenicomaia avatar Aug 01 '17 15:08 josenicomaia

I added an PR to solve it.

josenicomaia avatar Aug 01 '17 16:08 josenicomaia