Adding improved yet simple validation for functions within namespaces
Publish PHP Package / docker (push) Successful in 6s Details

This commit is contained in:
Pavel Shevaev 2024-04-24 16:29:13 +03:00
parent 3b466a105f
commit f5d1c418ed
1 changed files with 20 additions and 5 deletions

View File

@ -272,16 +272,26 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra
if(sizeof($signature) == 0) if(sizeof($signature) == 0)
throw new Exception("Signature is invalid"); 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); $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); $module_src = file_get_contents($module_file);
if(!$module_src) if(!$module_src)
throw new Exception("Bad module file '{$module_file}'"); 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 = '';
$signature_pattern .= '~func\s+'; $signature_pattern .= '~func\s+';
if($signature[0] !== 'void') if($signature[0] !== 'void')
@ -303,8 +313,13 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra
$signature_pattern .= '\s*\)'; $signature_pattern .= '\s*\)';
$signature_pattern .= '~'; $signature_pattern .= '~';
if(!preg_match($signature_pattern, $module_src)) foreach($module_chunks as $module_chunk_src)
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'"); {
if(preg_match($signature_pattern, $module_chunk_src))
return;
}
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'");
} }
function bhl_upm_path() : string function bhl_upm_path() : string