Update: New Solution
Skip to the tldr; section below for direct solution. Otherwise, enjoy:
I had to accept that there were no php-cs-fixer vscode plugins that would work with solving the path difference between my host machine and docker container.
I got deep into the weeds and I forked Simple PHP CS FIXER adding a few more configuration options:
executablePath - relative to my host machine so I could use a script in my project. eg. /Users/me/myproject/docker-phpcs - this project was originally hardcoding php-cs-fixer here. That is still the default in my version.
Here's an example script:
#!/bin/bash
docker exec -t "mycontainer" /var/www/site/app/vendor/bin/php-cs-fixer $@
Note: I made sure to chmod +x on my script to make it executable
hostPath - so I could tell it my project path eg. "/Users/me/code/myproject/"
dockerPath - so I could tell it my docker container path eg. "/var/www/site"
Then I modified the extension.js to replace the hostName with the dockerPath in the following files:
- the config file, if one is set.
- the document-being-saved's fileName.
So when using this, I create my settings per-folder like this:
"simple-php-cs-fixer.executablePath": "/Users/me/code/myproject/docker-phpcs",
"simple-php-cs-fixer.config": ".php_cs",
"simple-php-cs-fixer.hostPath": "/Users/me/code/myproject",
"simple-php-cs-fixer.dockerPath": "/var/www/site",
And I purposefully do not force this on save - and instead made a key-binding for the plugin's command (ctrl+s, instead of cmd + s), so I can control when I want to completely obliterate my file.
tldr;
I forked the plugin to provide options for docker-relative paths and custom executable path to php-cs-fixer - I also did make a PR to the plugin - but if that does not get accepted... you can side load the extension like so:
- "install" the plugin using marketplace as normal
- go to your extensions folder (on my mac it was $HOME/.vscode/extensions
git clone git@github.com:amurrell/simple-php-cs-fixer.git
- remove the one installed by marketplace like:
calebporzio.simple-php-cs-fixer-x.x.x
Hopefully PR gets accepted...
In the meantime, with ctrl+s key-binding and this modified plugin with extra settings, I now get a fixed file according to my configuration, using my docker-container.
Another Close Solution
Update: This was not really accurately solving my problem - my script would trigger when saving but it wasn't actually processing the current document file being edited/saved - it was triggering the script which would scan all my files defined in my configuration file. This means the plugin wasn't getting to control the configuration file either.
I got what I wanted! Assuming the context I have in my question (eg. your project has friendsofphp/php-cs-fixer required in composer):
- Make an edit to your .zshrc or .bashrc file with snippet below
- Install the Simple PHP CS Fixer extension & update with desired settings
Make Edit to your .zshrc or .bashrc
This source gave me the idea to store php-cs-fixer in my .zshrc or .bashrc file like this:
php-cs-fixer () {
docker run -it --rm --net host -v `pwd`:/app ypereirareis/php-cs-fixer fix --level=psr2 --verbose $@
}
Adapted to my circumstance:
I pasted this into the bottom of my .zshrc file:
php-cs-fixer () {
docker exec -t "mycontainername" /var/www/site/app/vendor/bin/php-cs-fixer fix --verbose --config=/var/www/site/.php_cs $@
}
- mycontainername can be retrieved by:
docker ps and looking at last column "Names"
/var/www/site/app/ is the container's path to my project's app folder where my composer.json file is
/var/www/site/.php_cs is the container's path to my project's .php_cs file which is at the root of my project eg. ~/code/project/.php_cs
Install Simple PHP CS FIXER
This extension for vscode makes it easy. I assume it will just call "php-cs-fixer" and the edit your profile will find that function and run the docker stuff for you.
Here are my setting changes:
"simple-php-cs-fixer.config": ".php_cs",
"simple-php-cs-fixer.save": true,
BONUS Note:
My .php_cs file in case anyone is curious - I did make my project_path relative to the container and not my host machine.
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$rules = [
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'default' => 'single_space',
'operators' => ['=>' => null],
],
'blank_line_after_namespace' => false,
'blank_line_after_opening_tag' => false,
// 'blank_line_before_statement' => [
// 'statements' => ['return'],
// ],
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => [
'elements' => ['method'],
],
'class_definition' => true,
'concat_space' => [
'spacing' => 'one',
],
'declare_equal_normalize' => true,
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'fully_qualified_strict_types' => true, // added by Shift
'function_declaration' => true,
'function_typehint_space' => true,
'heredoc_to_nowdoc' => true,
'include' => true,
'increment_style' => ['style' => 'post'],
'indentation_type' => true,
'linebreak_after_opening_tag' => false,
'line_ending' => true,
'lowercase_cast' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true, // added from Symfony
'magic_method_casing' => true, // added from Symfony
'magic_constant_casing' => true,
'method_argument_space' => true,
'native_function_casing' => true,
'no_alias_functions' => true,
'no_extra_blank_lines' => [
'tokens' => [
'extra',
'throw',
'use',
'use_trait',
],
],
'no_blank_lines_after_class_opening' => false,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => [
'use' => 'echo',
],
'no_multiline_whitespace_around_double_arrow' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_around_offset' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'not_operator_with_successor_space' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'phpdoc_indent' => true,
'phpdoc_inline_tag' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'psr4' => true,
'self_accessor' => true,
'short_scalar_cast' => true,
'simplified_null_return' => false, // disabled by Shift
'single_blank_line_at_eof' => true,
'single_blank_line_before_namespace' => false,
'single_class_element_per_statement' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_line_comment_style' => [
'comment_types' => ['hash'],
],
'single_quote' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => ['method', 'property'],
],
'whitespace_after_comma_in_array' => true,
];
$project_path = '/var/www/site/app';
$finder = Finder::create()
->in([
$project_path . '/app',
$project_path . '/config',
$project_path . '/database',
$project_path . '/resources',
$project_path . '/routes',
$project_path . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return Config::create()
->setFinder($finder)
->setRules($rules)
->setRiskyAllowed(true)
->setUsingCache(false);