MacroCleanIndex::findSections()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ublaboo\Anabelle\Markdown\Macros;
6
7
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException;
8
9
final class MacroCleanIndex implements IMacro
10
{
11
12
	/**
13
	 * @throws DocuGeneratorException
14
	 */
15
	public function runMacro(
16
		string $inputDirectory,
17
		string $outputDirectory,
18
		string & $content // Intentionally &
19
	): void
20
	{
21
		/**
22
		 * Everything except for title and sections
23
		 */
24
		$heading = $this->findHeading($content);
25
26
		$lines = explode("\n", $content);
27
28
		[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...
29
30
		/**
31
		 * Now put allowed lines back together
32
		 */
33
		$content = "$heading\n\n";
34
35
		foreach ($siteSections as $siteSection) {
36
			$content .= "$siteSection\n";
37
		}
38
39
		foreach ($methodSections as $methodSection) {
40
			$content .= "$methodSection\n";
41
		}
42
	}
43
44
45
	private function findHeading(string $content): string
46
	{
47
		if (preg_match('/^# ?[^#].+/m', $content, $matches)) {
48
			return $matches[0];
49
		}
50
51
		return '# API Docs';
52
	}
53
54
55
	private function findSections(array $lines): array
56
	{
57
		$return = [1 => [], 2 => []];
58
59
		foreach ($lines as $line) {
60
			if (preg_match('/^(@@?) ?.+[^:]:.+\.md/', $line, $matches)) { // Section
61
				$return[strlen($matches[1])][] = $line;
62
			}
63
		}
64
65
		return $return;
66
	}
67
}
68