Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/attitude/duck-types-php

If it walks like a duck and talks like a duck, treat it like a duck, even if it’s not a duck — a dynamic typing for PHP inspired by Flow types
https://github.com/attitude/duck-types-php

dynamic-type dynamic-types dynamic-typing experimental php php-library php7 type-safety typesafety

Last synced: about 6 hours ago
JSON representation

If it walks like a duck and talks like a duck, treat it like a duck, even if it’s not a duck — a dynamic typing for PHP inspired by Flow types

Awesome Lists containing this project

README

        

Duck Types for PHP
==================

If it walks like a duck and talks like a duck, treat it like a duck, even if it’s not a duck — a dynamic typing for PHP inspired by [Flow](https://flow.org/en/) types.

This tool let's you use basic [FLow annotation syntax](https://flow.org/en/docs/types/) to check data flowing through it. It can generate validation [\Closures](https://www.php.net/manual/en/class.closure) (validator) from a Flow annotation.

[Read the full story…](https://martinadamko.medium.com/asserts-with-flow-annotations-in-php7-83126b5f58ab)

```php
` utility type](https://flow.org/en/docs/types/utilities/#toc-exact)
- [ ] Publish asserts for at least some cases

---
---
---

## About

This project is an **experiment turned into a dev-tool**. It is meant to be used during the development and should not be be used in production, so make sure to disable the *duck* validations in production using constant:

```php
define('DUCK_TYPE_VAlIDATION_IS_ENABLED', false);
```

After disabling the validation, value **just flows through** the `Type::pass()` method.

You're not even bound to use the "pass" variable method. It's here to serve as an inspiration and you are encouraged to use your own way how to use this library.

You can just use the `Annotation` methods to `parse()` (some) Flow logic syntax, or use the `compile()` method to generate \Closure validators.

---

Creating a custom reusable type
-------------------------------

By registering a type alias you can make it awailable to the type system for later reuse.

A registered type can be later
- used in annotations;
- made nullable prepending `'?'` before the type alias

```php
assert ( mixed $assertion [, Throwable $exception ] ) : bool
> - Checks if assertion is FALSE
> - $assertion in PHP 7 may also be **any expression that returns a value**, which will be executed and the result used to indicate whether the assertion succeeded or failed.

Source: [php.net](https://www.php.net/manual/en/function.assert.php#function.assert.expectations)

An expression can be a function call, so let's use that:

```php
getMessages());

```

---

Validator `\Closures`
--------------------

Validator is an anonymous functions accepting one optional parameter that throws `IncompatibleTypeError` when the validation fails. Validator is required to return `bool` type also to comply with [assert()](https://www.php.net/manual/en/function.assert.php).

Optional parameter allows closure to pass `null | undefined` to cover scenarios when the value is missing and to control the thrown more meaningful message thatn PHP's built-in one.

### Validator example:

```php