From 20b3d26ecec7b29fdf8dfed5445cfb2755af9a1a Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 24 Apr 2024 19:39:01 +0300 Subject: [PATCH] A bit improving function signature validation heuristics --- bhl.inc.php | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/bhl.inc.php b/bhl.inc.php index 7a0b9d6..fe08cfd 100644 --- a/bhl.inc.php +++ b/bhl.inc.php @@ -284,10 +284,18 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra $module_chunks = array(); if($namespace) { - $module_chunks = preg_split('~namespace\s+'.preg_quote($namespace).'\s*{~', $module_src); - if(count($module_chunks) < 2) + $ns_chunks = _bhl_split_by_namespaces($module_src); + if(count($ns_chunks) == 0) + throw new Exception("No namespaces found in '$module_file'"); + + foreach($ns_chunks as $ns => $ns_src) + { + if($ns === $namespace) + $module_chunks[] = $ns_src; + } + + if(count($module_chunks) == 0) throw new Exception("Namespace '$namespace' for func '$func_full_name' not found in '$module_file'"); - $module_chunks = array_filter($module_chunks, function($item) { return strlen($item) > 0;}); } else $module_chunks[] = $module_src; @@ -322,6 +330,29 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra throw new Exception("Func '$func_full_name(".implode(',', $signature).")' not found in '$module_file'"); } +function _bhl_split_by_namespaces(string $src) : array +{ + $nss = array(); + + $chunks = preg_split('~^\s*(namespace\s+[^\{]+){~m', $src, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); + + for($i=0;$i