Passed
Branch master (106872)
by Henri
02:03 queued 53s
created

HelperTrait::replace_Array()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 4
1
<?php
2
3
namespace HnrAzevedo\Viewer;
4
5
trait HelperTrait{
6
    use CheckTrait;
7
8
    protected function getOB(string $require): string
9
    {
10
        ob_start();
11
        require($require);
12
        $response = ob_get_contents();
13
        ob_end_clean();
14
15
        $response = explode(PHP_EOL,$response);
16
        foreach($response as $index => $value){
17
            $response[$index] = ltrim($value);
18
        }
19
        
20
        return implode('',$response);
21
    }
22
23
    protected function getVars(string $buffer,string $prefix = null, ?array $values = null): string
24
    {
25
        $_SESSION['data'] = (empty($_SESSION['data'])) ? null : $_SESSION['data'];
26
27
        $vars = (is_null($values)) ? $_SESSION['data'] : $values;
28
29
        return (is_null($vars)) ? $buffer : $this->replace_vars($buffer, $vars, $prefix);
30
    }
31
32
    protected function replace_vars($buffer, $vars, $prefix): string
33
    {
34
        foreach ($vars as $key => $value) {
35
            switch(gettype($value)){
36
                case 'string':
37
                case 'int':
38
                case 'integer':
39
                case 'float':
40
                case 'boolean':
41
                case 'NULL':
42
                    $buffer = $this->replace_value($buffer, $value, $prefix, $key);
43
                    break;
44
                case 'array':
45
                    $buffer = $this->replace_Array($buffer, $value, $prefix, $key);
46
                    break;
47
                case 'object':
48
                    $buffer = $this->replace_Object($buffer, $value, $prefix, $key);
49
                    break;
50
                default:
51
                    break;
52
            }
53
        }
54
55
        return $buffer;
56
    }
57
58
    protected function replace_value(string $buffer, $value, string $prefix, string $key): string
59
    {
60
        if(gettype($value)!=='array' && gettype($value)!=='object'){
61
            while(strstr($buffer,'{{ '.$prefix.$key.' }}')){
62
                $buffer = str_replace('{{ '.$prefix.$key.' }}', $value ,$buffer);
63
            }
64
        }
65
        return $buffer;
66
    }
67
68
    protected function replace_Object(string $buffer, object $obj, string $prefix, string $key): string
69
    {
70
        foreach($obj->getFields() as $field => $val){
71
            
72
            $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field);
73
74
            while(strstr($buffer,'{{ '.$prefix.$key.'.'.$field.' }}')){
75
                $buffer = str_replace('{{ '.$prefix.$key.'.'.$field.' }}',$val[$field] ,$buffer);
76
            }
77
        }
78
        return $buffer;
79
    }
80
81
    protected function replace_Array(string $buffer, array $array, string $prefix, string $key): string
82
    {
83
        foreach($array as $field => $val){
84
            $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field);
85
86
            while(strstr($buffer,'{{ '.$prefix.$key.'.'.$field.' }}')){
87
                $buffer = str_replace('{{ '.$prefix.$key.'.'.$field.' }}',$val,$buffer);
88
            }
89
        }
90
        return $buffer;
91
    }
92
93
    protected function removeComments(string $buffer): string
94
    {
95
        while(strstr($buffer,'<!--')){
96
            $comment = substr(
97
                $buffer,
98
                strpos($buffer,'<!--'),
99
                strpos(strstr($buffer,'<!--'),'-->')+3
100
            );
101
            $buffer = str_replace($comment,'',$buffer);
102
        }
103
        return $buffer;
104
    }
105
106
    protected function getImport(string $buffer): string
107
    {
108
        while(strstr($buffer,'@import')){
109
            $buffer = $this->getVars($buffer);
110
111
            $import = substr(
112
                $buffer,
113
                strpos($buffer,'@import(\''),
114
                strpos(strstr($buffer,'@import'),'\')')+2
115
            );
116
117
            $tpl = $this->check_importExist($import);
118
119
            try{
120
                $buffer_tpl = $this->getOB($this->path . DIRECTORY_SEPARATOR . $tpl . '.tpl.php');
121
            }catch(\Exception $er){
122
                var_dump($er);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($er) looks like debug code. Are you sure you do not want to remove it?
Loading history...
123
                die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
124
            }
125
            
126
            $buffer_tpl = $this->getVars($buffer_tpl);
127
            $buffer = str_replace($import,$buffer_tpl,$buffer);
128
        }
129
130
        return $buffer;
131
    }
132
133
    protected function saveData(): bool
134
    {   
135
        if(session_status() !== PHP_SESSION_ACTIVE){
136
            return false;
137
        }
138
        unset($_SESSION['data']);
139
140
        if(!empty($_SESSION['save'])){
141
            foreach ($_SESSION['save'] as $key => $value) {
142
                $_SESSION['data'][$key] = $value;
143
            }
144
        }
145
        return true;
146
    }
147
148
}