aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/CustomFunction.php
blob: 1ebdd2c7f1296226952e2216c7e7175226526531 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace NXP\Classes;

use NXP\Exception\IncorrectFunctionParameterException;
use NXP\Exception\IncorrectNumberOfFunctionParametersException;
use ReflectionException;
use ReflectionFunction;

class CustomFunction
{
    public string $name = '';

    /**
     * @var callable $function
     */
    public $function;

    private ReflectionFunction $reflectionFunction;

    /**
     * CustomFunction constructor.
     *
     * @throws ReflectionException
     */
    public function __construct(string $name, callable $function)
    {
        $this->name = $name;
        $this->function = $function;
        $this->reflectionFunction = new ReflectionFunction($function);

    }

    /**
     * @param array<Token> $stack
     *
     * @throws IncorrectNumberOfFunctionParametersException|IncorrectFunctionParameterException
     */
    public function execute(array &$stack, int $paramCountInStack) : Token
    {
        if ($paramCountInStack < $this->reflectionFunction->getNumberOfRequiredParameters()) {
            throw new IncorrectNumberOfFunctionParametersException($this->name);
        }
        $args = [];

        if ($paramCountInStack > 0) {
            $reflectionParameters = $this->reflectionFunction->getParameters();

            for ($i = 0; $i < $paramCountInStack; $i++) {
                $value = \array_pop($stack)->value;
                $valueType = \gettype($value);
                $reflectionParameter = $reflectionParameters[\min(\count($reflectionParameters) - 1, $i)];
                //TODO to support type check for union types (php >= 8.0) and intersection types (php >= 8.1), we should increase min php level in composer.json
                // For now, only support basic types. @see testFunctionParameterTypes
                if ($reflectionParameter->hasType() && $reflectionParameter->getType()->getName() !== $valueType){
                    throw new IncorrectFunctionParameterException();
                }

                \array_unshift($args, $value);
            }
        }

        $result = \call_user_func_array($this->function, $args);

        return new Token(Token::Literal, $result);
    }
}