<?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;
}
Warning: "integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning in /in/fJgLA on line 19
Warning: "boolean" will be interpreted as a class name. Did you mean "bool"? Write "\boolean" to suppress this warning in /in/fJgLA on line 20
Warning: "double" will be interpreted as a class name. Did you mean "float"? Write "\double" to suppress this warning in /in/fJgLA on line 21
Warning: "resource" is not a supported builtin type and will be interpreted as a class name. Write "\resource" to suppress this warning in /in/fJgLA on line 22
Subject {
"valid": [
"int",
"bool",
"array",
"float",
"string",
"iterable",
"callable",
"void",
"object",
"mixed"
],
"invalid": [
"self",
"integer",
"boolean",
"double",
"resource"
]
}
Subject2 {
"valid": [
"INT -> int",
"BOOL -> bool",
"ARRAY -> array",
"FLOAT -> float",
"STRING -> string",
"ITERABLE -> iterable",
"CALLABLE -> callable",
"VOID -> void",
"OBJECT -> object",
"MIXED -> mixed"
],
"invalid": [
"SELF",
"INTEGER",
"BOOLEAN",
"DOUBLE",
"RESOURCE"
]
}
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Warning: "integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning in /in/fJgLA on line 19
Warning: "boolean" will be interpreted as a class name. Did you mean "bool"? Write "\boolean" to suppress this warning in /in/fJgLA on line 20
Warning: "double" will be interpreted as a class name. Did you mean "float"? Write "\double" to suppress this warning in /in/fJgLA on line 21
Warning: "resource" is not a supported builtin type and will be interpreted as a class name. Write "\resource" to suppress this warning in /in/fJgLA on line 22
Subject {
"valid": [
"int",
"bool",
"array",
"float",
"string",
"iterable",
"callable",
"void",
"object",
"mixed"
],
"invalid": [
"self",
"integer",
"boolean",
"double",
"resource"
]
}
Subject2 {
"valid": [
"INT -> int",
"BOOL -> bool",
"ARRAY -> array",
"FLOAT -> float",
"STRING -> string",
"ITERABLE -> iterable",
"CALLABLE -> callable",
"VOID -> void",
"OBJECT -> object",
"MIXED -> mixed"
],
"invalid": [
"SELF",
"INTEGER",
"BOOLEAN",
"DOUBLE",
"RESOURCE"
]
}