Download the PHP package forrest79/type-validator without Composer

On this page you can find all versions of the php package forrest79/type-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package type-validator

TypeValidator

Build codecov

Introduction

Validates types using PHP Doc descriptions and narrows types for PHPStan.

Imagine you're loading data from some external source. For PHP, this is mostly mixed (or some other common type like array/object), and PHPStan is unhappy with this. If data is some simple type, most of us will add something like:

Both make PHPStan happy, and you code is also tested (the first example mostly in dev environment, where the assertion is on).

But when the loaded data is a complex type like list<array{type: int, dates?: array<string, \DateTime>, validator: class-string<IValidator>}>.

Checking this at runtime and making PHPStan happy is now harder. The goal of this library is to make this as simple as assert(is_int($data)).

Use assert(is_type($data, 'list<array{type: int, dates?: array<string, \DateTime>, validator: class-string<IValidator>}>')) and the variable is really checked for the correct type at runtime, and the type is also narrowed for PHPStan.

Code coverage is computed without PHPStan extension - only the PHP runtime part.

Installation

To use this extension, require it in Composer:

You probably only want this extension for development, but it can also be used in production (omit --dev).

Using

There is one global function is_type(mixed $var, string $type) and static methods Forrest79\TypeValidator::isType(mixed $var, string $type): bool or Forrest79\TypeValidator::checkType(mixed $var, string $type): void.

All of them really check the data in $var against the type description and there is corresponding PHPStan extension so PHPStan will understand, that $var is in described type.

The function is_type(mixed $var, string $type) and method Forrest79\TypeValidator::isType(mixed $var, string $type) return a bool - true if $var matches the $type, and false otherwise.

The Method Forrest79\TypeValidator::checkType(mixed $var, string $type) has no return, but it throws a CheckException, if $var does not match the $type.

Example:

With this you can replace your @var annotations:

With:

The benefit is that variable $arr is checked for defined type.

Almost all PHPDoc types from PHPStan are supported (more information about supported types is provided later in the docs).

To use this library as PHPStan extension include extension.neon in your project's PHPStan config:

Because of PHPStan, the type description must be a static stringβ€”nothing can be generated dynamically.

Use in production

Typically, the assert function is disabled in production, so checks are only performed in development/test environments, and there is no need to distribute this library in a production environment.

But you can use this for validation also in your production code. Parsing PHPDoc types is not too performance-intensive. This library depends on phpstan/phpdoc-parser for parsing types and nikic/php-parser for detection fully qualified class names.

FQN (Fully qualified names)

Correct fully qualified names are computed from the current namespace and use statements, just like every other item in your PHP source files. However, if you use a use statement only for this library, your IDE and PHPCS may mark it as unused because they don't know about this library:

Example:

One solution is to concatenate the type string with ::class such as assert($presenter, 'class-string<\\' . Presenter::class . '>'). However, this looks very ugly. I prefer to use an FQN in the type description and omit the use statement:

Supported PHPStan - PHPDoc Types

According to https://github.com/phpstan/phpstan/blob/2.1.x/website/src/writing-php-code/phpdoc-types.md

βœ… supported 🚫 not supported - doesn't make sense for variables ❌ not supported

Basic types βœ…/🚫/❌

Classes and interfaces βœ…

Integer ranges βœ…

General arrays βœ…

Lists βœ…

Key and value types of arrays and iterables ❌

Iterables ❌ (there can be some side effect while iterate in runtime to check correct type)

Union types βœ…

Intersection types βœ…

Parentheses βœ…

self, static, parent and $this 🚫

Generics βœ…/🚫/❌ (some yes, some no, some doesn't make sense - concrete info can be found in the other types description)

Conditional return types 🚫

Utility types for generics ❌

class-string, interface-string βœ…

Global type aliases ❌

Local type aliases ❌

Array shapes βœ…

Object shapes βœ…

Literals and constants βœ…/❌

Global constants βœ…

Callables ❌ (only simple callable is supported)

Bottom type 🚫

Integer masks βœ…/❌

Offset access ❌


All versions of type-validator with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
phpstan/phpdoc-parser Version ^2.3
nikic/php-parser Version ^5.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package forrest79/type-validator contains the following files

Loading the files please wait ...