You are here

class AddClass in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 phpunit_example/src/AddClass.php \Drupal\phpunit_example\AddClass

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

Hierarchy

Expanded class hierarchy of AddClass

Related topics

1 file declares its use of AddClass
AddClassTest.php in modules/phpunit_example/tests/src/Unit/AddClassTest.php

File

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

Namespace

Drupal\phpunit_example
View source
class AddClass {

  /**
   * A simple addition method with validity checking.
   *
   * @param int|float $a
   *   A number to add.
   * @param int|float $b
   *   Another number to add.
   *
   * @return 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.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddClass::add public function A simple addition method with validity checking.