Func   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 97
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setName() 0 12 2
A getName() 0 4 1
A setCallback() 0 12 2
A getCallback() 0 4 1
A call() 0 11 4
1
<?php
2
3
namespace League\Plates\Template;
4
5
use League\Plates\Extension\ExtensionInterface;
6
use LogicException;
7
8
/**
9
 * A template function.
10
 */
11
class Func
12
{
13
    /**
14
     * The function name.
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * The function callback.
21
     * @var callable
22
     */
23
    protected $callback;
24
25
    /**
26
     * Create new Func instance.
27
     * @param string   $name
28
     * @param callable $callback
29
     */
30 200
    public function __construct($name, $callback)
31
    {
32 200
        $this->setName($name);
33 200
        $this->setCallback($callback);
34 200
    }
35
36
    /**
37
     * Set the function name.
38
     * @param  string $name
39
     * @return Func
40
     */
41 200
    public function setName($name)
42
    {
43 200
        if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name) !== 1) {
44 4
            throw new LogicException(
45 2
                'Not a valid function name.'
46 2
            );
47 2
        }
48
49 200
        $this->name = $name;
50
51 200
        return $this;
52
    }
53
54
    /**
55
     * Get the function name.
56
     * @return string
57
     */
58 8
    public function getName()
59
    {
60 8
        return $this->name;
61
    }
62
63
    /**
64
     * Set the function callback
65
     * @param  callable $callback
66
     * @return Func
67
     */
68 200
    public function setCallback($callback)
69
    {
70 200
        if (!is_callable($callback, true)) {
71 4
            throw new LogicException(
72 2
                'Not a valid function callback.'
73 2
            );
74
        }
75
76 200
        $this->callback = $callback;
77
78 200
        return $this;
79
    }
80
81
    /**
82
     * Get the function callback.
83
     * @return callable
84
     */
85 16
    public function getCallback()
86
    {
87 16
        return $this->callback;
88
    }
89
90
    /**
91
     * Call the function.
92
     * @param  Template $template
93
     * @param  array    $arguments
94
     * @return mixed
95
     */
96 16
    public function call(Template $template = null, $arguments = array())
97
    {
98 16
        if (is_array($this->callback) and
99 16
            isset($this->callback[0]) and
100 10
            $this->callback[0] instanceof ExtensionInterface
101 8
        ) {
102 4
            $this->callback[0]->template = $template;
103 2
        }
104
105 16
        return call_user_func_array($this->callback, $arguments);
106
    }
107
}
108