Skip to content

Warning about "unused code after RETURN." #40

Open
@rr-

Description

@rr-

Consider this example:

<?php
class Test
{
    public function test($params)
    {
        return $this->commit(function() use ($params)
        {
            $whatever = 5;
            return $whatever;
        });
    }

    private function commit($callback)
    {
        //complex logic using $callback can go here
        $callback();
    }
}

PHPCheckstyle warns me about it:

File "/home/rr-/test/test.php" warning, line 6 - Function test has unused code after RETURN.

even though it isn't true. To make the warning go away, I need to refactor the code like this:

<?php
class Test
{
    public function test($params)
    {
        $callback = function() use ($params)
        {
            $whatever = 5;
            return $whatever;
        };
        return $this->commit($callback);
    }

    private function commit($callback)
    {
        //complex logic using $callback can go here
        $callback();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions