Passed
Push — main ( f61cf1...0f2cdb )
by Guillaume
02:44
created

JsUtils   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanJSONFunctions() 0 3 1
A removeQuotes() 0 2 1
A generateFunction() 0 5 2
A declareVariable() 0 4 2
A kebabToPascal() 0 7 1
1
<?php
2
3
namespace PHPMV\utils;
4
5
class JsUtils {
6
	static private array $removeQuote = ["start" => "!!%", "end" => "%!!"];
7
8 1
	public static function cleanJSONFunctions(string $json): string {
9 1
		$pattern = '/(("|\')' . self::$removeQuote['start'] . ')|(' . self::$removeQuote['end'] . '("|\'))/';
10 1
		return \preg_replace($pattern, '', $json);
11
	}
12
13 1
	public static function removeQuotes(string $body): string {
14 1
		return self::$removeQuote["start"] . $body . self::$removeQuote["end"];
15
	}
16
17 1
	public static function generateFunction(string $body, array $params = [], bool $needRemoveQuote = true): string {
18 1
		if ($needRemoveQuote) {
19 1
			return self::removeQuotes("function(" . implode(",", $params) . "){" . $body . "}");
20
		}
21 1
		return "function(" . implode(",", $params) . "){" . $body . "}";
22
	}
23
24 1
	public static function declareVariable(string $type, string $name, $value, bool $lineBreak = true): string {
25 1
		$declaration = $type . " " . $name . " = " . $value . ";";
26 1
		if ($lineBreak) $declaration .= PHP_EOL;
27 1
		return $declaration;
28
	}
29
30 1
	public static function kebabToPascal(string $string): string {
31 1
		$string[0] = \strtoupper($string[0]);
32 1
		$pattern = '/(-\w{1})/';
33 1
		return \preg_replace_callback($pattern,
34 1
			function ($matches) {
35 1
				return \strtoupper($matches[1][1]);
36 1
			}, $string);
37
	}
38
}
39
40