Reputation: 2455
I want to use php-cs-fixer + .php_cs config file for defining rules - without having to install PHP on my host machine.
I am using PHP7.3 inside of a docker container - this is also where composer is installed. This means I do not have php running on my local machine.
Extensions for vscode - like junstyle's php-cs-fixer seemed to require that the executable, config file, and current working directory are localized. I could not get this to work.
friendsofphp/php-cs-fixer
installed with composer required for dev/var/www/site/app/vendor/bin/php-cs-fixer fix --verbose --config=/var/www/site/.php_cs {PATH TO MY FILE I WANT TO FIX}
as a proof of concept from within the docker container. It worked perfectly.
/var/www/site/
is the path inside the container that maps to my ~/code/project
on my host machineThis blog post gave a tip about making a script eg `/usr/local/bin/docker-phpcs with the following contents (and chmod +x to make it executable)
#!/bin/bash
/usr/bin/docker-compose exec -T app-php /var/www/vendor/bin/php-cs-fixer "$@"
But, I couldn't get this to work, even when defining the vscode setting for the junstyle extension from "php-cs-fixer.executablePath": "php-cs-fixer"
to "php-cs-fixer.executablePath": "/usr/local/bin/docker-phpcs",
This gave me a ton of:
[2021-04-12 18:36:39.174] [exthost] [error] [junstyle.php-cs-fixer] provider FAILED
[2021-04-12 18:36:39.174] [exthost] [error] undefined
Upvotes: 12
Views: 10325
Reputation: 897
I come with my 2 cents for vscode, as I gave up on using any php-cs-fixer related extension.
Install this extensions https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave.
Basically, it allows you to run any command when saving a file; it is not targeted specifically to php fixer
Then, in workspace's or folder's settings, setup a command to forward instruction on saved file, using extension's placeholder.
{containerName}
is the name of the container you want to forward command to, ${relativeFile}
is the extension's placeholder for the relative path of the saved file.
"emeraldwalk.runonsave": {
"commands": [
{
"match": ".php",
"cmd": "docker exec -i {containerName} /var/www/html/tools/php-cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --format=json fix ${relativeFile}"
},
]
}
Make sure to set the appropriate path (inside the container) of php-cs-fixer executable.
Upvotes: 3
Reputation: 310
As part of a package solution for overall application code quality control (interitty/code-checker), I solved the situation of running a php-cs-fixer inside a docker container from NetBeans IDE on a guest machine.
So, I created a bash script that emulates the php-cs-fixer interface and passes the parameters to the specified container.
Related documentation can be found in the readme
I hope that this should help you too.
Upvotes: 0
Reputation: 2455
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:
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.
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:
git clone [email protected]:amurrell/simple-php-cs-fixer.git
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.
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):
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 $@
}
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
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,
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);
Upvotes: 6