Search code examples
phplaravellaravel-5phpmdcode-climate

Explicit variable declaration when using extract()


I have the following snippet:

protected function sendEmail($email)
{
    extract($email);

    $this->transmail->locale($locale)
                    ->timezone($timezone)
                    ->template($template)
                    ->subject($subject)
                    ->send($header, $params);
}

This code works perfectly (full source code here). However, I want to make sure to follow some good practices on the go. I'm currenlty getting [some CodeClimate warnings] (PHPMD)(https://codeclimate.com/github/timegridio/timegrid/app/Listeners/SendBookingNotification.php):

  • Avoid unused local variables such as '$locale'.
  • Avoid unused local variables such as '$timezone'.
  • Avoid unused local variables such as '$template'.
  • Avoid unused local variables such as '$subject'.
  • Avoid unused local variables such as '$header'.
  • Avoid unused local variables such as '$params'.

Which would be elegant ways to go about it?

Should I explicitly declare the variables with list() or something like such?

Thanks in advance


Solution

  • You can use doc comment annotations to exclude methods or classes from PHPMD or to suppress special rules for some software artifacts.

    /**
     * This will suppress all the PMD warnings in
     * this class.
     *
     * @SuppressWarnings(PHPMD)
     */
    class Bar {
        function  foo() {
            $baz = 23;
        }
    }
    

    Or you can suppress one rule with an annotation like this:

    /**
     *
     */
    class Bar {
        /**
         * This will suppress UnusedLocalVariable
         * warnings in this method
         *
         * @SuppressWarnings(PHPMD.UnusedLocalVariable)
         */
        public function foo() {
            $baz = 42;
        }
    }
    

    Source https://phpmd.org/documentation/suppress-warnings.html


    Users of PHPStorm not using PHPMD can use

    /** @noinspection RULE */
    

    Where rule can be found here

    https://gist.github.com/discordier/ed4b9cba14652e7212f5