Passed
Push — main ( 1c4e95...6e2581 )
by Guillaume
02:37
created

JsUtils::declareVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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