phpro / grumphp

A PHP code-quality tool
MIT License
4.14k stars 430 forks source link

Git Blacklist example does not work #1149

Open Jamesking56 opened 1 month ago

Jamesking56 commented 1 month ago
Q A
Version 2.6.0
Bug? yes
New feature? no
Question? no
Documentation? yes
Related tickets

My configuration

# grumphp.yml
grumphp:
    tasks:
        git_blacklist:
            keywords:
                - "die\\("
                - "dd\\("
                - "var_dump\\("
                - "exit;"
                - "file_put_contents\\("
            triggered_by: ['php']
            regexp_type: G

Steps to reproduce:

# Generate empty folder
mkdir tmp
cd tmp
git init
echo "vendor" > .gitignore
pbpaste > grumphp.yml
composer require --dev phpro/grumphp

# Your actions
# Please add the steps on how to reproduce the issue here.
Add config file from above

# Run GrumPHP:
git add -A && git commit -m"Test"
# or
./vendor/bin/grumphp run

Result:

git_blacklist
=============

Something went wrong:
fatal: -e option, 'die\(': Unmatched ( or \(

This config matches the config in the docs here: https://github.com/phpro/grumphp/blob/v2.x/doc/tasks/git_blacklist.md

veewee commented 1 month ago

You might need to escape the character twice to make sure it is escaped in both yaml and next in your git cli command.

An alternative solution is to use: https://github.com/phpro/grumphp/blob/v2.x/doc/tasks/phpparser.md

Or phpstan with https://github.com/ekino/phpstan-banned-code.

Both use PHP's AST to figure out if it's allowed or not which is less error prone on input like dump[space]( Whilst the blacklist task only checks for a regex match.

(It has been reported in https://github.com/phpro/grumphp/issues/1139 as well.)