Completed
Push — master ( 2e705b...f82cfc )
by Pavel
10:01
created

MacroCleanIndex::runMacro()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ublaboo\Anabelle\Markdown\Macros;
6
7
use Ublaboo\Anabelle\Console\Utils\Logger;
8
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException;
9
use Ublaboo\Anabelle\Markdown\Parser;
10
11
final class MacroCleanIndex implements IMacro
12
{
13
14
	/**
15
	 * @throws DocuGeneratorException
16
	 */
17
	public function runMacro(
18
		string $inputDirectory,
19
		string $outputDirectory,
20
		string & $content // Intentionally &
21
	): void
22
	{
23
		/**
24
		 * Everything except for title and sections
25
		 */
26
		$heading = $this->findHeading($content);
27
28
		$lines = explode("\n", $content);
29
30
		[1 => $siteSections, 2 => $methodSections] = $this->findSections($lines);
0 ignored issues
show
Bug introduced by
The variable $siteSections does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $methodSections does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
31
32
		/**
33
		 * Now put allowed lines back together
34
		 */
35
		$content = "$heading\n\n";
36
37
		foreach ($siteSections as $siteSection) {
38
			$content .= "$siteSection\n";
39
		}
40
41
		foreach ($methodSections as $methodSection) {
42
			$content .= "$methodSection\n";
43
		}
44
	}
45
46
47
	private function findHeading(string $content): string
48
	{
49
		if (preg_match('/^# ?[^#].+/m', $content, $matches)) {
50
			return $matches[0];
51
		}
52
53
		return '# API Docu';
54
	}
55
56
57
	private function findSections(array $lines): array
58
	{
59
		$return = [1 => [], 2 => []];
60
61
		foreach ($lines as $line) {
62
			if (preg_match('/^(@@?) ?.+[^:]:.+\.md/', $line, $matches)) { // Section
63
				$return[sizeof($matches[1])][] = $line;
64
			}
65
		}
66
67
		return $return;
68
	}
69
}
70