Question: When do you check input type when writing class method

Question

When do you check input type when writing class method

Answers 2
Added at 2016-12-02 21:12
Tags
Question

How would you proceed writing the code when you have multiple inputs like in this example:

public function addNameAndAge($name, $age) {
    if (false === is_string($name) {
        throw new InvalidArgumentException(...);
    }

    // ... same for age but using is_int().
}

Do we have to check each of these inputs?

I could rewrite the source code to use PHP 7's type hinting string $name and int $age which would actually help me/us, but after that we have to check also if the $name is empty or the $age is not lower or hight than xy.

This variables are really for example. I would be interested how you would guys progress in this kind of situation. Validate everything in every way, or just check the type?

Answers
nr: #1 dodano: 2016-12-02 22:12

If you want to validate arguments, in fact, it is convenient to use some validation library. You can inject it as a dependency.

The important point here, and it regards not only validation but all methods, to make a method to do one and only one thing. This is a rule that method should contain 5+/-2 lines of code.

class Person
{
    private $name;
    private $age;

    private $validator;

    public function __construct($validator)
    {
        $this->validator = $validator;
    }

    public function addNameAndAge($name, $age)
    {
        $this->setName($name)->setAge($age);

        return $this;
    }

    public function setName($name)
    {
        // perform validation here

        $this->name = $name;

        return $this;
    }

    public function setAge($age)
    {
        // perform validation here

        $this->age = $age;

        return $this;
    }
}

So if we define setting and validating operation as atomic operation this will work. But if not, we can go even further and extract validations to their own methods.

class Person
{
    private $name;
    private $age;

    private $validator;

    public function __construct($validator)
    {
        $this->validator = $validator;
    }

    public function addNameAndAge($name, $age)
    {
        $this->setName($name)->setAge($age);

        return $this;
    }

    public function setName($name)
    {
        $this->name = $name;

        $this->validateName();

        return $this;
    }

    private function validateName()
    {
        // perform validation here
    }

    public function setAge($age)
    {
        $this->age = $age;

        $this->validateAge();

        return $this;
    }

    private function validateAge()
    {
        // perform validation here
    }
}

If you careful to set properties with the setters all the time, you don't need to worry about validation except in the setters themselves.

nr: #2 dodano: 2016-12-04 05:12

(converted from comment to answer) Honestly, why reinvent the wheel. I'd use a validation library such as github.com/Respect/Validation -- and just include it via Composer.

Source Show
◀ Wstecz