- json_encode: documentation ( source)
<?php
interface Subject
{
// valid return types
public function int(): int;
public function bool(): bool;
public function array(): array;
public function float(): float;
public function string(): string;
public function iterable(): iterable;
public function callable(): callable;
public function void(): void;
public function object(): object;
public function mixed(): mixed;
// invalid return types
public function self(): self;
public function integer(): integer;
public function boolean(): boolean;
public function double(): double;
public function resource(): resource;
}
interface Subject2
{
// valid return types
PUBLIC FUNCTION INT(): INT;
PUBLIC FUNCTION BOOL(): BOOL;
PUBLIC FUNCTION ARRAY(): ARRAY;
PUBLIC FUNCTION FLOAT(): FLOAT;
PUBLIC FUNCTION STRING(): STRING;
PUBLIC FUNCTION ITERABLE(): ITERABLE;
PUBLIC FUNCTION CALLABLE(): CALLABLE;
PUBLIC FUNCTION VOID(): VOID;
PUBLIC FUNCTION OBJECT(): OBJECT;
PUBLIC FUNCTION MIXED(): MIXED;
PUBLIC FUNCTION SELF(): SELF;
// invalid return types
PUBLIC FUNCTION INTEGER(): INTEGER;
PUBLIC FUNCTION BOOLEAN(): BOOLEAN;
PUBLIC FUNCTION DOUBLE(): DOUBLE;
PUBLIC FUNCTION RESOURCE(): RESOURCE;
}
test(Subject::class);
test(Subject2::class);
/**
* @param class-string $class
*/
function test(string $class): void
{
$result = [];
$ref = new ReflectionClass($class);
foreach ($ref->getMethods() as $ref_method) {
$name = $ref_method->getName();
$type = $ref_method->getReturnType();
$is_valid = $type instanceof ReflectionNamedType && $type->isBuiltin();
$key = $is_valid ? 'valid' : 'invalid';
$type_name = $type instanceof ReflectionNamedType ? $type->getName() : null;
if ($type_name && $name !== $type_name) {
$name = "{$name} -> {$type_name}";
}
$result[$key][] = $name;
}
echo $class, ' ', json_encode($result, JSON_PRETTY_PRINT), PHP_EOL;
}