Completed
Push — master ( b0b364...ad1072 )
by Pavel
09:08
created

MacroBlockVariableOutput::runVariableMacro()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 1
nop 1
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): 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): 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
				return $input[1] . preg_replace('/\n/m', "\n{$whitespacePrefix}", $block);
36
			},
37
			$content
38
		);
39
	}
40
}
41