Completed
Push — master ( e088fc...e52644 )
by Adam
02:49
created

Macros::macroWidgets()   D

Complexity

Conditions 12
Paths 193

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.8484
c 0
b 0
f 0
cc 12
eloc 27
nc 193
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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