parent
bd6335ec3b
commit
79e9928999
4 changed files with 105 additions and 36 deletions
@ -0,0 +1,34 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
namespace TypePhp\Generator; |
||||
|
||||
class ParamCountCheckMarco |
||||
{ |
||||
/** |
||||
* ZEND_PARSE_PARAMETERS_NONE cannot be used here, because it hardcodes a return statement, |
||||
* which would result in a type mismatch. |
||||
*/ |
||||
private const string NONE_PARAMS_TEMPLATE = <<<EOL |
||||
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_QUIET, 0, 0) |
||||
ZEND_PARSE_PARAMETERS_END_EX(php::throwErrorIfOccurred()); |
||||
EOL; |
||||
|
||||
/** |
||||
* @param int $maxNumArgs |
||||
* @param int $minNumArgs |
||||
* @param bool $hasVariadic |
||||
* @return string |
||||
*/ |
||||
public static function genParamCountMacroDefinition(int $maxNumArgs, int $minNumArgs, bool $hasVariadic = false): string |
||||
{ |
||||
if ($maxNumArgs == 0) { |
||||
return self::NONE_PARAMS_TEMPLATE; |
||||
} |
||||
|
||||
return sprintf(<<<EOL |
||||
ZEND_PARSE_PARAMETERS_START(%d, %d) |
||||
ZEND_PARSE_PARAMETERS_END_EX(php::throwErrorIfOccurred()); |
||||
EOL, $minNumArgs, ($hasVariadic ? -1 : $maxNumArgs)); |
||||
} |
||||
} |
||||
@ -0,0 +1,59 @@ |
||||
--TEST-- |
||||
args count check |
||||
--FILE-- |
||||
<?php |
||||
declare(strict_types=1); |
||||
function hello(string $value) {} |
||||
|
||||
function world(callable $callback) { |
||||
$callback('a'); |
||||
} |
||||
|
||||
function variadic(string $value, ...$results) { |
||||
var_dump($value, $results); |
||||
} |
||||
|
||||
function main() |
||||
{ |
||||
try { |
||||
$callback = 'hello'; |
||||
$callback(); |
||||
} catch (ArgumentCountError $e) { |
||||
var_dump($e->getMessage()); |
||||
} |
||||
|
||||
try { |
||||
world(function(string $value, string $value1) { |
||||
|
||||
}); |
||||
} catch (ArgumentCountError $e) { |
||||
var_dump($e->getMessage()); |
||||
} |
||||
|
||||
try { |
||||
$callable = "variadic"; |
||||
$callable(); |
||||
} catch (ArgumentCountError $e) { |
||||
var_dump($e->getMessage()); |
||||
} |
||||
|
||||
$callable('1', 1, 2, 3, 4, 5); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(43) "hello() expects exactly 1 argument, 0 given" |
||||
string(58) "stdClass::{closure}() expects exactly 2 arguments, 1 given" |
||||
string(47) "variadic() expects at least 1 argument, 0 given" |
||||
string(1) "1" |
||||
array(5) { |
||||
[0]=> |
||||
int(1) |
||||
[1]=> |
||||
int(2) |
||||
[2]=> |
||||
int(3) |
||||
[3]=> |
||||
int(4) |
||||
[4]=> |
||||
int(5) |
||||
} |
||||
Loading…
Reference in new issue