phpro / grumphp

A PHP code-quality tool
MIT License
4.15k stars 431 forks source link

Question or issue about die() function blacklisted in Phpparser #243

Closed brunoauger closed 7 years ago

brunoauger commented 7 years ago
Q A
Bug? no
New feature? yes ?
Question? yes

Hello, I'm trying to remove all die() or exit() from my code....

My configuration

parameters:
    bin_dir: "./vendor/bin"
    git_dir: "."
    tasks:
        phpcs:
            standard: PSR2
            show_warnings: false
        yamllint: ~
        phplint: ~
        phpcsfixer: ~
        phpparser:
            visitors:
                forbidden_function_calls:
                    blacklist:
                        - 'var_dump'
                        - 'dump'
                        - 'die'

It doesn't work... for the function die() So I did that in: vendor/phpro/grumphp/src/GrumPHP/Parser/Php/Visitor/ForbiddenFunctionCallsVisitor.php

    public function leaveNode(Node $node)
    {

        if($node instanceof Node\Expr\Exit_){
            $this->addError(
                sprintf('Found blacklisted "%s" function call', 'die or exit'),
                $node->getLine(),
                ParseError::TYPE_ERROR
            );
            return;
        }

        if (!$node instanceof Node\Expr\FuncCall) {
            return;
        }

        $function = $node->name;
        if (!in_array($function, $this->blacklist)) {
            return;
        }

        $this->addError(
            sprintf('Found blacklisted "%s" function call', $function),
            $node->getLine(),
            ParseError::TYPE_ERROR
        );
    }

And it works as expected!

Improvement I would like to avoid override the bundle...Workable ?

veewee commented 7 years ago

Hello @brunoauger,

In the php parser, these nodes are called exit nodes. We've added a separate visitor no_exit_statements to handle these exit nodes. More info can be found here: https://github.com/phpro/grumphp/blob/master/doc/tasks/phpparser.md#no_exit_statements

FYI: I've added an additional note to the documentation of the forbidden_function_calls visitor: https://github.com/phpro/grumphp/commit/36d53d49e2535b07b8bdd582fbc1d3b827ea2f6e