function AddClass::add

Same name in other branches
  1. 3.x modules/phpunit_example/src/AddClass.php \Drupal\phpunit_example\AddClass::add()

A simple addition method with validity checking.

Parameters

int|float $a: A number to add.

int|float $b: Another number to add.

Return value

int|float The sum of $a and $b.

Throws

\InvalidArgumentException If either $a or $b is non-numeric, we can't add, so we throw.

File

modules/phpunit_example/src/AddClass.php, line 26

Class

AddClass
A class with features to show how to do unit testing.

Namespace

Drupal\phpunit_example

Code

public function add($a, $b) {
    // Check whether the arguments are numeric.
    foreach ([
        $a,
        $b,
    ] as $argument) {
        if (!is_numeric($argument)) {
            throw new \InvalidArgumentException('Arguments must be numeric.');
        }
    }
    return $a + $b;
}