coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

Ignore some case for EarlyExit rule

Open VincentLanglet opened this issue 2 years ago • 1 comments

I have the following method

public function foo(Collection $collection)
{
     $collection->add($this->bar);
     $collection->add($this->bar);

     if ($collection instanceof BOOM) {
          $collection->add($this->some)
     }
}

which reports an error because I didn't use an early exit.

But changing

     if ($collection instanceof BOOM) {
          $collection->add($this->some)
     }

to

     if (! $collection instanceof BOOM) {
          return;
     }

     $collection->add($this->some)

is taking more lines and not really reducing complexity.

Also, I may change later to

public function foo(Collection $collection)
{
     $collection->add($this->bar);
     $collection->add($this->bar);

     if ($collection instanceof Boom) {
          $collection->add($this->some)
     }

     if ($collection instanceof Please) {
          $collection->add($this->thanks)
     }
}

and so on.

The slevomat Early Exit standard has some configuration available https://github.com/slevomat/coding-standard#slevomatcodingstandardcontrolstructuresearlyexit-

I would say it's better to use some.

VincentLanglet avatar Aug 11 '21 21:08 VincentLanglet

I had no idea it had any configuration. Right now I do this 🙃

public function foo(Collection $collection)
{
     $collection->add($this->bar);
     $collection->add($this->bar);

     if ($collection instanceof BOOM) {
          $collection->add($this->some)
     }

     return;
}

simPod avatar Aug 12 '21 06:08 simPod