vscode-php-cs-fixer icon indicating copy to clipboard operation
vscode-php-cs-fixer copied to clipboard

Fix on save does not respect finder

Open cartok opened this issue 3 years ago • 8 comments

Looks like fix on save does not respect the finder object. I've created a finder with in('/foo/bar'), created a file /baz.php and it gets auto fixed on save, but it should not. This can make the plugin unusable, can someone confirm this?

cartok avatar Jul 04 '22 14:07 cartok

I guess it's because the plugin has an extra setting (exclude). I think it should rather be include but why have it at all?

cartok avatar Jul 04 '22 14:07 cartok

I also tried to use a negated glob (ext glob is enabled by default in anymatch) and a negative lookahead regex but without success.

cartok avatar Jul 04 '22 14:07 cartok

Tried it again with:

"php-cs-fixer.exclude": [
  "**/src/**",
  "!(**/src/foo/bar/**)",
]

Which seems to work files outside of src/foo/bar get ignored on save but inside still gets fixed, but it's not a nice solution I think.

cartok avatar Jul 04 '22 15:07 cartok

when use this extension to fix a file, it create a tmp file and then pass to php-cs-fixer, not the current edited file, maybe the config in is useless.

 "php-cs-fixer.pathMode": {
          "type": "string",
          "enum": [
            "override",
            "intersection"
          ],
          "default": "override",
          "description": "--path-mode can be override or intersection, intersection only works on explorer context menu action, not works for current focused file. detail see:https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage"
        },

and see this extension's config: pathMode, this option always override when you fix the current edited file. so the config in
has been overridden, if you open the vscode's Developer Tools, you will see

Paths from configuration file have been overridden by paths provided as command arguments.

if you want the config in to work, you can use the command php-cs-fixer.fix, or context menu -> php-cs-fixer.fix

so i think the best way is using the option: php-cs-fixer.exclude, if you fix the current edited file.

junstyle avatar Jul 05 '22 00:07 junstyle

Thanks, so ok I get it, you documented that it pathMode=intersection is not possible atm. Can't it get improved? I imagine that it would be good if by default mode would be intersection and the config would get used (including the rules). I tried it with intersection and get Cannot create intersection with not-fully defined Finder in configuration file., see: image The screenshot also shows that --config is not passed.

My finder configuration is:

$fullPath = getcwd() . '/app';
$finder = PhpCsFixer\Finder::create()
   ->in($fullPath)
   ->files()
   ->name(['*.php', '*.phtml'])
;

cartok avatar Jul 06 '22 07:07 cartok

To the topic about the exclude option: What about adding an include option aswell?

cartok avatar Jul 06 '22 07:07 cartok

Why the temp file?

cartok avatar Jul 06 '22 07:07 cartok

because use tmp file, so the config in can't match the tmp file path generally.

why i use tmp file? this extension not only provide to format a whole file, but also format partial codes, such as selected range formatting... and if you want to format an unsaved file, and not change the behavior to save first, need a temp file to do that.

junstyle avatar Jul 06 '22 09:07 junstyle