0

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

alariva
  • 2,051
  • 1
  • 22
  • 37
  • In case you are working with PHPStorm, just set it in a way so this file (or method) is excluded from PHPMD... nothing is perfect. Its going to append special phpdocblock on top so no other developer has same warnings. – Kyslik Dec 04 '16 at 12:25
  • Thanks, @Kyslik. So is this phpdocblock interpreted by phpmd? In that case it looks like fair enough, since you are anyway being explicit about the code intention (variables declaration). – alariva Dec 04 '16 at 12:31
  • Just use this `/** @noinspection PhpUndefinedVariableInspection */` above $this->transmail ... and detector will just ignore it (or better said: phpstorm will not allow run inspection on next statement) – Kyslik Dec 04 '16 at 12:39
  • @Kyslik thanks, I will try that out and post back – alariva Dec 04 '16 at 12:40
  • Hold on, take a look at this list, https://gist.github.com/discordier/ed4b9cba14652e7212f5 I am testing it right now but it seems phpmd still reports issues. – Kyslik Dec 04 '16 at 12:41
  • Ok. Just one thing, Im not using phpstorm, these errors are raised by the phpmd inspections run on CodeClimate, i'd be particularly interested in taking effect there. – alariva Dec 04 '16 at 12:43

1 Answers1

1

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

Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • So this is [the updated code](https://github.com/timegridio/timegrid/commit/d29096d61aa49ca6ffecc581128154e635488989). Thanks @Kyslik – alariva Dec 05 '16 at 01:54