Adding improved yet simple validation for functions within namespaces
Publish PHP Package / docker (push) Successful in 6s
Details
Publish PHP Package / docker (push) Successful in 6s
Details
This commit is contained in:
parent
3b466a105f
commit
f5d1c418ed
23
bhl.inc.php
23
bhl.inc.php
|
@ -272,16 +272,26 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra
|
|||
if(sizeof($signature) == 0)
|
||||
throw new Exception("Signature is invalid");
|
||||
|
||||
//NOTE: we can't validate if the function is actually in a namespace
|
||||
// but was passed without a namespace prefix
|
||||
$name_items = explode('.', $func_full_name);
|
||||
$func = end($name_items);
|
||||
$func = array_pop($name_items);
|
||||
$namespace = implode('.', $name_items);
|
||||
|
||||
$module_src = file_get_contents($module_file);
|
||||
|
||||
if(!$module_src)
|
||||
throw new Exception("Bad module file '{$module_file}'");
|
||||
|
||||
$module_chunks = array();
|
||||
if($namespace)
|
||||
{
|
||||
$module_chunks = preg_split('~namespace\s+'.preg_quote($namespace).'\s*{~', $module_src);
|
||||
if(count($module_chunks) < 2)
|
||||
throw new Exception("Namespace '$namespace' not found in '$module_file'");
|
||||
$module_chunks = array_filter($module_chunks, function($item) { return strlen($item) > 0;});
|
||||
}
|
||||
else
|
||||
$module_chunks[] = $module_src;
|
||||
|
||||
$signature_pattern = '';
|
||||
$signature_pattern .= '~func\s+';
|
||||
if($signature[0] !== 'void')
|
||||
|
@ -303,7 +313,12 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra
|
|||
$signature_pattern .= '\s*\)';
|
||||
$signature_pattern .= '~';
|
||||
|
||||
if(!preg_match($signature_pattern, $module_src))
|
||||
foreach($module_chunks as $module_chunk_src)
|
||||
{
|
||||
if(preg_match($signature_pattern, $module_chunk_src))
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue