https://github.com/donatj/php-dnf-solver
PHP DNF (Disjunctive Normal Form) Signature Compatibility Solver
https://github.com/donatj/php-dnf-solver
dnf php8 type-resolution types
Last synced: 11 months ago
JSON representation
PHP DNF (Disjunctive Normal Form) Signature Compatibility Solver
- Host: GitHub
- URL: https://github.com/donatj/php-dnf-solver
- Owner: donatj
- License: mit
- Created: 2023-05-03T16:03:26.000Z (about 3 years ago)
- Default Branch: dev
- Last Pushed: 2025-04-10T04:14:23.000Z (about 1 year ago)
- Last Synced: 2025-04-10T05:16:55.525Z (about 1 year ago)
- Topics: dnf, php8, type-resolution, types
- Language: PHP
- Homepage:
- Size: 135 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PHP DNF Solver
[](https://packagist.org/packages/donatj/php-dnf-solver)
[](https://packagist.org/packages/donatj/php-dnf-solver)
[](https://github.com/donatj/php-dnf-solver/actions/workflows/ci.yml)
[](https://coveralls.io/github/donatj/php-dnf-solver)
PHP DNF (Disjunctive Normal Form) Signature Compatibility Solver - see: https://wiki.php.net/rfc/dnf_types
## Requirements
- **php**: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
## Installing
Install the latest version with:
```bash
composer require 'donatj/php-dnf-solver'
```
## Examples
### Example Parameter Satisfaction Check
```php
getParameters()[0]->getType();
$quxDnf = DNF::getFromReflectionType($quxParamType);
var_dump($quxDnf->isSatisfiedBy(
new UserDefinedType(Foo::class)
)); // true
var_dump($quxDnf->isSatisfiedBy(
new UserDefinedType(Bar::class)
)); // true
var_dump($quxDnf->isSatisfiedBy(
new UserDefinedType(Baz::class)
)); // false
```
Outputs:
```
bool(true)
bool(true)
bool(false)
```
### Example DNF Building
```php
dnf()); // A|B|C
var_dump((new OrClause(
new UserDefinedType(A::class),
new AndClause(
new UserDefinedType(B::class),
new UserDefinedType(C::class)
)
))->dnf()); // A|(B&C)
var_dump((new OrClause(
new AndClause(new UserDefinedType(A::class), new UserDefinedType(B::class)),
new AndClause(
new UserDefinedType(B::class),
new UserDefinedType(C::class)
),
new BuiltInType('null'),
))->dnf()); // (A&B)|(B&C)|null
```
Outputs:
```
string(32) "Examples\A|Examples\B|Examples\C"
string(34) "Examples\A|(Examples\B&Examples\C)"
string(52) "(Examples\A&Examples\B)|(Examples\B&Examples\C)|null"
```
## Documentation
### Class: donatj\PhpDnfSolver\DNF
#### Method: DNF::getFromReflectionType
```php
function getFromReflectionType(\ReflectionType $type) : \donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface
```
Helper to convert a ReflectionType into it's DNF representation
##### Example sources include
- ReflectionFunctionAbstract::getParameters()[…]->getType()
- ReflectionParameter::getType()
- ReflectionMethod::getReturnType()
- ReflectionProperty::getType()
---
#### Method: DNF::reflectionTypeSatisfiesReflectionType
```php
function reflectionTypeSatisfiesReflectionType(\ReflectionType $satisfyingType, \ReflectionType $satisfiedType) : bool
```
Helper to quickly check if a ReflectionType satisfies another ReflectionType
##### Parameters:
- ***\ReflectionType*** `$satisfyingType` - The type which must be satisfied (e.g. a parameter type)
- ***\ReflectionType*** `$satisfiedType` - The type which must satisfy the other (e.g. a return type)
---
#### Method: DNF::getFromVarType
```php
function getFromVarType(\ReflectionParameter|\ReflectionProperty $parameter) : \donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface|null
```
Helper to quickly get a DNF representation of a (ReflectionParameter or ReflectionProperty)'s return type
---
#### Method: DNF::getFromReturnType
```php
function getFromReturnType(\ReflectionFunctionAbstract $func) : \donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface|null
```
Helper to quickly get a DNF representation of a ReflectionFunctionAbstract (ReflectionFunction /
ReflectionMethod)'s return type
### Class: donatj\PhpDnfSolver\Exceptions\InvalidArgumentException
### Class: donatj\PhpDnfSolver\Exceptions\LogicException
### Class: donatj\PhpDnfSolver\Types\AndClause
Represents an "and clause" - a set of types which must all be satisfied - e.g. "A&B&C"
#### Method: AndClause->__construct
```php
function __construct(\donatj\PhpDnfSolver\SingularDnfTypeInterface ...$types)
```
##### Parameters:
- ***\donatj\PhpDnfSolver\SingularDnfTypeInterface*** `$types` - The list of types to be satisfied
---
#### Method: AndClause->dnf
```php
function dnf() : string
```
Return the canonical string representation of the DNF representation of this type
---
#### Method: AndClause->isSatisfiedBy
```php
function isSatisfiedBy(\donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface $value) : bool
```
Tests if this type is satisfied by the given type
For example, if this type is "A|(B&C)" and the given type matches just "A", this method returns true.
If the given type matches just "B", this method returns false.
If the given type matches "B&C", this method returns true.
---
#### Method: AndClause->count
```php
function count() : int
```
Returns the number of types in this DNF type
---
#### Method: AndClause->getTypes
```php
function getTypes() : array
```
##### Returns:
- ***\donatj\PhpDnfSolver\SingularDnfTypeInterface[]***
### Class: donatj\PhpDnfSolver\Types\BuiltInType
Represents a "built in type" as defined by ReflectionNamedType::isBuiltin()
This includes:
- int
- float
- string
- bool
- array
- iterable
#### Method: BuiltInType->__construct
```php
function __construct(string $name)
```
##### Parameters:
- ***string*** `$name` - The name of the built-in type
---
#### Method: BuiltInType->dnf
```php
function dnf() : string
```
Return the canonical string representation of the DNF representation of this type
---
#### Method: BuiltInType->getTypeName
```php
function getTypeName() : string
```
Returns the fully qualified type name of this type
---
#### Method: BuiltInType->isSatisfiedBy
```php
function isSatisfiedBy(\donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface $value) : bool
```
Tests if this type is satisfied by the given type
For example, if this type is "A|(B&C)" and the given type matches just "A", this method returns true.
If the given type matches just "B", this method returns false.
If the given type matches "B&C", this method returns true.
---
#### Method: BuiltInType->count
```php
function count() : int
```
Always 1 for singular types
Returns the number of types in this DNF type
### Class: donatj\PhpDnfSolver\Types\CallableType
Represents a "callable" type
This includes:
- callable
- Closure
- Invokable classes
#### Method: CallableType->dnf
```php
function dnf() : string
```
Return the canonical string representation of the DNF representation of this type
---
#### Method: CallableType->isSatisfiedBy
```php
function isSatisfiedBy(\donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface $value) : bool
```
Tests if this type is satisfied by the given type
For example, if this type is "A|(B&C)" and the given type matches just "A", this method returns true.
If the given type matches just "B", this method returns false.
If the given type matches "B&C", this method returns true.
---
#### Method: CallableType->getTypeName
```php
function getTypeName() : string
```
Returns the fully qualified type name of this type
---
#### Method: CallableType->count
```php
function count() : int
```
Always 1 for singular types
Returns the number of types in this DNF type
### Class: donatj\PhpDnfSolver\Types\OrClause
Represents a "or" clause - a set of types where any one of them must be satisfied - e.g. "A|B|(C&D)"
#### Method: OrClause->__construct
```php
function __construct(\donatj\PhpDnfSolver\Types\AndClause|\donatj\PhpDnfSolver\SingularDnfTypeInterface ...$types)
```
##### Parameters:
- ***\donatj\PhpDnfSolver\Types\AndClause*** | ***\donatj\PhpDnfSolver\SingularDnfTypeInterface*** `$types` - The list of types to be satisfied. Does not accept an OrClause as DNF defines that as invalid.
---
#### Method: OrClause->dnf
```php
function dnf() : string
```
Return the canonical string representation of the DNF representation of this type
---
#### Method: OrClause->isSatisfiedBy
```php
function isSatisfiedBy(\donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface $value) : bool
```
Tests if this type is satisfied by the given type
For example, if this type is "A|(B&C)" and the given type matches just "A", this method returns true.
If the given type matches just "B", this method returns false.
If the given type matches "B&C", this method returns true.
---
#### Method: OrClause->count
```php
function count() : int
```
Returns the number of types in this DNF type
---
#### Method: OrClause->getTypes
```php
function getTypes() : array
```
##### Returns:
- ***\donatj\PhpDnfSolver\Types\AndClause[]***
### Class: donatj\PhpDnfSolver\Types\UserDefinedType
Represents a "user defined type" - a class, interface, or trait, etc.
```php
__construct
```php
function __construct(string $className)
```
##### Parameters:
- ***class-string*** `$className` - The name of the class, interface, or trait to be satisfied
**Throws**: `\donatj\PhpDnfSolver\Exceptions\InvalidArgumentException` - if the user defined type does not exist after triggering registered autoloaders
---
#### Method: UserDefinedType->dnf
```php
function dnf() : string
```
Return the canonical string representation of the DNF representation of this type
---
#### Method: UserDefinedType->getTypeName
```php
function getTypeName() : string
```
Returns the fully qualified type name of this type
##### Returns:
- ***class-string***
---
#### Method: UserDefinedType->isSatisfiedBy
```php
function isSatisfiedBy(\donatj\PhpDnfSolver\SingularDnfTypeInterface|\donatj\PhpDnfSolver\NestedDnfTypeInterface $value) : bool
```
Tests if this type is satisfied by the given type
For example, if this type is "A|(B&C)" and the given type matches just "A", this method returns true.
If the given type matches just "B", this method returns false.
If the given type matches "B&C", this method returns true.
---
#### Method: UserDefinedType->count
```php
function count() : int
```
Always 1 for singular types
Returns the number of types in this DNF type