Passed
Push — master ( f519e1...ef65fe )
by Brian
02:51
created

ParserUtils   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 40
c 1
b 0
f 0
dl 0
loc 106
ccs 28
cts 48
cp 0.5833
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A sessionExists() 0 5 1
A resolveTagName() 0 11 2
A __get() 0 3 1
A resolveTagClass() 0 18 3
A getAnswer() 0 16 3
A clean() 0 7 2
A translate() 0 16 3
A instantiateTag() 0 5 1
A __set() 0 3 1
1
<?php
2
3
namespace Bmatovu\Ussd\Traits;
4
5
use Bmatovu\Ussd\Contracts\RenderableTag;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Str;
8
9
trait ParserUtils
10
{
11 1
    public function __get(string $key)
12
    {
13 1
        return $this->{$key};
14
    }
15
16
    public function __set(string $key, $value)
17
    {
18
        $this->{$key} = $value;
19
    }
20
21
    /**
22
     * @see https://stackoverflow.com/q/413071/2732184
23
     * @see https://www.regextester.com/97707
24
     */
25
    public function translate(string $text, string $pattern = '/[^{{\}\}]+(?=}})/'): string
26
    {
27
        preg_match_all($pattern, $text, $matches);
28
29
        if (0 === \count($matches[0])) {
30
            return $text;
31
        }
32
33
        $replace_vars = [];
34
35
        foreach ($matches[0] as $match) {
36
            $var = Str::slug($match, '_');
37
            $replace_vars["{{{$match}}}"] = $this->store->get("{$prefix}{$var}", "{{$var}}");
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist on Bmatovu\Ussd\Traits\ParserUtils. Since you implemented __get, consider adding a @property annotation.
Loading history...
Comprehensibility Best Practice introduced by
The variable $prefix seems to be never defined.
Loading history...
38
        }
39
40
        return strtr($text, $replace_vars);
41
    }
42
43 5
    protected function sessionExists(string $sessionId): bool
44
    {
45 5
        $preSessionId = $this->store->get('_session_id', '');
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist on Bmatovu\Ussd\Traits\ParserUtils. Since you implemented __get, consider adding a @property annotation.
Loading history...
46
47 5
        return $preSessionId === $sessionId;
48
    }
49
50 3
    protected function clean(string $code = ''): string
51
    {
52 3
        if (! $code) {
53 3
            return $code;
54
        }
55
56
        return rtrim(ltrim($code, '*'), '#');
57
    }
58
59 5
    protected function getAnswer(?string $userInput): ?string
60
    {
61 5
        if (! $userInput) {
62 5
            return '';
63
        }
64
65
        $preAnswer = $this->store->get('_answer');
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist on Bmatovu\Ussd\Traits\ParserUtils. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
        if (! $preAnswer) {
67
            return (string) $userInput;
68
        }
69
70
        $answer = $this->clean(str_replace($preAnswer, '', $userInput));
71
72
        $this->store->put('_answer', $userInput);
73
74
        return $answer;
75
    }
76
77 5
    protected function resolveTagName(\DOMNode $node): string
78
    {
79 5
        $tagName = $node->tagName;
80
81 5
        if ('action' !== strtolower($tagName)) {
82 5
            return Str::studly("{$tagName}Tag");
83
        }
84
85
        $tagName = $node->attributes->getNamedItem('name')->nodeValue;
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

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

85
        $tagName = $node->attributes->/** @scrutinizer ignore-call */ getNamedItem('name')->nodeValue;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
87
        return Str::studly("{$tagName}Action");
88
    }
89
90 5
    protected function resolveTagClass(string $tagName): string
91
    {
92
        // $config = Container::getInstance()->make('config');
93 5
        $tagNs = config('ussd.tag-ns', []);
94 5
        $actionNs = config('ussd.action-ns', []);
95
96 5
        $namespaces = array_merge($tagNs, $actionNs);
97
98 5
        $fqcn = $tagName;
0 ignored issues
show
Unused Code introduced by
The assignment to $fqcn is dead and can be removed.
Loading history...
99
100 5
        foreach ($namespaces as $ns) {
101 5
            $fqcn = "{$ns}\\{$tagName}";
102 5
            if (class_exists($fqcn)) {
103 4
                return $fqcn;
104
            }
105
        }
106
107 1
        throw new \Exception("Missing class: {$tagName}");
108
    }
109
110 5
    protected function instantiateTag(string $tagName, array $args = []): RenderableTag
111
    {
112 5
        $fqcn = $this->resolveTagClass($tagName);
113
114 4
        return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args);
115
    }
116
}
117