Passed
Push — master ( 3d398f...64f30d )
by Brian
02:20
created

Helper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 47.06%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 41
ccs 8
cts 17
cp 0.4706
rs 10
c 3
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDomElements() 0 17 3
A translate() 0 16 3
1
<?php
2
3
namespace Bmatovu\Ussd\Support;
4
5
class Helper
6
{
7 11
    public static function getDomElements(\DOMNodeList $nodeList, ?string $nodeName): array
8
    {
9 11
        $els = array_filter(iterator_to_array($nodeList), function ($node) use ($nodeName) {
10
            // return $node instanceof \DOMElement && ($nodeName && $node->nodeName === $nodeName);
11
12 11
            if (! $node instanceof \DOMElement) {
13 10
                return false;
14
            }
15
16 11
            if (! $nodeName) {
17 4
                return true;
18
            }
19
20 7
            return $node->nodeName === $nodeName;
21
        });
22
23 11
        return array_values($els);
24
    }
25
26
    /**
27
     * @see https://stackoverflow.com/q/413071/2732184
28
     * @see https://www.regextester.com/97707
29
     */
30
    public static function translate(string $text, string $pattern = '/[^{\}]+(?=})/'): string
31
    {
32
        preg_match_all($pattern, $text, $matches);
33
34
        if (0 === \count($matches[0])) {
35
            return $text;
36
        }
37
38
        $replace_vars = [];
39
40
        foreach ($matches[0] as $match) {
41
            $var = Str::slug($match, '_');
0 ignored issues
show
Bug introduced by
The type Bmatovu\Ussd\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
            $replace_vars["{{$match}}"] = $this->cache->get("{$this->prefix}_{$var}", "{{$var}}");
0 ignored issues
show
Bug Best Practice introduced by
The property cache does not exist on Bmatovu\Ussd\Support\Helper. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property prefix does not exist on Bmatovu\Ussd\Support\Helper. Did you maybe forget to declare it?
Loading history...
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
43
        }
44
45
        return strtr($text, $replace_vars);
46
    }
47
}
48