PHP code example of dneustadt / type-checker

1. Go to this page and download the library: Download dneustadt/type-checker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dneustadt / type-checker example snippets


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