function AddClass::add

Same name in this branch
  1. 4.0.x modules/phpunit_example/src/AddClass.php \Drupal\phpunit_example\AddClass::add()
Same name in other branches
  1. 3.x modules/phpunit_example/src/AddClass.php \Drupal\phpunit_example\AddClass::add()
  2. 8.x-1.x 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/testing_example/src/AddClass.php, line 26

Class

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

Namespace

Drupal\testing_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;
}