Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add psalm type declarations (and push type inference) #1

Closed
Ocramius opened this issue Jan 20, 2020 · 5 comments
Closed

Comments

@Ocramius
Copy link
Collaborator

Not sure if you are interested in this, but it is an interesting exercise, and good added value for downstream consumers.

Given (for example) combinator A:

$next = function (string $input) : int {
    return strlen($input); // simplistic example, ignore the fact that it is redundant
};

$a = new A($next, 'foo');

$a(); // int - can be inferred, in theory

This can be achieved via something like:

/**
 * @psalm-template XType
 * @psalm-template ResultType
 */
final class A extends Combinator
{
    /**
     * @psalm-param callable(XType) : ResultType $f
     * @psalm-param XType $x
     */
    public function __construct(callable $f, $x)
    {
        // ...
    }

    /** @psalm-return ResultType */
    public function __invoke()
    {
        // ...
    }

    /** @psalm-return callable() : ResultType */
    public static function closure(): callable
    {
        return static function (callable $f, $x) {
            return (new self($f, $x))();
        };
    }
}

This is currently tested in (for example) phpunit via some static analysis scripts: https://github.com/sebastianbergmann/phpunit/tree/619868463e06e5072c6e42dbae174d99cdd10fc2/tests/static-analysis

@drupol
Copy link
Contributor

drupol commented Jan 20, 2020

Hi Marco,

I will give it a try asap :-)

Thanks !!!

@drupol
Copy link
Contributor

drupol commented Jan 20, 2020

Here's a first try: https://psalm.dev/r/a71e6f340c

I don't think that it is possible to infer the type yet...

@Ocramius
Copy link
Collaborator Author

If you ignore the analysis on the internals, it's working on the inferred types (https://psalm.dev/r/0d396e5142). Specifically:

$v = new A(function (string $p) : array { return explode(',', $p); }, 'yadda');

echo $v(); // ERROR: InvalidArgument - 66:6 - Argument 1 of echo expects string, non-empty-list<string> provided

@drupol
Copy link
Contributor

drupol commented Jan 20, 2020

I just created a PR, it would be nice if you could give a quick review !

drupol added a commit that referenced this issue Jan 20, 2020
drupol added a commit that referenced this issue Jan 21, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 22, 2020
drupol added a commit that referenced this issue Jan 23, 2020
drupol added a commit that referenced this issue Jan 23, 2020
drupol added a commit that referenced this issue Jan 23, 2020
drupol added a commit that referenced this issue Jan 24, 2020
drupol added a commit that referenced this issue Jan 24, 2020
drupol added a commit that referenced this issue Jan 24, 2020
drupol added a commit that referenced this issue Jan 27, 2020
drupol added a commit that referenced this issue Jan 27, 2020
@drupol
Copy link
Contributor

drupol commented Jan 28, 2020

Thanks @Ocramius and @dannywillems for their help!

Tag 1.1.0 is available and this issue can now be closed.

@drupol drupol closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants