dneustadt/type-checker

Simple implementation for asserting PHP7 scalar type declarations and return types

1.0.0 2018-11-22 10:42 UTC

This package is auto-updated.

Last update: 2024-11-23 00:20:47 UTC


README

Simple implementation for asserting PHP7 scalar type declarations and return types.

composer require dneustadt/type-checker

Example

function test_function(string $foo): string
{
    return '';
}

class TestClass extends ReflectionClass
{
    protected function return_string(): string
    {
        return '';
    }

    protected function return_class(): TestClass
    {
        return $this;
    }

    protected function test_parameters(
        string $foo,
        TestClass $bar
    ): bool {
        unset($foo, $bar);

        return false;
    }
}

\TypeChecker\TypeChecker::reflectFunction('test_function')
        ->returnTypeIsEqual('string');
// true

\TypeChecker\TypeChecker::reflectMethod('return_string', TestClass::class)
        ->returnTypeIsEqual('string');
// true

\TypeChecker\TypeChecker::reflectMethod('return_class', TestClass::class)
        ->returnTypeIsInstanceOf('ReflectionClass');
// true

\TypeChecker\TypeChecker::reflectFunction('test_function')
        ->getArgument(0)
        ->typeIsEqual('bool');
// false

\TypeChecker\TypeChecker::reflectMethod('test_parameters', TestClass::class)
        ->getArgument(1)
        ->typeIsInstanceOf('ReflectionClass');
// true