vancodx/php-data-assertion

VanCodX PHP Data Assertion

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/vancodx/php-data-assertion

1.0.2 2025-11-24 07:02 UTC

This package is auto-updated.

Last update: 2025-11-25 18:45:43 UTC


README

This library contains a set of static methods for assertion the type of variables and arguments of functions and methods. The main goal is to improve the quality of data validation and static analysis. Each assertion method has "@phpstan-assert" tag in its DocBlock comment to narrow the type of "value" argument.

This library also includes my PHP Data Validation library.

Installation

Install this package with the following command:

composer require vancodx/php-data-assertion

Usage

Example of assertion the type of a function argument:

<?php declare(strict_types=1);

use VanCodX\Data\Assertion\Assertion as A;

/**
 * @param list<int> $ids
 * @return void
 */
function printIds(array $ids): void
{
    A::argIsListLenOfIntId($ids);
    \PHPStan\dumpType($ids); // Reports: Dumped type: non-empty-list<int<1, max>>
    echo 'IDs: ', implode(', ', $ids), "\n";
}

Example of assertion the type of a variable:

<?php declare(strict_types=1);

use VanCodX\Data\Assertion\Assertion as A;

/**
 * @return void
 */
function printUsername(): void
{
    $username = getenv('USER');
    A::valIsStrLen($username);
    \PHPStan\dumpType($username); // Reports: Dumped type: non-empty-string
    echo 'Username: ', $username, "\n";
}

Running tests

Use the following command for running the tests inside a Docker container:

docker compose up --build tests