Psl is a standard library for PHP, inspired by hhvm/hsl.
The goal of Psl is to provide a consistent, centralized, well-typed set of APIs for PHP programmers.
<?php
declare(strict_types=1);
use Psl\Arr;
use Psl\Str;
/**
* @psalm-param iterable<?int> $codes
*/
function foo(iterable $codes): string
{
/** @var list<int> $codes */
$codes = Arr\filter_nulls($codes);
/** @var list<string> $chars */
$chars = Arr\map($codes, fn(int $code): string => Str\chr($code));
return Str\join($chars, ', ');
}
foo([95, 96, null, 98]);
// 'a, b, d'
This package doesn't have a stable release yet, but you can still install it using composer :
$ composer require azjezz/psl:dev-develop
Documentation is not available yet.
- All functions should be typed as strictly as possible
- The library should be internally consistent
- References may not be used
- Arguments should be as general as possible. For example, for
array
functions, preferiterable
inputs where practical, falling back toarray
when needed. - Return types should be as specific as possible
- All files should contain
declare(strict_types=1);
This is not exhaustive list.
- Functions argument order should be consistent within the library
- All iterable-related functions take the iterable as the first argument ( e.g.
Iter\map
andIter\filter
) $haystack
,$needle
, and$pattern
are in the same order for all functions that take them
- All iterable-related functions take the iterable as the first argument ( e.g.
- Functions should be consistently named.
- If an operation can conceivably operate on either keys or values, the default is to operate on the values - the version that operates on keys should have
_key
suffix (e.g.Iter\last
,Iter\last_key
,Iter\contains
,Iter\contains_key
) - Find-like operations that can fail should return
?T
; a second function should be added with anx
suffix that uses an invariant to returnT
(e.g.Arr\last
,Arr\lastx
) - Iterable functions that do an operation based on a user-supplied keying function for each element should be suffixed with
_by
(e.g.Arr\sort_by
,Iter\group_by
,Math\max_by
)
Thanks to our sponsors and supporters:
JetBrains |
---|
The MIT License (MIT). Please see LICENSE
for more information.