Passed
Push — main ( c2a6e5...44af98 )
by Guillaume
02:49
created

PhpUtils::parseFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 7
c 2
b 1
f 0
nc 2
nop 2
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace PHPMV\utils;
4
5
class PhpUtils {
6
	static private string $delimiter = '---';
7
	static private array $parsedJs = [];
8
9 2
	public static function importFromFile(string $filename, string $extension): string {
10 2
		return \str_replace(["\n","\r","\t"],"",\file_get_contents("$filename.$extension", true));
11
	}
12
13 1
	public static function parseFile(string $filename, string $extension): array {
14 1
		$pattern = '/' . self::$delimiter . '(.+?)' . self::$delimiter . '(.+?)' . self::$delimiter . 'end' . self::$delimiter . '/s';
15 1
		$templateString = self::importFromFile($filename, $extension);
16 1
		\preg_match_all($pattern, $templateString, $templateArray);
17 1
		$iterationNumber = count($templateArray[0]);
18 1
		for ($i = 0; $i < $iterationNumber; $i++) {
19 1
			self::$parsedJs[$templateArray[1][$i]] = $templateArray[2][$i];
20
		}
21 1
		return $templateArray;
22
	}
23
24 1
	public static function getParsedJs(string $name,array $variables = []): string {
25 1
		if(isset($variables)){
26 1
			foreach($variables as $key => $value){
27 1
				self::$parsedJs[$name] = \str_replace("{{ $key }}",$value, self::$parsedJs[$name]);
28
			}
29
		}
30 1
		return self::$parsedJs[$name];
31
	}
32
}
33
34