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

FenceHolder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hide() 0 19 3
A reveal() 0 8 2
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