phpro / grumphp

A PHP code-quality tool
MIT License
4.11k stars 429 forks source link

php-cs-fixer when run from grumphp #1043

Closed kapdom closed 1 year ago

kapdom commented 1 year ago
Q A
Version 1.13.0
Bug? yes
New feature? no
Question? yes/no
Documentation? no

My configuration

grumphp:
    tasks:
        phpcsfixer:
            allow_risky: ~
            cache_file: ~
            config: ~
            rules:
                '@PSR2': true
                line_ending: false
                array_syntax:
                    syntax: short
            using_cache: false
            config_contains_finder: true
            verbose: true
            diff: true
            triggered_by: [ 'php' ]

    git_hook_variables:
        DOCKER_CONTAINER: php_centralised_app
        DOCKER_HOME_DIRECTORY: /var/www/html
        EXEC_GRUMPHP_COMMAND: docker exec -i $(docker-compose ps -q php) php
        ENV: { }

.php.cs-fixer.php file

<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$rules = [
    'array_syntax' => ['syntax' => 'short'],
    'binary_operator_spaces' => [
        'default' => 'single_space',
        'operators' => ['=>' => null]
    ],
    'blank_line_after_namespace' => true,
    'blank_line_after_opening_tag' => true,
    'blank_line_before_statement' => [
        'statements' => ['return']
    ],
    'braces' => true,
    'cast_spaces' => true,
    'class_attributes_separation' => [
        'elements' => [
            'method' => 'one',
            'trait_import' => 'none'
        ]
    ],
    'class_definition' => true,
    'concat_space' => [
        'spacing' => 'one'
    ],
    'declare_equal_normalize' => true,
    'elseif' => true,
    'encoding' => true,
    'full_opening_tag' => true,
    'fully_qualified_strict_types' => true,
    'function_declaration' => true,
    'function_typehint_space' => true,
    'heredoc_to_nowdoc' => true,
    'include' => true,
    'increment_style' => ['style' => 'post'],
    'indentation_type' => true,
    'linebreak_after_opening_tag' => true,
    'line_ending' => true,
    'lowercase_cast' => true,
    'constant_case' => true,
    'lowercase_keywords' => true,
    'lowercase_static_reference' => true,
    'magic_method_casing' => true,
    'magic_constant_casing' => true,
    'method_argument_space' => true,
    'native_function_casing' => true,
    'no_alias_functions' => true,
    'no_extra_blank_lines' => [
        'tokens' => [
            'extra',
            'throw',
            'use'
        ]
    ],
    'no_blank_lines_after_class_opening' => true,
    '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' => false,
    'object_operator_without_whitespace' => true,
    'ordered_imports' => ['sort_algorithm' => 'alpha'],
    'phpdoc_indent' => true,
    'general_phpdoc_tag_rename' => true,
    'phpdoc_inline_tag_normalizer' => true,
    'phpdoc_tag_type' => 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,
    'psr_autoloading' => true,
    'self_accessor' => true,
    'short_scalar_cast' => true,
    'simplified_null_return' => false,
    'single_blank_line_at_eof' => true,
    'single_blank_line_before_namespace' => true,
    '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' => true,
    'trim_array_spaces' => true,
    'unary_operator_spaces' => true,
    'visibility_required' => [
        'elements' => ['method', 'property']
    ],
    'whitespace_after_comma_in_array' => true,
    'no_unused_imports' => true,
];

$finder = Finder::create()
    ->in([
        __DIR__ . '/app',
        __DIR__ . '/config',
        __DIR__ . '/database',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

$config = new Config();
return $config->setFinder($finder)
    ->setRules($rules)
    ->setRiskyAllowed(true)
    ->setUsingCache(true);

pre-commit hook

#!/bin/sh

#
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#

# Fetch the GIT diff and format it as command input:
DIFF=$(git -c diff.mnemonicprefix=false -c diff.noprefix=false --no-pager diff -r -p -m -M --full-index --no-color --staged | cat)

# Grumphp env vars

export GRUMPHP_GIT_WORKING_DIR="$(git rev-parse --show-toplevel)"

# Run GrumPHP
(cd "./" && printf "%s\n" "${DIFF}" | docker exec -i $(docker-compose ps -q php) php 'vendor/bin/grumphp' 'git:pre-commit' '--skip-success-output')

Hi. When I run grumphp from command line vendor/bin/grumphp run then everything is ok. But when grumphp run is triggered by git pre-commit hook then I have error.

PHP CS Fixer 3.11.0 Oliva by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 7.4.30

In ConfigurationResolver.php line 551:

  [PhpCsFixer\ConfigurationException\InvalidConfigurationException (16)]  
  For multiple paths config parameter is required.                        

Exception trace:
  at /var/www/html/vendor/friendsofphp/php-cs-fixer/src/Console/ConfigurationResolver.php:551
 PhpCsFixer\Console\ConfigurationResolver->computeConfigFiles() at /var/www/html/vendor/friendsofphp/php-cs-fixer/src/Console/ConfigurationResolver.php:243
 PhpCsFixer\Console\ConfigurationResolver->getConfig() at /var/www/html/vendor/friendsofphp/php-cs-fixer/src/Console/ConfigurationResolver.php:275
 PhpCsFixer\Console\ConfigurationResolver->getConfigFile() at /var/www/html/vendor/friendsofphp/php-cs-fixer/src/Console/Command/FixCommand.php:260
 PhpCsFixer\Console\Command\FixCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1028
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:299
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/friendsofphp/php-cs-fixer/src/Console/Application.php:94
 PhpCsFixer\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /var/www/html/vendor/friendsofphp/php-cs-fixer/php-cs-fixer:114
 include() at /var/www/html/vendor/bin/php-cs-fixer:115

When I debug this error I see in line 546 I have following content in variable.

array:2 [
  0 => "/var/www/html/.php-cs-fixer.php"
  1 => "/var/www/html/app/Services/PostUpdateService.php"
]

Why that second file in array this is a file I modified before commit. So why phpfixer take it as config?

veewee commented 1 year ago

The pre-commit taks passes the changes files to the tool, so that only the changed files are being checked. Normally, the config should only take one argument. Can you try by explicitly setting the config file to .php.cs-fixer.php in your grumphp php-cs-fixer config?

Just out of curiousity: any specific reason you don't validate all rules from config and overwrite them with a limited set of rules?

GuillaumeSTEIN commented 1 year ago

I have a very similar issue. php-grumphp run return success but php-grumphp git:pre-commit return error :


PHP CS Fixer 3.12.0 Oliva by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.1.11

In ConfigurationResolver.php line 559:

  [PhpCsFixer\ConfigurationException\InvalidConfigurationException (16)]  
  For multiple paths config parameter is required.                        

Did someone found the fix ?

GuillaumeSTEIN commented 1 year ago

Found the issue : You need to provide the config attribute in the grumphp.yaml file. In @kapdom case : config : 'php.cs-fixer.php'

I also had to remove the rules attribute :-) Hope this help

veewee commented 1 year ago

Looks like the filename you are using is not using the default filename

https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/UPGRADE-v3.md#rename-of-files

You either have to pass the config option or use the supported default name for the tool. Does it work after renaming the file or do we need to change something at our end?

I currently feel like providing null for the config is the best option for grumphp at this moment. Butif you can think of a better default, feel free to change my mind.

veewee commented 1 year ago

Closing this one for now since an answer was provided. Feel free to reopen or create a new issue if the still persists