Passed
Push — master ( 336d57...057162 )
by Brian
09:01
created

Attributes::readAttr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Bmatovu\Ussd\Traits;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Str;
8
9
trait Attributes
10
{
11
    /**
12
     * @see https://stackoverflow.com/q/413071/2732184
13 24
     * @see https://www.regextester.com/97707
14
     */
15 24
    public function hydrate(string $text, string $pattern = '/[^{{\}\}]+(?=}})/'): string
16
    {
17 24
        preg_match_all($pattern, $text, $matches);
18 23
19
        if (0 === \count($matches[0])) {
20
            return $text;
21 2
        }
22
23 2
        $replace_vars = [];
24 2
25 2
        foreach ($matches[0] as $match) {
26
            $var = Str::slug($match, '_');
27
            $replace_vars["{{{$match}}}"] = $this->store->get($var, "{{$var}}");
28 2
        }
29
30
        return strtr($text, $replace_vars);
31 26
    }
32
33 26
    public function readAttr(string $name, $default = null)
34
    {
35 26
        $value = $this->node->attributes->getNamedItem($name)->nodeValue ?? $default;
36 6
37
        if (!$value) {
38
            return $value;
39 24
        }
40
41
        return $this->hydrate(trans($value));
0 ignored issues
show
Bug introduced by
trans($value) of type Illuminate\Translation\Translator is incompatible with the type string expected by parameter $text of Bmatovu\Ussd\Traits\Attributes::hydrate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return $this->hydrate(/** @scrutinizer ignore-type */ trans($value));
Loading history...
42
    }
43
}
44