GithubHelp home page GithubHelp logo

php-resources / psl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azjezz/psl

0.0 1.0 0.0 809 KB

PHP Standard Library - a modern, consistent, centralized, well-typed set of APIs for PHP programmers.

License: MIT License

PHP 100.00%

psl's Introduction

Psl - PHP Standard Library

Unit tests status Static analysis status Security analysis status Coding standards status TravisCI Build Status Coverage Status Type Coverage SymfonyInsight Total Downloads Latest Stable Version License

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.

Example

<?php

declare(strict_types=1);

use Psl\{Dict, Str, Vec};

/**
 * @psalm-param iterable<?int> $codes
 */
function foo(iterable $codes): string
{
    $codes = Vec\filter_nulls($codes);

    $chars = Dict\map($codes, fn(int $code): string => Str\chr($code));

    return Str\join($chars, ', ');
}

foo([95, 96, null, 98]);
// 'a, b, d'

Installation

Supported installation method is via composer:

$ composer require azjezz/psl

Psalm Integration

PSL comes with a Psalm plugin, that improves return type for PSL functions that psalm cannot infer from source code.

To enable the Psalm plugin, add the Psl\Integration\Psalm\Plugin class to your psalm configuration file plugins list as follows:

<psalm>
   ...
   <plugins>
      ...
      <pluginClass class="Psl\Integration\Psalm\Plugin" />
   </plugins>
</psalm>

Documentation

Documentation is not available yet.

Principles

  • 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, prefer iterable inputs where practical, falling back to array when needed.
  • Return types should be as specific as possible
  • All files should contain declare(strict_types=1);

Consistency Rules

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 and Iter\filter )
    • $haystack, $needle, and $pattern are in the same order for all functions that take them
  • 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 an x suffix that uses an invariant to return T (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)

Sponsors

Thanks to our sponsors and supporters:

JetBrains

License

The MIT License (MIT). Please see LICENSE for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.