Passed
Push — master ( 5dc97c...6d4d86 )
by Brian
02:30
created

ParserUtils   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 16
eloc 37
c 5
b 1
f 0
dl 0
loc 94
ccs 41
cts 44
cp 0.9318
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 3 1
A sessionExists() 0 5 1
A resolveTagName() 0 11 2
A resolveTagClass() 0 18 3
A getAnswer() 0 21 5
A clean() 0 3 1
A instantiateTag() 0 5 1
A xpathFromStr() 0 7 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\Contracts\Config\Repository as ConfigRepository;
8
use Illuminate\Support\Str;
9
10
trait ParserUtils
11
{
12 3
    public function __get(string $key)
13
    {
14 3
        return $this->{$key};
15
    }
16
17 1
    public function __set(string $key, $value)
18
    {
19 1
        $this->{$key} = $value;
20
    }
21
22 1
    protected function xpathFromStr(string $file): \DOMXPath
23
    {
24 1
        $doc = new \DOMDocument();
25
26 1
        $doc->load($file);
27
28 1
        return new \DOMXPath($doc);
29
    }
30
31 9
    protected function sessionExists(string $sessionId): bool
32
    {
33 9
        $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...
34
35 9
        return $preSessionId === $sessionId;
36
    }
37
38 2
    protected function clean(string $code = ''): string
39
    {
40 2
        return trim(trim($code, '*'), '#');
41
    }
42
43 9
    protected function getAnswer(?string $userInput): ?string
44
    {
45 9
        if ('' === (string) $userInput) {
46 7
            return '';
47
        }
48
49 2
        $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...
50
51 2
        $answer = $this->clean(str_replace($preAnswer, '', $userInput));
52
53 2
        if ('' === (string) $answer) {
54
            return '';
55
        }
56
57 2
        if (! $preAnswer || Str::endsWith($preAnswer, '*')) {
58 1
            $this->store->append('_answer', "{$answer}*");
59
        } else {
60 1
            $this->store->append('_answer', "*{$answer}*");
61
        }
62
63 2
        return $answer;
64
    }
65
66 9
    protected function resolveTagName(\DOMNode $node): string
67
    {
68 9
        $tagName = $node->tagName;
69
70 9
        if ('action' !== strtolower($tagName)) {
71 9
            return Str::studly("{$tagName}Tag");
72
        }
73
74
        $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

74
        $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...
75
76
        return Str::studly("{$tagName}Action");
77
    }
78
79 9
    protected function resolveTagClass(string $tagName): string
80
    {
81 9
        $config = Container::getInstance()->make(ConfigRepository::class);
82 9
        $tagNs = $config->get('ussd.tag-ns', []);
83 9
        $actionNs = $config->get('ussd.action-ns', []);
84
85 9
        $namespaces = array_merge($tagNs, $actionNs);
86
87 9
        $fqcn = $tagName;
0 ignored issues
show
Unused Code introduced by
The assignment to $fqcn is dead and can be removed.
Loading history...
88
89 9
        foreach ($namespaces as $ns) {
90 9
            $fqcn = "{$ns}\\{$tagName}";
91 9
            if (class_exists($fqcn)) {
92 8
                return $fqcn;
93
            }
94
        }
95
96 1
        throw new \Exception("Missing tag {$tagName}.");
97
    }
98
99 9
    protected function instantiateTag(string $tagName, array $args = []): RenderableTag
100
    {
101 9
        $fqcn = $this->resolveTagClass($tagName);
102
103 8
        return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args);
104
    }
105
}
106