Phpinspectionsea: [optional, opinionated] Report multiple returns in class methods (ignore traits)

Created on 24 Mar 2017  ·  10Comments  ·  Source: kalessil/phpinspectionsea

? static methods ?

enhancement fixed

Most helpful comment

All fine if it is disabled by default, and available for those who really need it.

For any user arriving here in search of more info on this, please go through this Stackoverflow thread where people discuss this topic in extreme length. There is lots of great literature being quoted from (including famous clean coders like Steve McConnell, Martin Fowler, Kent Beck, … of which I recommend every single one to buy and read). 🐱

All 10 comments

@funivan : thanks for a link ;)

Emmm.. what link? ))

And I'm curious if you are talking about methods like this:

    /**
     * Cast an attribute to a native PHP type.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return mixed
     */
    protected function castAttribute($key, $value)
    {
        if (is_null($value)) {
            return $value;
        }
        switch ($this->getCastType($key)) {
            case 'int':
            case 'integer':
                return (int) $value;
            case 'real':
            case 'float':
            case 'double':
                return (float) $value;
            case 'string':
                return (string) $value;
            case 'bool':
            case 'boolean':
                return (bool) $value;
            case 'object':
                return $this->fromJson($value, true);
            case 'array':
            case 'json':
                return $this->fromJson($value);
            case 'collection':
                return new BaseCollection($this->fromJson($value));
            case 'date':
                return $this->asDate($value);
            case 'datetime':
                return $this->asDateTime($value);
            case 'timestamp':
                return $this->asTimestamp($value);
            default:
                return $value;
        }
    }

If yes, which should be the solution?

There is absolutely nothing wrong with multiple return statements!?!

@Fleshgrinder : no worries, it's optional tool for myself.

The rule indicates methods written in a procedural style, but can be simplified.
Such methods harder to test and might be a good candidate for more precise review.

@rentalhost : same answer as above =)

All fine if it is disabled by default, and available for those who really need it.

For any user arriving here in search of more info on this, please go through this Stackoverflow thread where people discuss this topic in extreme length. There is lots of great literature being quoted from (including famous clean coders like Steve McConnell, Martin Fowler, Kent Beck, … of which I recommend every single one to buy and read). 🐱

@kalessil yep. After this video I started to look at my code from another side. Single return has pros and cons, but having such inspection would be great.
I see this inspection simple, if you would like I can implement it =) What do you think?

@funivan, go ahead =)

Was this page helpful?
0 / 5 - 0 ratings