All Projects → azjezz → typed

azjezz / typed

Licence: MIT License
Typed variables for PHP 7.4+ ( don't use this please )

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to typed

typed flags
Type-safe and human-readable set of bool flags
Stars: ✭ 23 (-65.15%)
Mutual labels:  type-safety
type-lite
type - Strong types for C++98, C++11 and later in a single-file header-only library
Stars: ✭ 25 (-62.12%)
Mutual labels:  type-safety
coalton
Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.
Stars: ✭ 439 (+565.15%)
Mutual labels:  type-safety
playwarts
WartRemover warts for Play Framework.
Stars: ✭ 23 (-65.15%)
Mutual labels:  type-safety
genTypeScript
Auto generation of type-safe bindings between Reason and Typescript.
Stars: ✭ 75 (+13.64%)
Mutual labels:  type-safety
simplepie-ng
Don't use this yet.
Stars: ✭ 41 (-37.88%)
Mutual labels:  php74
tsafe
🔩 The missing TypeScript utils
Stars: ✭ 285 (+331.82%)
Mutual labels:  type-safety
magento2-fast-vm
Optimal vagrant developer box for Magento2. Folders synced by nfs/rsync. This box includes Magento developer utilities.
Stars: ✭ 89 (+34.85%)
Mutual labels:  php74
pasvl
Array Validator (regular expressions for arrays, sort of)
Stars: ✭ 40 (-39.39%)
Mutual labels:  php74
eslint-plugin-total-functions
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.
Stars: ✭ 72 (+9.09%)
Mutual labels:  type-safety
yants
Yet Another Nix Type System | Source has moved to https://git.tazj.in/tree/nix/yants
Stars: ✭ 35 (-46.97%)
Mutual labels:  type-safety
php-currency-api
Standardized wrapper for popular currency rate APIs. Currently supports FixerIO, CurrencyLayer, Open Exchange Rates and Exchange Rates API.
Stars: ✭ 17 (-74.24%)
Mutual labels:  php74
docker-php-fpm-7.4
PHP-FPM 7.4 Docker Image (base image only)
Stars: ✭ 69 (+4.55%)
Mutual labels:  php74
sniff
Simpler PHP code sniffer built on top of PHP-CS-Fixer.
Stars: ✭ 14 (-78.79%)
Mutual labels:  psr-1
php-json-schema-model-generator
Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
Stars: ✭ 36 (-45.45%)
Mutual labels:  php74
PolyCast
Safely cast values to int, float, or string in PHP
Stars: ✭ 52 (-21.21%)
Mutual labels:  type-safety
HashedExpression
Type-safe modelling DSL, symbolic transformation, and code generation for solving optimization problems.
Stars: ✭ 40 (-39.39%)
Mutual labels:  type-safety
php-rdkafka-ffi
PHP Kafka client - binding librdkafka via FFI
Stars: ✭ 49 (-25.76%)
Mutual labels:  php74
llvm-hs-typed
Type Safe LLVM IR ( Experimental )
Stars: ✭ 47 (-28.79%)
Mutual labels:  type-safety
mdserver-web
Simple Linux Panel
Stars: ✭ 1,064 (+1512.12%)
Mutual labels:  php74

Typed

This library allows you to create typed variables in PHP 7.4 +

Composer installation :

$ composer require azjezz/typed

Usage Example :

<?php declare(strict_types=1);

use Typed as t;

$name = &t\string('saif');
$age = &t\int(19);

try {

  $name = 'azjezz'; // works
  $name = 15; // fails
  $age = null; // fails

} catch(TypeError $error) {
   
} finally {
    t\clean();
}

callable => func, array => { arr, keyset, vector, set }

Since callable and array cant be used for function names, instead i have used func for callable and arr for array. keyset, vector and set are just helper to help you create an array of keys ( keyset = arr(array_keys($value)) ), an array of values ( vector = arr(array_values($value)) ) and an array of unique values ( set = arr(array_unique($value)) ).

typed

typed() is a function to help you create a typed variables based on the type of the passed variable, currently it supports string, int, float, bool, object and iterable. it was suggested by @mikesherov on twitter.

purge, clean, delete and c

As mentioned here and here, memory leak is a huge issue here, if a specific function created a typed variable, the variable will still exist inside the repository even after execution, for this i made some helper functions that allow you to remove variables from the repository.

  • Typed\purge() will delete every reference registered in the repository, you can call this function at the end of every request/response cycle in a web application per example.
  • Typed\clean(string $namespace = 'default') this function will delete every reference to a typed variable in a specific namespace as shown in the example above, its recommended to use a unique namespace every time you use typed variables and call clean() to ensure there's no memory leak.
  • Typed\filter(mixed $value, string $namespace = 'default') this function will delete the every created reference to a typed variable with the given value in a specific namespace.
  • Typed\delete(mixed $value, string $namespace = 'default') similar to what Typed\filter does, this function allows you to delete the last created reference to a typed variable with the given value in a specific namespace. example :
<?php declare(strict_types=1);

use Typed as t;
    
$name = &t\string('azjezz', 'data');
$age = &t\int(19, 'data');

t\delete($name, 'data');

/**
 * the reference to the `$name` variable has been deleted.
 * therefor we can assign any type to the variable `$name` now. 
 */
$name = []; // works
  • Typed\c this function accepted a callable that take no arguments, the callable will be called inside c ( container ) and all the assigned typed variables in the time of the call would be deleted afterward, this makes it easier to ensure that function calls don't cause any memory leak. example :
<?php declare(strict_types=1);

use Typed as t;
use function Typed\c;

/**
 * all assigned variables inside the callable will be destroyed after execution. 
 */
c(function(): void {
    $name = &t\string('saif eddin');
    $age = &t\int(5);
    $arr = &t\arr([
        'age' => $age,
        'name' => $name    
    ]);
});

More :

A cool PSR-15 Middleware to delete all typed variables created inside the next middleware/handler

<?php declare(strict_types=1);

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use function Typed\c;

class TypedMiddleware implements MiddlewareInterface 
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = null;
        c(function() use($request, &$response, $handler) {
            /**
             * if any typed variable is created in the handler, it would be deleted after execution.
             */
            $response = $handler->handle($request);
        });
        return $response;
    }
}

TODO :

  • add nullable* functions.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].