hamcrest/hamcrest-php · error · InvalidArgumentException

assertThat() requires one to three arguments

Error message

assertThat() requires one to three arguments

What it means

MatcherAssert::assertThat() only supports signatures with one, two, or three arguments; any other argument count (0, 4+) falls into the default branch and throws InvalidArgumentException. It is a guard against calling a variadic-style assertion with the wrong arity.

Solutions

  1. Call assertThat with 1-3 args: (matcher), (actual, matcher), or (reason, actual, matcher)
  2. Remove extra arguments and assert them separately
  3. If args are dynamic, validate count($args) between 1 and 3 before calling

Example fix

// before
assertThat($reason, $actual, $matcher, $extra);
// after
assertThat($reason, $actual, $matcher);
assertThat($extra, anything());
Defensive patterns

Strategy: validation

Validate before calling

if (count($args) < 1 || count($args) > 3) {
    throw new PreconditionException('assertThat needs 1-3 args');
}

Try / catch

try {
    assertThat(...$args);
} catch (\InvalidArgumentException $e) {
    if ($e->getMessage() !== 'assertThat() requires one to three arguments') throw $e;
    throw new BadMethodCallException('Wrong assertThat arity: '.count($args));
}

Prevention

When it happens

Trigger: Calling assertThat() with zero arguments, or four or more arguments (e.g. assertThat($desc, $actual, $matcher, $extra)).

Common situations: Refactoring signatures and leaving a stray extra argument; dynamic call_user_func_array with an unexpected-sized args array; forgetting the actual value entirely.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of hamcrest/hamcrest-php@aa726aeff9 (2026-09-15). Data as JSON: /api/errors/2967f534dcbb62fc. Report an issue: GitHub.

Appendix: source

Thrown at hamcrest/Hamcrest/MatcherAssert.php:68

                self::$_count++;
                if ($args[1] instanceof Matcher) {
                    self::doAssert('', $args[0], $args[1]);
                } elseif (!$args[1]) {
                    throw new AssertionError($args[0]);
                }
                break;

            case 3:
                self::$_count++;
                self::doAssert(
                    $args[0],
                    $args[1],
                    Util::wrapValueWithIsEqual($args[2])
                );
                break;

            default:
                throw new \InvalidArgumentException('assertThat() requires one to three arguments');
        }
    }

    /**
     * Returns the number of assertions performed.
     *
     * @return int
     */
    public static function getCount(): int
    {
        return self::$_count;
    }

    /**
     * Resets the number of assertions performed to zero.
     */
    public static function resetCount(): void
    {

View on GitHub (pinned to aa726aeff9)