PHPackages                             eloquent/typhax - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. eloquent/typhax

AbandonedArchivedLibrary[Parsing &amp; Serialization](/categories/parsing)

eloquent/typhax
===============

A flexible PHP type hinting syntax.

0.10.1(10y ago)133.8k[3 issues](https://github.com/eloquent/typhax/issues)1MITPHPPHP &gt;=5.3

Since Mar 3Pushed 10y ago1 watchersCompare

[ Source](https://github.com/eloquent/typhax)[ Packagist](https://packagist.org/packages/eloquent/typhax)[ Docs](https://github.com/eloquent/typhax)[ RSS](/packages/eloquent-typhax/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (23)Used By (1)

Typhax
======

[](#typhax)

*A flexible PHP type hinting syntax.*

[![Current version image](https://camo.githubusercontent.com/70988107da03ff580ca79e92f835fc96b72f80018dcdacfafa334baa880472f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6f7175656e742f7479706861782e7376673f7374796c653d666c61742d737175617265 "This project uses semantic versioning")](https://packagist.org/packages/eloquent/typhax)[![Current build status image](https://camo.githubusercontent.com/60e656566b4830ba244e9459403923fc45fc67134b374fced8744624bb2ec475/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f656c6f7175656e742f7479706861782f646576656c6f702e7376673f7374796c653d666c61742d737175617265 "Current build status for the develop branch")](https://travis-ci.org/eloquent/typhax)[![Current coverage status image](https://camo.githubusercontent.com/48a99675d921a5a098752ad85fc2e6315c2fbbd5362006259e059e5e1283e91a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f656c6f7175656e742f7479706861782f646576656c6f702e7376673f7374796c653d666c61742d737175617265 "Current test coverage for the develop branch")](https://codecov.io/github/eloquent/typhax)

Installation and documentation
------------------------------

[](#installation-and-documentation)

- Available as [Composer](http://getcomposer.org/) package [eloquent/typhax](https://packagist.org/packages/eloquent/typhax).
- [API documentation](http://lqnt.co/typhax/artifacts/documentation/api/) available.

What is Typhax?
---------------

[](#what-is-typhax)

Typhax is a standard for the specification of PHP types in parameter type hints, and anywhere a type needs to be described in a human-readable form.

It expands on the capabilities of existing conventions for specifying type requirements, such as those used in the [PHP documentation](http://php.net/manual/en/language.types.php) and PHPDoc's [@param tag](http://www.phpdoc.org/docs/latest/references/phpdoc/tags/param.html).

In addition to scalar and class type hints, Typhax allows powerful features, including the specification of key and value types for arrays, as well as compound types that use boolean logic (e.g. integer|float).

Supported types
---------------

[](#supported-types)

### Array

[](#array)

```
array
array

```

An [array](http://php.net/array). The key type and value type can optionally be specified. See the section on [traversable types](#traversable-types) below.

Equivalent to the [is\_array()](http://php.net/is_array) function.

### Boolean

[](#boolean)

```
boolean

```

A [boolean](http://php.net/boolean) true or false value. The value must be an actual boolean, and not an equivalent integer.

Equivalent to the [is\_bool()](http://php.net/is_bool) function.

### Callable

[](#callable)

```
callable

```

A user-defined callback function. This is exactly equivalent to the [callable](http://php.net/manual/en/language.types.callable.php)type hint introduced in PHP 5.4.

A callable can be any of the following:

- A string containing the name of a function or static class method.
- An array with an object at index 0 and the method name at index 1.
- An array with a class name at index 0 and the method name at index 1.
- A [closure](http://php.net/manual/en/functions.anonymous.php).
- An object with an [\_\_invoke()](http://php.net/manual/en/language.oop5.magic.php#object.invoke) method.
- The result of a [create\_function()](http://php.net/create_function) call.

Equivalent to the [is\_callable()](http://php.net/is_callable) function.

### Float

[](#float)

```
float

```

A [floating-point number](http://php.net/float). The value must be a true floating point number, and not an equivalent string or integer.

Equivalent to the [is\_float()](http://php.net/is_float) function.

### Integer

[](#integer)

```
integer

```

An [integer](http://php.net/integer). The value must be a true integer, and not an equivalent string, boolean, or any other kind of value.

Equivalent to the [is\_int()](http://php.net/is_int) function.

### Mixed

[](#mixed)

```
mixed
mixed

```

The mixed type accepts any value of any type (including null).

Mixed can be treated as a [traversable type](#traversable-types), as in the second example above. When used in this fashion, mixed indicates any type that can be traversed, such as an [array](http://php.net/array) or an instance of the [Traversable](http://php.net/traversable)interface. This is useful in the case that the value must be a collection, but the outer type is not important.

### Null

[](#null)

```
null

```

A [null](http://php.net/manual/en/language.types.null.php) value.

Equivalent to `=== null`.

### Object

[](#object)

```
object
ClassName
ClassName

```

The first form, `object`, indicates a value that is an [object](http://www.php.net/manual/en/language.oop5.php) of *any* class. This is equivalent to the [is\_object()](http://php.net/is_object) function.

The second form, `ClassName`, indicates a value that is an [object](http://www.php.net/manual/en/language.oop5.php) of class `ClassName`. This includes instances of `ClassName`, instances of objects whose class extends from `ClassName`, and instances of objects that implement the `ClassName` interface. This is equivalent to the [instanceof](http://php.net/manual/en/language.operators.type.php) operator.

The third form, `ClassName`, follows the same rules as the second form, with the additional requirement that the object must implement the [Traversable](http://php.net/traversable) interface. The key type and value type can optionally be specified. See the section on [traversable types](#traversable-types) below.

#### Regarding namespaces

[](#regarding-namespaces)

Namespace resolution should follow the same rules as PHP source code. That is; if the class name is in the current namespace, or has a relevant use statement, it's okay to use the short version.

[Cosmos](https://github.com/eloquent/cosmos) can be used to aid in resolving class names at runtime.

### Resource

[](#resource)

```
resource
resource{ofType:resourceType}

```

The first form, `resource`, indicates a value that is a [resource](http://php.net/resource) of *any*type. This is equivalent to the [is\_resource()](http://php.net/is_resource) function.

The second form, `resource{ofType:resourceType}`, indicates a value that is a [resource](http://php.net/resource) that returns a string equal to 'resourceType' when passed to [get\_resource\_type()](http://php.net/get_resource_type).

### Stream

[](#stream)

```
stream
stream{readable:true,writable:true}
stream{readable:true,writable:false}
stream{readable:false,writable:true}

```

Represents a [stream](http://php.net/stream) resource. The **readable** and **writable** attributes determine the requirements for the **mode** of the stream.

The stream type is equvalent to the Typhax type `resource{ofType:stream}`.

See the PHP documentation for [fopen()](http://php.net/fopen) for more information about stream modes.

### String

[](#string)

```
string

```

A [string](http://php.net/string). The value must be a true string, and not any other type that can be converted to a string.

Equivalent to the [is\_string()](http://php.net/is_string) function.

### Stringable

[](#stringable)

```
stringable

```

Represents a value of any type that can be converted to a *useful* string representation. This includes strings, integers, floats, and objects that have a `__toString()` method.

Arrays, booleans, nulls, resources, and objects *without* a `__toString()`method do not qualify as 'stringable'.

### Tuple

[](#tuple)

```
tuple

```

A [tuple](http://en.wikipedia.org/wiki/Tuple) is an array value of a fixed size, with each element being of a specific type.

Tuples are generally referred to as an *n*-tuple, where *n* is the number of elements. For example, a 2-tuple definition where the first element is a string, and the second element is an integer, looks like `tuple`.

Tuple arrays must be sequential. That is, the keys of the array must be integers, with the first key being 0, and subsequent keys incrementing by 1 for each element of the tuple.

As an example, `['foo', 1]` satisfies the constraint `tuple`.

### Legacy types

[](#legacy-types)

The following types are also implemented, but are considered deprecated. They exist primarily for compatibility with types and pseudo-types used in the PHP manual, and an effort should be made to avoid their use:

- `bool` = `boolean`
- `callback` = `callable`
- `double` = `float`
- `int` = `integer`
- `long` = `integer`
- `number` = `integer|float`
- `numeric` = equivalent to [is\_numeric()](http://php.net/is_numeric)
- `real` = `float`
- `scalar` = `integer|float|string|boolean`

Traversable types
-----------------

[](#traversable-types)

Typhax supports the specification of key and value types for arrays and [Traversable](http://php.net/traversable) objects.

The specification for key and value types is as follows:

```
primaryType

```

This specification represents a value of type `primaryType` which, when iterated over, produces keys of type `keyType` and values of type `valueType`.

### Omitting a traversable type's key type

[](#omitting-a-traversable-types-key-type)

The `keyType` may be omitted:

```
primaryType

```

This specification represents a value of type `primaryType` which, when iterated over, produces **sequential integer keys starting with 0**, and values of type `valueType`.

### Using `array` without specifying key and value types

[](#using-array-without-specifying-key-and-value-types)

For `array`, both the key, and value types can be omitted:

```
array

```

This specification is equivalent to:

```
array

```

Meaning that the keys and values can be of any type.

### Using `mixed` as the primary type in a traversable type

[](#using-mixed-as-the-primary-type-in-a-traversable-type)

A traversable type may specify `mixed` as the primary type:

```
mixed

```

This specification represents a value of *any* type which can be iterated over, producing keys of type `keyType` and values of type `valueType`.

Boolean type logic
------------------

[](#boolean-type-logic)

Typhax supports boolean logic in type specifications. There are two operators, the pipe symbol (|) which represents boolean OR, and the plus symbol (+) which represents boolean AND.

This specification:

```
typeA|typeB

```

represents a type that is either of type `typeA` OR of type `typeB`. A real-world example might be `integer|float` to accept either an integer OR float number.

This specification:

```
typeA+typeB

```

represents a type that is both of type `typeA` AND of type `typeB`. A real-world example might be `InterfaceA+InterfaceB` to accept only an object that implements both `InterfaceA` AND `InterfaceB`.

### Extension types

[](#extension-types)

```
:ClassName
:ClassName{attribute:value,...}
:ClassName{attribute:value,...}

```

Extensions provide a means to expand the capabilies of Typhax with custom logic.

White space
-----------

[](#white-space)

In general, Typhax does not care whether white space is used in type specifications. However, the above documentation should serve as a recommended style guide.

Usage
-----

[](#usage)

*Typhax* includes a parser for type expressions, which produces a syntax tree of the parsed type:

```
use Eloquent\Typhax\Parser\TypeParser;
use Eloquent\Typhax\Renderer\CondensedTypeRenderer;

$parser = TypeParser::create();
$type = $parser->parse('primaryType');

$renderer = CondensedTypeRenderer::create();
echo $renderer->render($type); // outputs 'primaryType'
```

Also included is a comparator for determining whether two types are equivalent:

```
use Eloquent\Typhax\Comparator\TypeEquivalenceComparator;

$typeA = $parser->parse('integer|string');
$typeB = $parser->parse('string|integer');
$typeC = $parser->parse('string|integer|null');

$comparator = TypeEquivalenceComparator::create();
var_dump($comparator->isEquivalent($typeA, $typeB)); // outputs 'bool(true)'
var_dump($comparator->isEquivalent($typeB, $typeC)); // outputs 'bool(false)'
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~71 days

Recently: every ~256 days

Total

20

Last Release

3838d ago

PHP version history (2 changes)0.1.0PHP &gt;=5.3.0

0.10.0PHP &gt;=5.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/100152?v=4)[Erin](/maintainers/ezzatron)[@ezzatron](https://github.com/ezzatron)

---

Top Contributors

[![ezzatron](https://avatars.githubusercontent.com/u/100152?v=4)](https://github.com/ezzatron "ezzatron (67 commits)")[![jmalloc](https://avatars.githubusercontent.com/u/761536?v=4)](https://github.com/jmalloc "jmalloc (13 commits)")

---

Tags

typecheckparsersyntaxhinthintingchecking

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/eloquent-typhax/health.svg)

```
[![Health](https://phpackages.com/badges/eloquent-typhax/health.svg)](https://phpackages.com/packages/eloquent-typhax)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
