Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

--TEST-- Union types in reflection --FILE-- <?php function dumpType(ReflectionUnionType $..

Decoded Output download

--TEST--
Union types in reflection
--FILE--
<?php

function dumpType(ReflectionUnionType $rt) {
    echo "Type $rt:
";
    echo "Allows null: " . ($rt->allowsNull() ? "true" : "false") . "
";
    foreach ($rt->getTypes() as $type) {
        echo "  Name: " . $type->getName() . "
";
        echo "  String: " . (string) $type . "
";
        echo "  Allows Null: " . ($type->allowsNull() ? "true" : "false") . "
";
    }
}

function test1(): X|Y|int|float|false|null { }
function test2(): X|iterable|bool { }

class Test {
    public X|Y|int $prop;
}

dumpType((new ReflectionFunction('test1'))->getReturnType());
dumpType((new ReflectionFunction('test2'))->getReturnType());

$rc = new ReflectionClass(Test::class);
$rp = $rc->getProperty('prop');
dumpType($rp->getType());

/* Force CE resolution of the property type */

class x {}
$test = new Test;
$test->prop = new x;

$rp = $rc->getProperty('prop');
dumpType($rp->getType());

class y {}
$test->prop = new y;

$rp = $rc->getProperty('prop');
dumpType($rp->getType());

?>
--EXPECT--
Type X|Y|int|float|false|null:
Allows null: true
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
  Name: float
  String: float
  Allows Null: false
  Name: false
  String: false
  Allows Null: false
  Name: null
  String: null
  Allows Null: true
Type X|iterable|bool:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: iterable
  String: iterable
  Allows Null: false
  Name: bool
  String: bool
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false

Did this file decode correctly?

Original Code

--TEST--
Union types in reflection
--FILE--
<?php

function dumpType(ReflectionUnionType $rt) {
    echo "Type $rt:\n";
    echo "Allows null: " . ($rt->allowsNull() ? "true" : "false") . "\n";
    foreach ($rt->getTypes() as $type) {
        echo "  Name: " . $type->getName() . "\n";
        echo "  String: " . (string) $type . "\n";
        echo "  Allows Null: " . ($type->allowsNull() ? "true" : "false") . "\n";
    }
}

function test1(): X|Y|int|float|false|null { }
function test2(): X|iterable|bool { }

class Test {
    public X|Y|int $prop;
}

dumpType((new ReflectionFunction('test1'))->getReturnType());
dumpType((new ReflectionFunction('test2'))->getReturnType());

$rc = new ReflectionClass(Test::class);
$rp = $rc->getProperty('prop');
dumpType($rp->getType());

/* Force CE resolution of the property type */

class x {}
$test = new Test;
$test->prop = new x;

$rp = $rc->getProperty('prop');
dumpType($rp->getType());

class y {}
$test->prop = new y;

$rp = $rc->getProperty('prop');
dumpType($rp->getType());

?>
--EXPECT--
Type X|Y|int|float|false|null:
Allows null: true
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
  Name: float
  String: float
  Allows Null: false
  Name: false
  String: false
  Allows Null: false
  Name: null
  String: null
  Allows Null: true
Type X|iterable|bool:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: iterable
  String: iterable
  Allows Null: false
  Name: bool
  String: bool
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false
Type X|Y|int:
Allows null: false
  Name: X
  String: X
  Allows Null: false
  Name: Y
  String: Y
  Allows Null: false
  Name: int
  String: int
  Allows Null: false

Function Calls

None

Variables

None

Stats

MD5 423d06fffb1b054f6d8098aacfe99702
Eval Count 0
Decode Time 83 ms