FunctionToken::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the fubhy/math-php package.
5
 *
6
 * (c) Sebastian Siemssen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fubhy\Math\Token;
13
14
/**
15
 * Token class for functions.
16
 *
17
 * @author Sebastian Siemssen <[email protected]>
18
 */
19
class FunctionToken extends BaseToken
20
{
21
    /**
22
     * Evaluates the function on the stack.
23
     *
24
     * @param array $stack
25
     *     The stack of tokens in reverse polish (postfix) notation.
26
     *
27
     * @return \Fubhy\Math\Token\NumberToken
28
     *     The result as a NumberToken object.
29
     */
30
    public function execute(&$stack)
31
    {
32
        $arguments = [];
33
        list($count, $function) = $this->value;
34
        for ($i = 0; $i < $count; $i++) {
35
            array_push($arguments, array_pop($stack)->getValue());
36
        }
37
        $result = call_user_func_array($function, array_reverse($arguments));
38
        return new NumberToken($this->getOffset(), $result);
39
    }
40
}
41