MacroBlockVariableOutput::runVariableMacro()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
nc 1
cc 5
eloc 16
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ublaboo\Anabelle\Markdown\Macros;
6
7
final class MacroBlockVariableOutput extends AbstractMacroVariable implements IMacro
8
{
9
10
	protected function runVariableMacro(string & $content, int $depth): void // Intentionally &
11
	{
12
		/**
13
		 * Remove lines with inline variables definition and put then into DocuScope
14
		 */
15
		$content = preg_replace_callback(
16
			'/(^.*)?\{\$\$([a-zA-Z_0-9]+)\}/m',
17
			function(array $input) use ($depth): string {
18
				$whitespacePrefix = '';
19
20
				/**
21
				 * Prefix the whole block with current identation
22
				 */
23
				foreach (str_split($input[1]) as $character) {
24
					if ($character === "\t") {
25
						$whitespacePrefix .= $character;
26
27
						continue;
28
					}
29
30
					break;
31
				}
32
33
				$block = $this->docuScope->getBlockVariable($input[2]);
34
35
				if ($depth <= parent::MAX_EXECUTE_DEPTH) {
36
					foreach ($this->getMacrosToRunOnBlockVariables() as $macro) {
37
						$macro->runVariableMacro($block, $depth + 1);
38
					}
39
				}
40
41
				return $input[1] . preg_replace('/\n/m', "\n{$whitespacePrefix}", $block);
42
			},
43
			$content
44
		);
45
	}
46
}
47