Types

Typing/Types Logo

Build Job Patch Job Documentation License Issues Downloads Latest

Quality Gate Status Maintainability Rating Reliability Rating Security Rating Bugs Coverage Duplicated Lines (%) Lines of Code Technical Debt Vulnerabilities

Types is a library that provides a collection of useful primitive wrappers, similar to what other languages can do ( Java, etc). It fixes a few issues some internal functions have, and limits coercion around native PHP functions.


Below you will find information about the specific types, and the interfaces they expose.

Class Type Description
BooleanObject bool Base wrapper around primitive with some custom logical string keyword mappings.
Collection array Analogous to Doctrine collections, and implements same interfaces (including Collection interface), but provides Typed Collection functionality.
FloatObject float Provides mathematics functionality, can use 3 different drivers.
IntObject int Provides mathematics functionality, can use 3 different drivers.
StringObject string Extends Stringy using a multi-language inflector and 3rd party slugifier.
Enum* enum Extends MyCLabs/Enum and provides a static alias for toArray, syntax sugar for using in attributes.
DateTime datetime Extends Carbon with a custom wrapper that can be automatically cast to an ISO 8601 date timestamp.

*This class might be deprecated after PHP 8.1

PHP 8.1 is introducing native Enum structures and this class may no longer be needed.

Installation

Using CLI:

composer require typing/types:*@stable

Or directly on the composer.json file:

{
    "require": {
        "typing/types": "*@stable"
    }
}

See https://getcomposer.org/ for more information and documentation.

Requirements

  • PHP >= 8.0

Optional

Features

  • Rich interfaces exposing native PHP functions while fixing quirks, return values, etc.
  • Multi-byte support
  • Sane transmutation
  • Explicit boxing of variable types, with very limited coercion.

To Do

  • Symfony bundle:
    • Configurable extensions for Math in service container

What is boxing, and how does it work?

Note: Boxing is just a feature in this library, and not something you have to use.

Ever since PHP 7.4 we've been able to have strict variable typing at the class level (properties), and method/function level. However, even at the current version (8.0.x) we still cannot have it for variables outside a class, or locally when declared within class methods. This blog post shows examples of how PHP's type system is weak, even when declare(strict_types=1); is called.

There are 2 approaches to boxing. You can start with a primitive, and box it to a type, or start with a type and keep a variable boxed into that type.

Examples:

Starting as a type...

$myString = new StringObject('');
StringObject::box($myString);
$myString = 'my_new_value'; // This is still an instance of StringObject.
$myString->dasherize(); // my-new-value
$myString = new stdClass(); // TypeError.

Starting as a primitive...

$myString = 'This is my string, there are many like it, but this one is mine.';
StringObject::box($myString);
assert($myString instanceof StringObject); // true
$myString->dasherize(); // this-is-my-string...
$myString = new stdClass(); // TypeError.

In an ideal world, we would do something similar to typescript, or python and the language would auto-box:

Error:

$myString: StringObject = new StringObject();
$myString = new stdClass(); // TypeError

Auto:

$myString: StringObject = new StringObject('noop');
$myString = 'fooBarBaz';
echo $myString->dasherize(); //foo-bar-baz

License

This library is released under the MIT license. See the complete license in the LICENSE file.

Contact

Use the buttons below for issues regarding the library.

For other issues, reach out to vpassapera [at] linkedin [dot] com.