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

[Type] Introduce the Type API #44

Merged
merged 1 commit into from Aug 3, 2020
Merged

[Type] Introduce the Type API #44

merged 1 commit into from Aug 3, 2020

Conversation

azjezz
Copy link
Owner

@azjezz azjezz commented Jul 30, 2020

closes #42

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Psl\Type;
use Psl\Collection;

/** @psalm-var Type<iterable<array<int, array<int|string, string>>, Collection\ICollection&Collection\IIndexAccess> $spec */
$spec = Type\iterable(
    Type\arr(Type\int(), Type\arr(Type\array_key(), Type\string())),
    Type\intersection(
        Type\instance_of(Collection\ICollection::class),
        Type\instance_of(Collection\IIndexAccess::class)
    )
);

/**
 * @var null|iterable<array<int, array<int|string, string>>, Collection\ICollection&Collection\IIndexAccess> $value
 */
$value = Type\nullable($spec)->assert(null);

$value = Type\nullable($spec)->assert((static function () {
    yield [4 => ['bar' => 'baz', 1337 => 'qux']] => new Collection\Map([]);
})());

$value = $spec->assert((static function () {
    yield [4 => ['bar' => 'baz', 1337 => 'qux']] => new Collection\Map([]);
})());

try {
    $value = $spec->assert((static function () {
        yield [4 => ['bar' => 'baz', 1337 => 13]] => new Collection\Vector([]);
    })());
} catch (Type\Exception\TypeAssertException $e) {
    //    Expected "string", got "integer".
    print $e->getMessage();
}

try {
    $value = $spec->assert((static function () {
        yield [4 => ['bar' => 'baz', 1337 => 'f']] => new stdClass();
    })());
} catch (Type\Exception\TypeAssertException $e) {
    //    Expected "Psl\Collection\ICollection&Psl\Collection\IIndexAccess", got "stdClass".
    print $e->getMessage();
}

// iterable<array<int, array<array-key, string>>, Psl\Collection\ICollection&Psl\Collection\IIndexAccess>
print $spec->toString();

@azjezz azjezz added Status: In Progress This issue is being worked on, and has someone assigned. Status: Review Needed The issue has a PR attached to it which needs to be reviewed. Type: Enhancement Most issues will probably ask for additions or changes. labels Jul 30, 2020
@azjezz azjezz added this to the v1.0.0 milestone Jul 30, 2020
@azjezz azjezz self-assigned this Jul 30, 2020
@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@azjezz azjezz changed the title [TypeSpec] initial commit [TypeSpec] Introduce the TypeSpec API Jul 30, 2020
@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@azjezz azjezz force-pushed the type-spec branch 2 times, most recently from 8b5a974 to a0b3846 Compare August 2, 2020 21:02
@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@azjezz azjezz added Status: Revision Needed At least two people have seen issues in the PR that makes them uneasy. and removed Status: In Progress This issue is being worked on, and has someone assigned. labels Aug 2, 2020
@azjezz
Copy link
Owner Author

azjezz commented Aug 2, 2020

TypeSpec -> Type

Copy link
Owner Author

@azjezz azjezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few other things need to be fixed i guess

src/Psl/TypeSpec/Exception/TypeException.php Outdated Show resolved Hide resolved
src/Psl/TypeSpec/Exception/TypeException.php Outdated Show resolved Hide resolved
src/Psl/TypeSpec/Internal/ArraySpec.php Outdated Show resolved Hide resolved
src/Psl/TypeSpec/Internal/ArraySpec.php Outdated Show resolved Hide resolved
src/Psl/TypeSpec/Trace.php Outdated Show resolved Hide resolved
src/Psl/TypeSpec/TypeSpec.php Outdated Show resolved Hide resolved
tests/Psl/TypeSpec/ArrayKeySpecTest.php Outdated Show resolved Hide resolved
tests/Psl/TypeSpec/InstanceOfSpecTest.php Outdated Show resolved Hide resolved
@psalm-shepherd
Copy link

Psalm didn’t find any errors!

@azjezz azjezz changed the title [TypeSpec] Introduce the TypeSpec API [Type] Introduce the Type API Aug 3, 2020
@azjezz azjezz added Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness and removed Status: Review Needed The issue has a PR attached to it which needs to be reviewed. Status: Revision Needed At least two people have seen issues in the PR that makes them uneasy. labels Aug 3, 2020
@psalm-shepherd
Copy link

Psalm didn’t find any errors!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type Assertation API
2 participants