|
1
|
|
|
<?php |
|
2
|
|
|
namespace Desmond\functions\core; |
|
3
|
|
|
use Desmond\functions\DesmondFunction; |
|
4
|
|
|
use Desmond\ArgumentHelper; |
|
5
|
|
|
use Desmond\data_types\VectorType; |
|
6
|
|
|
use Desmond\exceptions\ArgumentException; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use ArgumentCountError; |
|
9
|
|
|
|
|
10
|
|
|
class DotFunc implements DesmondFunction |
|
11
|
|
|
{ |
|
12
|
|
|
use ArgumentHelper; |
|
13
|
|
|
public function id() |
|
14
|
|
|
{ |
|
15
|
|
|
return '.func'; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function run(array $args) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->expectArguments( |
|
21
|
|
|
'.func', |
|
22
|
|
|
[0 => ['Symbol', 'String']], |
|
23
|
|
|
$args |
|
24
|
|
|
); |
|
25
|
|
|
$function = self::getFunction($args); |
|
26
|
|
|
$args = self::getArgs($args); |
|
27
|
|
|
|
|
28
|
|
|
// This is to capture warnings when you call PHP functions without all their args. |
|
29
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) { |
|
|
|
|
|
|
30
|
|
|
throw new RuntimeException(); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
try { |
|
34
|
|
|
$value = $function(...$args); |
|
35
|
|
|
} catch (RuntimeException $error) { |
|
36
|
|
|
throw new ArgumentException("\".func\": Too few arguments passed to $function."); |
|
37
|
|
|
} catch (ArgumentCountError $error) { |
|
|
|
|
|
|
38
|
|
|
throw new ArgumentException("\".func\": Too few arguments passed to $function."); |
|
39
|
|
|
} |
|
40
|
|
|
restore_error_handler(); |
|
41
|
|
|
return $value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private static function getArgs($args) |
|
45
|
|
|
{ |
|
46
|
|
|
array_shift($args); |
|
47
|
|
|
$argValues = []; |
|
48
|
|
|
foreach ($args as $arg) { |
|
49
|
|
|
$argValues[] = $arg->value(); |
|
50
|
|
|
} |
|
51
|
|
|
return $argValues; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function getFunction(array $args) |
|
55
|
|
|
{ |
|
56
|
|
|
$functionName = $args[0]->value(); |
|
57
|
|
|
if (!function_exists($functionName)) { |
|
58
|
|
|
throw new ArgumentException("\".func\": undefined PHP function \"$functionName\"."); |
|
59
|
|
|
} else { |
|
60
|
|
|
return $args[0]->value(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.