ControlNode   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 12
Bugs 2 Features 1
Metric Value
c 12
b 2
f 1
dl 0
loc 179
rs 10
wmc 22
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
F __invoke() 0 136 18
1
<?php
2
3
/*
4
 * This file is part of the Patron package.
5
 *
6
 * (c) Olivier Laviale <[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 Patron;
13
14
class ControlNode extends Node
15
{
16
	/**
17
	 * @var string
18
	 */
19
	public $name;
20
21
	/**
22
	 * @var array
23
	 */
24
	public $args;
25
26
	/**
27
	 * @var Node[]
28
	 */
29
	public $nodes;
30
31
	/**
32
	 * @param string $name
33
	 * @param array $args
34
	 * @param Node[] $nodes
35
	 */
36
	public function __construct($name, array $args, array $nodes)
37
	{
38
		$this->name = $name;
39
40
		foreach ($nodes as $i => $node)
41
		{
42
			if (!($node instanceof self) || $node->name != 'with-param')
43
			{
44
				continue;
45
			}
46
47
			$args[$node->args['name']] = $node;
48
49
			unset($nodes[$i]);
50
		}
51
52
		$this->args = $args;
53
		$this->nodes = $nodes;
54
	}
55
56
	public function __invoke(Engine $engine, $context)
57
	{
58
		$name = $this->name;
59
60
		list($callback, $params) = $engine->markups[$name];
61
62
		$args = $this->args;
63
64
		$missing = [];
65
		$binding = empty($params['no-binding']);
66
67
		foreach ($params as $param => $options)
68
		{
69
			if (is_array($options))
70
			{
71
				#
72
				# default value
73
				#
74
75
				if (isset($options['default']) && !array_key_exists($param, $args))
76
				{
77
					$args[$param] = $options['default'];
78
				}
79
80
				if (array_key_exists($param, $args))
81
				{
82
					$value = $args[$param];
83
84
					if (isset($options['expression']))
85
					{
86
						$silent = !empty($options['expression']['silent']);
87
88
						//\ICanBoogie\log('\4:: evaluate expression "\3" with value: \5, params \1 and args \2', array($hook->params, $args, $param, $name, $value));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
89
90
						if ($value{0} == ':')
91
						{
92
							$args[$param] = substr($value, 1);
93
						}
94
						else
95
						{
96
							$args[$param] = $engine->evaluate($value, $silent, $context);
97
						}
98
					}
99
				}
100
				else if (isset($options['required']))
101
				{
102
					$missing[$param] = true;
103
				}
104
			}
105
			else
106
			{
107
				if (!array_key_exists($param, $args))
108
				{
109
					$args[$param] = $options;
110
				}
111
			}
112
113
			if (!isset($args[$param]))
114
			{
115
				$args[$param] = null;
116
			}
117
		}
118
119
		if ($missing)
120
		{
121
			throw new \Exception(\ICanBoogie\format('The %param parameter is required for the %markup markup, given %args', [
122
123
				'%param' => implode(', ', array_keys($missing)),
124
				'%markup' => $name,
125
				'%args' => json_encode($args)
126
127
			]));
128
		}
129
130
		#
131
		# resolve arguments
132
		#
133
134
		foreach ($args as &$arg)
135
		{
136
			if ($arg instanceof ControlNode)
137
			{
138
				if (isset($arg->args['select']))
139
				{
140
					$arg = $engine->evaluate($arg->args['select'], false, $context);
141
				}
142
				else
143
				{
144
					$arg = $engine($arg->nodes);
145
				}
146
			}
147
		}
148
149
		unset($arg);
150
151
		#
152
		# call hook
153
		#
154
155
		$engine->trace_enter([ 'markup', $name ]);
156
157
		if ($binding)
158
		{
159
			array_push($engine->context_markup, [ $engine->context['self'], $engine->context['this'] ]);
160
161
			$engine->context['self'] = [
162
163
				'name' => $name,
164
				'arguments' => $args
165
166
			];
167
		}
168
169
		$rc = null;
170
171
		try
172
		{
173
			$rc = call_user_func($callback, $args, $engine, $this->nodes);
174
		}
175
		catch (\Exception $e)
176
		{
177
			$engine->handle_exception($e);
178
		}
179
180
		if ($binding)
181
		{
182
			$a = array_pop($engine->context_markup);
183
184
			$engine->context['self'] = $a[0];
185
			$engine->context['this'] = $a[1];
186
		}
187
188
		$engine->trace_exit();
189
190
		return $rc;
191
	}
192
}
193