Passed
Push — main ( 976a1c...cb0b4a )
by Guillaume
03:00
created

JsUtils   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 31
ccs 11
cts 18
cp 0.6111
rs 10
c 1
b 0
f 0
wmc 6

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 2 1
A kebabToPascal() 0 8 1
1
<?php
2
namespace PHPMV\utils;
3
4
class JsUtils{
5
    static private array $removeQuote = ["start"=>"!!%","end"=>"%!!"];
6
7 2
    public static function cleanJSONFunctions(string $json):string {
8 2
        $pattern='/(("|\')'.self::$removeQuote['start'].')|('.self::$removeQuote['end'].'("|\'))/';
9 2
        return \preg_replace($pattern, '', $json);
10
    }
11
12 8
    public static function removeQuotes(string $body):string{
13 8
        return self::$removeQuote["start"].$body.self::$removeQuote["end"];
14
    }
15
16 10
    public static function generateFunction(string $body, array $params = [], bool $needRemoveQuote = true):string {
17 10
        if($needRemoveQuote){
18 6
            return self::removeQuotes("function(".implode(",",$params)."){".$body."}");
19
        }
20 6
        return "function(".implode(",",$params)."){".$body."}";
21
    }
22
23 2
    public static function declareVariable(string $type, string $name, $value):string {
24 2
        return $type." ".$name." = ".$value.";\n";
25
    }
26
27
    public static function kebabToPascal(string $string){
28
        $string[0] = \strtoupper($string[0]);
29
        $pattern='/(-\w{1})/';
30
        return \preg_replace_callback($pattern,
31
            function ($matches) use ($string){
0 ignored issues
show
Unused Code introduced by
The import $string is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
32
                return \strtoupper($matches[1][1]);
33
34
        },$string);
35
    }
36
}
37
38