Completed
Push — master ( ac571e...b4d025 )
by Peter
02:07
created

FenceHolder::reveal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Zamm\Helpers;
10
11
/**
12
 * This class hides fenced code from processing,
13
 * by replacing fenced fragments with placeholder,
14
 * and then this can also bring fenced fragments back.
15
 *
16
 *
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
class FenceHolder
20
{
21
22
	private $fences = [];
23
24
	public function hide(&$docBlock)
25
	{
26
		$matches = [];
27
		preg_match_all('~(```.*?```)~ms', $docBlock, $matches);
28
		if (empty($matches[0]))
29
		{
30
			return;
31
		}
32
		$i = 0;
33
34
		// Collect matches
35
		foreach ($matches[0] as $match)
36
		{
37
			$key = sprintf('~%s@%s~', __CLASS__, $i++);
38
			$this->fences[$key] = $match;
39
		}
40
41
		$docBlock = str_replace(array_values($this->fences), array_keys($this->fences), $docBlock);
42
	}
43
44
	public function reveal(&$docBlock)
45
	{
46
		if (empty($this->fences))
47
		{
48
			return;
49
		}
50
		$docBlock = str_replace(array_keys($this->fences), array_values($this->fences), $docBlock);
51
	}
52
53
}
54