modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-23 22:26:28 +04:00
parent c094eea62b
commit a4b67ec721
1 changed files with 55 additions and 17 deletions

View File

@ -42,10 +42,41 @@ use ReflectionMethod;
*/
class func {
/** tester si $value est une chaine non vide */
private static function ne($value): bool {
private static function is_ne($value): bool {
return is_string($value) && strlen($value) > 0;
}
/**
* tester si $func est d'une des formes suivantes:
* - "func" si function_exists("func")
* - [false, "func", ...]
*/
static final function is_global($func): bool {
if (self::is_ne($func)) {
$pos = strpos($func, "::");
return $pos === false && function_exists($func);
} elseif (is_array($func)) {
return ($func[0] ?? null) === false
&& self::is_ne($func[1] ?? null);
}
return false;
}
static final function fix_global_args(&$func, ?array &$args): bool {
if ($args === null) $args = [];
if (is_array($func)) {
if (count($func) > 2) {
$prefix_args = array_slice($func, 2);
$func = array_slice($func, 1, 1)[0];
$args = array_merge($prefix_args, $args);
} else {
$func = $func[0];
}
return true;
}
return false;
}
/**
* tester si $func est d'une des formes suivantes:
* - "::method"
@ -53,17 +84,16 @@ class func {
* - ["method"] si !class_exists("method")
* - [anything, "method", ...]
*/
static final function is_static($func, bool $allowClass=false): bool {
if (is_string($func)) {
static final function is_static($func): bool {
if (self::is_ne($func)) {
$pos = strpos($func, "::");
if ($pos === false) return false;
return $pos + 2 < strlen($func);
return $pos !== false && $pos + 2 < strlen($func);
} elseif (is_array($func) && array_key_exists(0, $func)) {
$count = count($func);
if ($count == 1) {
return self::ne($func[0]) && !class_exists($func[0]);
} elseif ($count > 1) {
return array_key_exists(1, $func) && self::ne($func[1]);
return self::is_ne($func[0]) && !class_exists($func[0]);
} elseif ($count > 1 && array_key_exists(1, $func)) {
return self::is_ne($func[1]);
}
}
return false;
@ -81,9 +111,11 @@ class func {
static final function fix_static(&$func, $class): bool {
if (is_object($class)) $class = get_class($class);
if (is_string($func) && substr($func, 0, 2) == "::") {
$func = "$class$func";
return true;
if (is_string($func)) {
if (substr($func, 0, 2) == "::") {
$func = "$class$func";
return true;
}
} else {
$count = count($func);
if ($count == 1) {
@ -98,10 +130,12 @@ class func {
}
/** tester si $method est une chaine de la forme "->method" */
private static function isam(&$method): bool {
private static function isam(&$method, bool $requireArrow=false): bool {
if (is_string($method)) {
if (substr($method, 0, 2) == "->") {
$method = substr($method, 2);
} elseif ($requireArrow) {
return false;
}
return strlen($method) > 0;
}
@ -116,7 +150,7 @@ class func {
*/
static final function is_method($func): bool {
if (is_string($func)) {
return self::isam($func);
return self::isam($func, true);
} elseif (is_array($func) && array_key_exists(0, $func)) {
$count = count($func);
if ($count == 1) {
@ -396,12 +430,16 @@ class func {
* - ["class", null, ...]
*/
static final function is_class($class): bool {
if (is_string($class)) {
if (self::is_ne($class)) {
return str::ends_with("::", $class)
|| !function_exists($class);
} elseif (is_array($class) && array_key_exists(0, $class)) {
return class_exists($class[0])
|| (array_key_exists(1, $class) && $class[1] === null);
} elseif (is_array($class) && self::is_ne($class[0] ?? null)) {
$count = count($class);
if ($count == 1) {
return class_exists($class[0]);
} elseif ($count > 1 && array_key_exists(1, $class)) {
return $class[1] === null;
}
}
return false;
}