Passed
Push — master ( 778cd6...f3a7d9 )
by Edson
01:42
created

Evaluate::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace EdsonOnildo\Tpl\Tag;
4
5
use EdsonOnildo\Tpl\Tpl;
6
7
class Evaluate extends Tag
8
{
9
    public function __construct()
10
    {
11
        $this->eval0();
12
        $this->eval1();
13
        $this->eval2();
14
        $this->eval3();
15
        $this->expression();
16
        $this->eval4();
17
    }
18
19
    private function eval0(): void
20
    {
21
        $search = "/\@{{(\s?)+([\w\.\(\)]+)(\s?)+}}/is";
22
23
        Tag::match($search, function($var) {
24
25
            Tag::replace("@@ $var @@");
26
        });
27
    }
28
29
    private function eval4(): void
30
    {
31
        $search = "/\@\@(\s?)+([\w\.\(\)]+)(\s?)+\@\@/is";
32
33
        Tag::match($search, function($var) {
34
35
            Tag::replace("{{ $var }}");
36
        });
37
    }
38
39
    private function eval1(): void
40
    {
41
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+}}/is";
42
43
        Tag::match($search, function($var) {
44
45
            $var = str_replace('.', '->', $var);
46
47
            Tag::replace("<?= \$$var ?>");
48
        });
49
    }
50
51
    private function eval2(): void
52
    {
53
        $search = "/{{(\s?)+([\w\.\(\)]+)(\s?)+\|(\s?)+([\w| ]+)(\s?)+}}/is";
54
55
        Tag::match($search, function($var, $filter) {
56
57
            $var = str_replace('.', '->', $var);
58
59
            $filters = explode('|', $filter);
60
61
            $res = "<?= ";
62
            
63
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
64
                $res .= "$filter(";
65
            }
66
67
            $res .= "$$var";
68
69
            foreach ($filters as $filter) {
0 ignored issues
show
introduced by
$filter is overwriting one of the parameters of this function.
Loading history...
70
                $res .= ")";
71
            }
72
73
            $res .= " ?>";
74
75
            Tag::replace($res);
76
        });
77
    }
78
79
    private function eval3(): void
80
    {
81
        $search = "/{{(\s?)+([\w]+)(\s?)+\((.*?)\)(\s?)+}}/is";
82
83
        Tag::match($search, function($func, $params) {
84
85
            if (method_exists(__CLASS__, $func)) {
86
                $func = __CLASS__ . "::$func";
87
            }
88
89
            Tag::replace("<?= $func($params) ?>");
90
        });
91
    }
92
93
    private function expression(): void
94
    {
95
        Tag::match("/{{(\s?)+(.*?)(\s?)+}}/is", function($expr) {
96
            Tag::replace("<?= $expr ?>");
97
        });
98
    }
99
100
    public static function style(String $asset): String
101
    {
102
        return '<link rel="stylesheet" href="'.Tpl::getUrl().Tpl::getAssets().$asset.'">';
103
    }
104
105
    public static function script(String $asset): String
106
    {
107
        return '<script src="'.Tpl::getUrl().Tpl::getAssets().$asset.'"></script>';
108
    }
109
110
    public static function assets(String $asset): String
111
    {
112
        return Tpl::getUrl() . Tpl::getAssets() . $asset;
113
    }
114
115
    public static function url(String $url): String
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

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

115
    public static function url(/** @scrutinizer ignore-unused */ String $url): String

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        return Tpl::getUrl();
118
    }
119
}
120