Macros::macroWidgets()   D
last analyzed

Complexity

Conditions 12
Paths 193

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
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        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