Completed
Push — master ( 3c7819...8c4383 )
by Scott
02:43
created

DotFunc::run()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
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) {
4 ignored issues
show
Unused Code introduced by
The parameter $errno is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errstr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errfile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errline is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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) {
1 ignored issue
show
Bug introduced by
The class ArgumentCountError does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
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