Skip to content

Type Assertions

Type assertions verify that values match expected PHP types.

Assert that a value is a PHP integer.

use Cline\Assert\Assertions\Assertion;
Assertion::integer($count);
Assertion::integer($id, 'ID must be an integer');

Assert that a value is a PHP float.

Assertion::float($price);
Assertion::float($temperature, 'Temperature must be a float');

Assert that a value is a string.

Assertion::string($name);
Assertion::string($email, 'Email must be a string');

Assert that a value is a PHP boolean.

Assertion::boolean($isActive);
Assertion::boolean($flag, 'Flag must be a boolean');

Assert that a value is numeric (int, float, or numeric string).

Assertion::numeric($value);
Assertion::numeric($amount, 'Amount must be numeric');

Assert that a value is an integer or can be cast to an integer.

Assertion::integerish('123'); // Passes
Assertion::integerish(123); // Passes
Assertion::integerish('123.0'); // Fails

Assert that a value is a PHP scalar.

Assertion::scalar($value);
Assertion::scalar($input, 'Input must be scalar');

Assert that a value is an array.

Assertion::isArray($items);
Assertion::isArray($config, 'Config must be an array');

Assert that a value is an object.

Assertion::isObject($instance);
Assertion::isObject($model, 'Model must be an object');

Assert that a value is a resource.

$file = fopen('file.txt', 'r');
Assertion::isResource($file);

Assert that a value is callable.

Assertion::isCallable($callback);
Assertion::isCallable(fn() => true);
Assertion::isCallable('strlen');
Assertion::isCallable([$object, 'method']);
use Cline\Assert\Assert;
Assert::that($age)
->integer()
->greaterThan(0);
Assert::that($name)
->string()
->notEmpty();
Assert::that($userId)
->integer('User ID must be an integer')
->greaterThan(0, 'User ID must be positive');
// Strict integer check
Assert::that($count)->integer();
// Flexible numeric check
Assert::that($amount)->numeric();
// Integer-like check
Assert::that($id)->integerish();
Assertion::integer(123); // Pass
Assertion::integer('123'); // Fail
Assertion::integer(123.0); // Fail
Assertion::numeric(123); // Pass
Assertion::numeric('123'); // Pass
Assertion::numeric(123.45); // Pass
Assertion::integerish(123); // Pass
Assertion::integerish('123'); // Pass
Assertion::integerish(123.0); // Fail
// Too loose
Assertion::scalar($age);
// Specific
Assertion::integer($age);
Assert::that($email)
->string()
->notEmpty()
->email();
Assert::that($_POST['quantity'])
->integerish()
->greaterThan(0);