<?php const SOMECONST = 'Const hello'; enum SomeEnum { case Hearts; case Diamonds; case Clubs; case Spades; } function SomeFunction() { } $SomeFunctionClosure = function() { }; $SomeFunctionFirstClassCallable = SomeFunction(...); $someFunctionClosureFromCallabe = Closure::fromCallable('SomeFunction'); class ShouldTypeError { public function __construct($returnValue) { if ($returnValue !== 'no-return') { if ($returnValue === '$this') { return $this; } elseif ($returnValue === 'no-return-value') { return; } elseif ($returnValue === 'void') { return void; } elseif ($returnValue === 'never') { return never; } return $returnValue; } } } $testcases = [ 'no-return', 'no-return-value', '$this', new ShouldTypeError('no-return'), SOMECONST, 'abc', 1, 1.23, null, true, false, ['a', 'b', 'c'], ['a' => 'a', 'b' => 'b', 'c' => 'c', 0 => 'Zero'], 'never', 'void', SomeEnum::Spades, function() { echo 'Hi'.PHP_EOL; }, $SomeFunctionClosure, $SomeFunctionFirstClassCallable, $someFunctionClosureFromCallabe, new DateTimeImmutable(), ]; foreach($testcases as $testcase) { echo "--------------[ TESTCASE ]--------------\n"; var_dump($testcase); echo "\n"; try { $didReturn = new ShouldTypeError($testcase); switch($testcase) { case 'no-return': echo "Success: without a return statement is always valid.\n"; break; case 'no-return-value': echo "Success: a return statement without a value is always valid.\n"; break; case '$this': echo "Dubious: return $this is dubious.\n"; echo "- it fullfills the return type, so it could be allowed.\n"; echo "- but returning anything from a constructor is nonsense, because it is discarded.\n"; echo " As shown by the third testcase new SomeTypeError('no-return').\n"; break; default: echo "Error: why is it not a return TypeError?\n"; break; } if (!($didReturn instanceof ShouldTypeError)) { echo "Failed to new a ShouldTypeError.\n"; } } catch (Throwable $ex) { echo "Success: throwable: ".$ex->getMessage()."\n"; } }
You have javascript disabled. You will not be able to edit any code.