[php-src] Issue #13899: [ feature/fix ] Internal functions needs exactly number of arguments needs to be optimized
| From: | 6562680 | Date: | Sat, 06 Apr 2024 20:52:23 +0000 |
| Subject: | [php-src] Issue #13899: [ feature/fix ] Internal functions needs exactly number of arguments needs to be optimized | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246713@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13899
Author: 6562680
### Description
```
function _call_user_func($fn, ...$args)
{
error_clear_last();
$eMsg = null;
try {
$result = call_user_func($fn, ...$args);
}
catch ( \Throwable $e ) {
$eMsg = $e->getMessage();
}
if ($e = error_get_last()) {
$eMsg = $e[ 'message' ];
}
if (null !== $eMsg) {
if (false !== ($pos = strpos($eMsg, '() expects exactly '))) {
$max = (int) substr($eMsg, $pos + 19);
array_splice($args, $max);
$result = call_user_func('strlen', ...$args);
}
}
return $result;
}
function _call_user_func_array($fn, array $args)
{
error_clear_last();
$eMsg = null;
try {
$result = call_user_func_array($fn, $args);
}
catch ( \Throwable $e ) {
$eMsg = $e->getMessage();
}
if ($e = error_get_last()) {
$eMsg = $e[ 'message' ];
}
if (null !== $eMsg) {
if (false !== ($pos = strpos($eMsg, '() expects exactly '))) {
$max = (int) substr($eMsg, $pos + 19);
array_splice($args, $max);
$result = call_user_func_array('strlen', $args);
}
}
return $result;
}
```
And please, do something with that stuff:
```
function _filter_function($value, bool $isInternal = null, bool $isUser = null) : ?string
{
static $defined;
$defined = $defined ?? [ 'internal' => [], 'user' => [] ];
$isInternal = $isInternal ?? false;
$isUser = $isUser ?? false;
if (! is_string($value)) return null;
if ('' === $value) return null;
if (! function_exists($value)) return null;
if ($isInternal || $isUser) {
$flipInternal = array_flip(get_defined_functions()[ 'internal' ] ?? []);
$flipUser = array_flip(get_defined_functions()[ 'user' ] ?? []);
$defined[ 'internal' ] += $flipInternal;
$defined[ 'user' ] += $flipUser;
if ($isInternal && ! isset($defined[ 'internal' ][ $value ])) {
return null;
}
if ($isUser && ! isset($defined[ 'user' ][ $value ])) {
return null;
}
}
return $value;
}
```
Your function names is unique but returned array needs to walk through array of values instead of
use isset() for dictionary.