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

Helper::translate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 12
rs 10
c 0
b 0
f 0
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