phpro / grumphp

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

Need more information on git_blacklist.keywords ignore on some codes #1079

Closed sandipansaha closed 1 year ago

sandipansaha commented 1 year ago
Q A
Version 1.15.0
Bug? no
New feature? no
Question? yes
Documentation? yes
Related tickets NA

How we can ignore the git_blacklist.keywords in some places where it is required as we can do with PHPCS or PHPMD?

My configuration

# grumphp.yml
# Please add a copy of your grumphp.yml file.
# Project level GrumPHP configuration for Magento 2
grumphp:
    hide_circumvention_tip: true
    process_timeout: 120
    stop_on_failure: false
    ignore_unstaged_changes: false
    tasks:
        jsonlint:
            detect_key_conflicts: true
            metadata:
                priority: 100
        xmllint:
            ignore_patterns:
                - "#test/(.*).xml#"
            metadata:
                priority: 100
        phplint:
            triggered_by: ['php', 'phtml']
            metadata:
                priority: 200
        yamllint:
            ignore_patterns:
                - "#test/(.*).yml#"
                - "#charts/.*#"
            metadata:
                priority: 100
        composer:
            file: ./composer.json
            no_check_all: true
            no_check_lock: false
            no_check_publish: false
            with_dependencies: false
            strict: false
            metadata:
                priority: 80
        # validate git commit message
        git_commit_message:
            allow_empty_message: false
            enforce_capitalized_subject: false
            enforce_no_subject_punctuations: false
            enforce_no_subject_trailing_period: true
            enforce_single_lined_subject: true
            type_scope_conventions: []
            max_body_width: 120
            max_subject_width: 80
            case_insensitive: true
            multiline: false
            additional_modifiers: ''
        # validate git branch names
        git_branch_name:
            whitelist:
                # allowed branch names: 'feature/1', 'feature/new', 'feature/new1', 'feature/new-test', 'task/1', etc
                - "/(hotfix|bugfix|feature|release|task)\\/([a-z|0-9|-]+)$/"
            blacklist:
                - "development"
                - "production"
                - "staging"
                - "master"
                - "infra"
            allow_detached_head: true
        # catch not allowed keywords
        git_blacklist:
            keywords:
                - "\\.dev"
                - "\\.local"
                - "\\.test"
                - "<<<<<<<"
                - "======="
                - "DebuggerUtility"
                - "ObjectManager::getInstance"
                - "_GET\\["
                - "_POST\\["
                - "_REQUEST\\["
                - "console.log("
                - "die("
                - "die;"
                - "exit("
                - "exit;"
                - "fileadmin"
                - "localhost"
                - "phpinfo"
                - "phpinfo("
                - "print_r("
                - "var_dump("
                - "_objectManager"
                - "ObjectManagerInterface"
            triggered_by: ['php', 'js', 'html', 'phtml']
            metadata:
                priority: 90
        # https://devdocs.magento.com/guides/v2.4/coding-standards/code-standard-php.html
        phpcs:
            standard: Magento2
            tab_width: 4
            severity: 10 # can remove this to dis-allow all level of severity.
            error_severity: 10
            warning_severity: ~
            report: full
            triggered_by: [phtml, php]
            metadata:
                priority: 70
        phpcsfixer2:
            allow_risky: false
            config: '.php-cs-fixer.dist.php'
            triggered_by: ['php', 'phtml']
            using_cache: true
            cache_file: './.php-cs-fixer.cache'
            config_contains_finder: false
            verbose: true
        phpmd:
            ruleset: ['./dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml']
            triggered_by: ['php']
            exclude:
                - "./app/code/Magento/"
                - "./app/code/*/*/Setup/"
            metadata:
                priority: 70
        #  uncomment to skip modules using whitelist patterns
        #  whitelist_patterns:
        #     - /^app\/code\/MyVendor\/MyModuleToSkip\/(.*)/

        # https://devdocs.magento.com/guides/v2.4/test/testing.html#phpstan
        phpstan:
            autoload_file: ~
            configuration: './dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon'
            level: 8
            triggered_by: ['php']
            force_patterns: []
            ignore_patterns: []
            memory_limit: "-1"
            metadata:
                priority: 90
        phpversion:
            project: '7.4'

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.

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

Result:

# Please add the result of the run or git commit actions here.
veewee commented 1 year ago

Not sure if I understand this question. But you can work with an ignore or whitelist pattern to conditionally check them? See https://github.com/phpro/grumphp/blob/master/doc/tasks/git_blacklist.md

sandipansaha commented 1 year ago

I wanted to keep the blacklist keyword but want to ignore the check for a particular function of a class but not the whole file. Like I have "print_r(" or "console.log(" added as blacklisted keywords. But I want to use it in certain methods called "debug".

Can we ignore the check for that area only? Like in phpcs we can ignore a line using - "// phpcs:ignore"

veewee commented 1 year ago

No that is not possible with that task. The task is a simple string lookup and doesn't have any context of what method or class it's in. If you want that, you probably should go for phpstan or psalm.