Completed
Pull Request — master (#421)
by personal
01:32
created

functions.php ➔ gradientStyleFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class MyVisitor extends \PhpParser\NodeVisitorAbstract
4
{
5
6
    /**
7
     * @var
8
     */
9
    private $callback;
10
11
    /**
12
     * @param $callback
13
     */
14
    public function __construct($callback)
15
    {
16
        $this->callback = $callback;
17
    }
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function leaveNode(\PhpParser\Node $node)
23
    {
24
        call_user_func($this->callback, $node);
25
    }
26
}
27
28
/**
29
 * @param $node
30
 * @param $callback
31
 */
32
function iterate_over_node($node, $callback)
33
{
34
    /*
35
         // way 2
36
         foreach ($node->getSubNodeNames() as $name) {
37
             $subNode = $node->$name;
38
39
40
41
             if ($subNode instanceof \PhpParser\Node) {
42
43
                 return iterate_over_node($node, $callback);
44
             }
45
46
             if (is_array($subNode)) {
47
                 foreach ($subNode as $sub) {
48
                     return iterate_over_node($sub, $callback);
49
                 }
50
             }
51
52
                 return $callback($node);
53
         }
54
55
         return $callback($node);
56
    */
57
58
    /*
59
        // way 1
60
        static $traverser;
61
        if (!isset($traverser)) {
62
            $myVisitor = new MyVisitor($callback);
63
            $traverser = new \PhpParser\NodeTraverser();
64
            $traverser->addVisitor($myVisitor);
65
        }
66
        $traverser->traverse(array($node));
67
        return;
68
    */
69
70
    // way 1
71
    $myVisitor = new MyVisitor($callback);
72
    $traverser = new \PhpParser\NodeTraverser();
73
    $traverser->addVisitor($myVisitor);
74
    $traverser->traverse([$node]);
75
    return;
76
}
77
78
/**
79
 * @param $node
80
 * @return string|null
81
 */
82
function getNameOfNode($node)
83
{
84
    if (is_string($node)) {
85
        return $node;
86
    }
87
88
    if ($node instanceof \PhpParser\Node\Name\FullyQualified) {
89
        return (string)$node;
90
    }
91
    if ($node instanceof \PhpParser\Node\Expr\New_) {
92
        return getNameOfNode($node->class);
93
    }
94
95
    if (isset($node->class)) {
96
        return getNameOfNode($node->class);
97
    }
98
99
    if ($node instanceof \PhpParser\Node\Name) {
100
        return (string)implode($node->parts);
101
    }
102
103
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) {
104
        return getNameOfNode($node->name);
105
    }
106
107
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) {
108
        return getNameOfNode($node->name);
109
    }
110
111
    if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
112
        return getNameOfNode($node->var);
113
    }
114
115
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) {
116
        return get_class($node->name);
117
    }
118
119
    if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) {
120
        return getNameOfNode($node->var);
121
    }
122
123
    if (isset($node->name) && !is_string($node->name)) {
124
        return getNameOfNode($node->name);
125
    }
126
127
    if (isset($node->name) && null === $node->name) {
128
        return 'anonymous@' . spl_object_hash($node);
129
    }
130
131
    if (isset($node->name)) {
132
        return (string)$node->name;
133
    }
134
135
    return null;
136
}
137
138
/**
139
 * @param $src
140
 * @param $dst
141
 */
142
function recurse_copy($src, $dst)
143
{
144
    $dir = opendir($src);
145
    if (!file_exists($dst)) {
146
        mkdir($dst);
147
    }
148
    while (false !== ($file = readdir($dir))) {
149
        if (($file != '.') && ($file != '..')) {
150
            if (is_dir($src . '/' . $file)) {
151
                recurse_copy($src . '/' . $file, $dst . '/' . $file);
152
            } else {
153
                copy($src . '/' . $file, $dst . '/' . $file);
154
            }
155
        }
156
    }
157
    closedir($dir);
158
}
159
160
/**
161
 * @return string
162
 */
163
function getVersion()
164
{
165
    return 'v2.6.0';
166
}
167
168
/**
169
 * @param array $array
170
 * @param string $attribute
171
 * @param mixed $currentValue
172
 * @return false|float
173
 */
174
function gradientAlphaFor($array, $attribute, $currentValue)
175
{
176
    // memory cache
177
    static $caches;
178
    if(null === $caches) {
179
        $caches = [];
180
    }
181
182
    if(!isset($caches[$attribute])) {
183
        // avoid to iterate over array too many times
184
        $max = 0;
185
        $min = 1;
186
        foreach($array as $item) {
187
            if(!isset($item[$attribute])) {
188
                continue;
189
            }
190
191
            $max = max($max, $item[$attribute]);
192
            $min = min($min, $item[$attribute]);
193
        }
194
195
        $caches[$attribute]['max'] = $max;
196
        $caches[$attribute]['min'] = $min;
197
    }
198
199
    $max = $caches[$attribute]['max'];
200
    $min = $caches[$attribute]['min'];
201
202
    $percent = (($currentValue - $min) * 100) / (max(1, $max - $min));
203
    return round($percent / 100, 2);
204
}
205
206
/**
207
 * Style an element according its position in range
208
 *
209
 * @param array $array
210
 * @param string $attribute
211
 * @param mixed $currentValue
212
 * @return string
213
 */
214
function gradientStyleFor($array, $attribute, $currentValue) {
215
    return sprintf(' style="background-color: hsla(203, 82%%, 76%%, %s);"', gradientAlphaFor($array, $attribute, $currentValue));
216
}
217