MacroInclude::runMacro()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 5
Ratio 26.32 %

Importance

Changes 0
Metric Value
dl 5
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
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 MacroInclude implements IMacro
10
{
11
12
	const INCLUDE_PATTERN = '/^#include (.+\.\w{1,4})/um';
13
14
15
	/**
16
	 * @throws DocuGeneratorException
17
	 */
18
	public function runMacro(
19
		string $inputDirectory,
20
		string $outputDirectory,
21
		string & $content // Intentionally &
22
	): void
23
	{
24
		/**
25
		 * Substitute "#include" macros with actual files
26
		 */
27
		$content = preg_replace_callback(
28
			self::INCLUDE_PATTERN,
29 View Code Duplication
			function(array $input) use ($inputDirectory): string {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
				$dir = $inputDirectory . '/' . dirname($input[1]);
31
32
				return $this->includeFile($dir, basename($input[1]));
33
			},
34
			$content
35
		);
36
	}
37
38
39
	/**
40
	 * @throws DocuGeneratorException
41
	 */
42
	public function includeFile(string $directory, string $filename): string
43
	{
44
		if (!file_exists("$directory/$filename")) {
45
			throw new DocuGeneratorException(
46
				sprintf("Can not include non-existing file %s/%s", $directory, $filename)
47
			);
48
		}
49
50
		$includeContent = file_get_contents("$directory/$filename");
51
52
		$includeContent = preg_replace_callback(
53
			self::INCLUDE_PATTERN,
54 View Code Duplication
			function(array $input) use ($directory): string {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
				$dir = $directory . '/' . dirname($input[1]);
56
57
				return $this->includeFile($dir, basename($input[1]));
58
			},
59
			$includeContent
60
		);
61
62
		return $includeContent;
63
	}
64
}
65