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

Attributes::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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