Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

Call::provideEvaluator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\ExpressionLanguage\Functions;
5
6
use Closure;
7
use SmartWeb\ModuleTesting\Contracts\ExpressionLanguage\Validator;
8
use SmartWeb\ModuleTesting\ExpressionLanguage\CallbackValidator;
9
use function call_user_func_array;
10
11
12
/**
13
 * Expression function for invoking custom callbacks
14
 *
15
 * @package SmartWeb\Testing\ExpressionLanguage\Functions
16
 */
17
class Call extends BaseExpressionFunction
18
{
19
    
20
    /**
21
     * @var Validator
22
     */
23
    private $callbackValidator;
24
    
25
    /**
26
     * @inheritDoc
27
     */
28
    protected function init()
29
    {
30
        $this->callbackValidator = new CallbackValidator("ExpressionFunction '{$this->getName()}' expects parameter 1 to be a valid callback");
31
    }
32
    
33
    /**
34
     * @inheritDoc
35
     */
36
    protected function provideCompiler() : callable
37
    {
38
        return function (...$args) : string
39
        {
40
            return sprintf('call_user_func(%s)', implode(', ', $args));
41
        };
42
43
//        return function (callable $callback, ...$args = []) : string
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
//        {
45
//            return sprintf('call_user_func_array(%s, %s)', $callback, implode(', ', $args));
46
//        };
47
    }
48
    
49
    /**
50
     * @inheritDoc
51
     */
52
    protected function provideEvaluator() : callable
53
    {
54
        /**
55
         * @param array            $arguments
56
         * @param callable|Closure $callback
57
         *
58
         * @return mixed
59
         */
60
        $evaluator = function (array $arguments, $callback)
61
        {
62
//            dd($callback);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
//            dd($arguments);
64
            $this->callbackValidator->validate($callback);
65
            
66
            return call_user_func_array($callback, $arguments['params']);
67
        };
68
        
69
        return $evaluator;
70
    }
71
    
72
}