phpro / grumphp

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

phpcs does not detect default ruleset xml file #1096

Closed iMojtaba closed 9 months ago

iMojtaba commented 12 months ago
Q A
Version 2.0.0
Bug? no
New feature? no
Question? yes
Documentation? no
Related tickets

I am encountering an issue with GrumPhp not detecting the PHP CodeSniffer (phpcs) ruleset file in my project. The ruleset file, named phpcs.xml, is located in the root directory of my project and is successfully recognized and applied when running vendor/bin/phpcs Author.php. However, when I use the command php ./vendor/bin/grumphp git:pre-commit, GrumPhp fails to detect and apply these rules. and it applies some other rules which I do not know what those are.

I have verified that GrumPhp is properly installed and configured in my project, and I have the necessary dependencies installed. However, it seems that GrumPhp has its own ruleset file configuration or a different default ruleset file name.

I have already checked the GrumPhp documentation and configuration files but could not find a solution specific to this problem. The only thing that I found is standard in grumphp.yml file and it only accepts phpcs registered styles. I tried to set it as standard: "/full/pathto/phpcs.xml" or standard: "phpcs.xml" and none of them worked.

Thank you in advance for your assistance and support!

My configuration

grumphp:
    parallel:
        enabled: true
        max_workers: 32
    ignore_unstaged_changes: true
    tasks:
        phpcs:
            severity: ~
            error_severity: ~
            warning_severity: ~
            tab_width: ~
            report: full
            report_width: ~
            whitelist_patterns: []
            encoding: ~
            ignore_patterns: []
            sniffs: []
            triggered_by: [php]
            exclude: []
iMojtaba commented 10 months ago

Any comment?

veewee commented 10 months ago

Can you verify what the actual phpcs command that gets executed by GrumPHP is and verify if this matches your expectations? You could do this by adding the -vvv flag to grumphp. (allthough you might want to disable parallel mode for that purpose)

veewee commented 9 months ago

Closing based on inactivity. Feel free to reopen if you are still blocked by this.

pgrond commented 6 months ago

Sorry to reopen this, but I think there is a problem with inclusion and exclusion of files. The phpcs.xml is not read for that, because Phpcs.php uses $files = $context->getFiles()->extensions($config['triggered_by'])->paths($config['whitelist_patterns'] ?? [])->notPaths($config['ignore_patterns'] ?? []); to get the files. This is ignoring the include and exclude patterns in phpcs.xml

The command that is run is '/var/www/html/vendor/bin/phpcs' '--standard=phpcs.xml' '--extensions=php' '--report=full' '--report-json' '--file-list=/tmp/phpxJt5Dv'

veewee commented 6 months ago

So the problem here is that phpcs does not respect it's own configuration file when files are being passed in? I'm not sure that's something we can do much about here. I tend to avoid parsing various configuration files / formats in here tbh.

pgrond commented 6 months ago

After some more debugging, I figured out that the reason is that the --file-list overrides the include/exclude of phpcs, and that is what you expect I guess. So because we only want to check staged files, we have to add the --file-list parameter, otherwise we check all the files all the time. I agree that this is not a grumphp issue. Can be closed again ;)