GithubHelp home page GithubHelp logo

pmmp / callbackvalidator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daverandom/callbackvalidator

1.0 3.0 0.0 130 KB

Tools for validating callback signatures in PHP

License: MIT License

PHP 100.00%
phpstan-strict on-packagist github-actions-enabled phpstan-l9 php83

callbackvalidator's Introduction

Callback Validator

Validates callback signatures against a prototype.

This is a fork of daverandom/callback-validator used by PocketMine-MP. There are no significant changes from the upstream repository apart from more test versions, updated dependencies, and tagged releases for packages to use.

Since the upstream version has no release, it affects the composer stability of packages that use it. This caused problems for packages depending on pocketmine/pocketmine-mp because they could not receive its latest versions.

Status

CI

Usage

// Create a prototype function (can be any callable)
$prototype = function (A $a, B $b, $c): ?string {};

// Validate that callables match the prototype
$tests = [
    $prototype, // true
    function (A $a, B $b, $c) {}, // false - return type does not match
    function ($a, $b, $c): ?string {}, // true - arguments are contravariant
    function (A $a, B $b): ?string {}, // true - extra args don't cause errors
    function (A $a, B $b, $c, $d): ?string {}, // false - Insufficient args cause an error
    function (C $a, B $b, $c): ?string {}, // true if C is a supertype of A, false otherwise
    function (SuperTypeOfA $a, B $b, $c): ?string {}, // true
    function (A $a, B $b, $c): string {}, // true - return types are covariant
];

// Create a type from a prototype
$type = CallbackType::createFromCallable($prototype);

run_tests($type, $tests);

// ...or create a type by hand for more granular control over variance rules
$type = new CallbackType(
    new ReturnType(BuiltInTypes::STRING, ReturnType::NULLABLE | ReturnType::COVARIANT),
    new ParameterType('a', A::class),
    new ParameterType('b', B::class),
    new ParameterType('c')
);

run_tests($type, $tests);

function run_tests(CallbackType $type, array $tests)
{
    foreach ($tests as $test) {
        if ($type->isSatisfiedBy($test)) {
            echo "pass\n";
        } else {
            // CallbackType implements __toString() for easy inspections
            echo CallbackType::createFromCallable($test) . " does not satisfy {$type}\n";
        }
    }
}

TODO

  • Lots more tests
  • Explain (text explanation of why callback does not validate)

callbackvalidator's People

Contributors

buchwasa avatar daverandom avatar dependabot-preview[bot] avatar dependabot[bot] avatar dktapps avatar peehaa avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

callbackvalidator's Issues

Utils::validateCallableSignature does not support UnionType

Issue description

  • Expected result: Can successfully verify callables including UnionType
  • Actual result: Get an error

This is actually a problem with the pmmp/CallbackValidator library, but I think PMs that provide APIs that use these support PHP8 and need to be able to use it.

Steps to reproduce the issue

  1. pocketmine\utils\Utils::validateCallableSignature(function (string|int $a): void {}, function (): void {});
  2. Throw exceptions

OS and versions

Server version: 4.0.0-BETA4 (git hash: 9e6d7405709560c0025e072324602983213276dd)
Compatible Minecraft version: 1.17.30 (protocol version: 465)
PHP version: 8.0.11
PHP JIT: disabled
Operating system: win

Plugins

  • If you remove all plugins, does the issue still occur? Yes
  • If the issue is not reproducible without plugins: No
    • Have you asked for help on our forums before creating an issue?
    • Can you provide sample, minimal reproducing code for the issue? If so, paste it in the bottom section

Crashdump, backtrace or other files

LogicException: "Unsupported reflection type ReflectionUnionType" (EXCEPTION) in "pmsrc/vendor/pocketmine/callback-validator/src/ParameterType" at line 82

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.