Macros   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 0 9 1
D macroWidgets() 0 41 12
1
<?php
2
/**
3
 * Macros.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           10.10.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Latte;
18
19
use Nette\Bridges;
20
use Nette\Utils;
21
22
use Latte;
23
use Latte\Compiler;
24
use Latte\MacroNode;
25
use Latte\PhpWriter;
26
27
/**
28
 * Widgets latte macros definition
29
 *
30
 * @package        iPublikuj:Widgets!
31
 * @subpackage     Latte
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35
final class Macros extends Bridges\ApplicationLatte\UIMacros
36
{
37
	/**
38
	 * @param Compiler $compiler
39
	 *
40
	 * @return void
41
	 */
42
	public static function install(Compiler $compiler) : void
43
	{
44
		$me = new static($compiler);
45
46
		/**
47
		 * {widget positionName}
48
		 */
49
		$me->addMacro('widgets', [$me, 'macroWidgets']);
50
	}
51
52
	/**
53
	 * @param MacroNode $node
54
	 * @param PhpWriter $writer
55
	 *
56
	 * @return string
57
	 * 
58
	 * @throws Latte\CompileException
59
	 */
60
	public static function macroWidgets(MacroNode $node, PhpWriter $writer) : string
61
	{
62
		$words = $node->tokenizer->fetchWords();
63
		if (!$words) {
64
			throw new Latte\CompileException('Missing widgets position name in {widgets}');
65
		}
66
		$name = $writer->formatWord($words[0]);
67
		$method = isset($words[1]) ? ucfirst($words[1]) : '';
68
		$method = Utils\Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";
69
70
		$tokens = $node->tokenizer;
71
		$pos = $tokens->position;
72
		$param = $writer->formatArray();
73
		$tokens->position = $pos;
74
75
		while ($tokens->nextToken()) {
76
			if ($tokens->isCurrent('=>') && !$tokens->depth) {
77
				$wrap = TRUE;
78
				break;
79
			}
80
		}
81
82
		if (empty($wrap)) {
83
			$param = substr($param, 1, -1); // removes array() or []
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
84
		}
85
86
		$prefix = '';
87
88
		if (is_string($name) && strpos($name, '-') === FALSE) {
89
			$prefix = '"widgets-" .';
90
		}
91
92
		return "/* line $node->startLine */ "
93
		. ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
94
		. '$_tmp = $this->global->uiControl->getComponent('. $prefix . $name . '); '
95
		. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); '
96
		. ($node->modifiers === ''
97
			? "\$_tmp->$method($param);"
98
			: $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
99
		);
100
	}
101
}
102