Type Assertions
Type assertions verify that values match expected PHP types.
Available Assertions
Section titled “Available Assertions”integer()
Section titled “integer()”Assert that a value is a PHP integer.
use Cline\Assert\Assertions\Assertion;
Assertion::integer($count);Assertion::integer($id, 'ID must be an integer');float()
Section titled “float()”Assert that a value is a PHP float.
Assertion::float($price);Assertion::float($temperature, 'Temperature must be a float');string()
Section titled “string()”Assert that a value is a string.
Assertion::string($name);Assertion::string($email, 'Email must be a string');boolean()
Section titled “boolean()”Assert that a value is a PHP boolean.
Assertion::boolean($isActive);Assertion::boolean($flag, 'Flag must be a boolean');numeric()
Section titled “numeric()”Assert that a value is numeric (int, float, or numeric string).
Assertion::numeric($value);Assertion::numeric($amount, 'Amount must be numeric');integerish()
Section titled “integerish()”Assert that a value is an integer or can be cast to an integer.
Assertion::integerish('123'); // PassesAssertion::integerish(123); // PassesAssertion::integerish('123.0'); // Failsscalar()
Section titled “scalar()”Assert that a value is a PHP scalar.
Assertion::scalar($value);Assertion::scalar($input, 'Input must be scalar');isArray()
Section titled “isArray()”Assert that a value is an array.
Assertion::isArray($items);Assertion::isArray($config, 'Config must be an array');isObject()
Section titled “isObject()”Assert that a value is an object.
Assertion::isObject($instance);Assertion::isObject($model, 'Model must be an object');isResource()
Section titled “isResource()”Assert that a value is a resource.
$file = fopen('file.txt', 'r');Assertion::isResource($file);isCallable()
Section titled “isCallable()”Assert that a value is callable.
Assertion::isCallable($callback);Assertion::isCallable(fn() => true);Assertion::isCallable('strlen');Assertion::isCallable([$object, 'method']);Chaining Type Assertions
Section titled “Chaining Type Assertions”use Cline\Assert\Assert;
Assert::that($age) ->integer() ->greaterThan(0);
Assert::that($name) ->string() ->notEmpty();Common Patterns
Section titled “Common Patterns”Input Validation
Section titled “Input Validation”Assert::that($userId) ->integer('User ID must be an integer') ->greaterThan(0, 'User ID must be positive');Numeric Type Checking
Section titled “Numeric Type Checking”// Strict integer checkAssert::that($count)->integer();
// Flexible numeric checkAssert::that($amount)->numeric();
// Integer-like checkAssert::that($id)->integerish();Type Coercion vs Strict Types
Section titled “Type Coercion vs Strict Types”Strict Type Checking
Section titled “Strict Type Checking”Assertion::integer(123); // PassAssertion::integer('123'); // FailAssertion::integer(123.0); // FailFlexible Type Checking
Section titled “Flexible Type Checking”Assertion::numeric(123); // PassAssertion::numeric('123'); // PassAssertion::numeric(123.45); // Pass
Assertion::integerish(123); // PassAssertion::integerish('123'); // PassAssertion::integerish(123.0); // FailBest Practices
Section titled “Best Practices”Be Specific
Section titled “Be Specific”// Too looseAssertion::scalar($age);
// SpecificAssertion::integer($age);Chain Related Assertions
Section titled “Chain Related Assertions”Assert::that($email) ->string() ->notEmpty() ->email();Use Integerish for Flexible Input
Section titled “Use Integerish for Flexible Input”Assert::that($_POST['quantity']) ->integerish() ->greaterThan(0);Next Steps
Section titled “Next Steps”- Numeric Assertions - Number validation
- Object Assertions - Object validation
- Array Assertions - Array validation