Completed
Pull Request — master (#448)
by
unknown
08:31
created

functions.php ➔ percentile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
use PhpParser\Node;
4
5
class MyVisitor extends \PhpParser\NodeVisitorAbstract
6
{
7
8
    /**
9
     * @var callable
10
     */
11
    private $callback;
12
13
    /**
14
     * @param callable $callback
15
     */
16
    public function __construct($callback)
17
    {
18
        $this->callback = $callback;
19
    }
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function leaveNode(\PhpParser\Node $node)
25
    {
26
        call_user_func($this->callback, $node);
27
        return null;
28
    }
29
}
30
31
/**
32
 * @param Node $node
33
 * @param callable $callback
34
 *
35
 * @return void
36
 */
37
function iterate_over_node($node, $callback)
38
{
39
    /*
40
         // way 2
41
         foreach ($node->getSubNodeNames() as $name) {
42
             $subNode = $node->$name;
43
44
45
46
             if ($subNode instanceof \PhpParser\Node) {
47
48
                 return iterate_over_node($node, $callback);
49
             }
50
51
             if (is_array($subNode)) {
52
                 foreach ($subNode as $sub) {
53
                     return iterate_over_node($sub, $callback);
54
                 }
55
             }
56
57
                 return $callback($node);
58
         }
59
60
         return $callback($node);
61
    */
62
63
    /*
64
        // way 1
65
        static $traverser;
66
        if (!isset($traverser)) {
67
            $myVisitor = new MyVisitor($callback);
68
            $traverser = new \PhpParser\NodeTraverser();
69
            $traverser->addVisitor($myVisitor);
70
        }
71
        $traverser->traverse(array($node));
72
        return;
73
    */
74
75
    // way 1
76
    $myVisitor = new MyVisitor($callback);
77
    $traverser = new \PhpParser\NodeTraverser();
78
    $traverser->addVisitor($myVisitor);
79
    $traverser->traverse([$node]);
80
    return;
81
}
82
83
/**
84
 * @param Node|string $node
85
 * @return string|null
86
 */
87
function getNameOfNode($node)
88
{
89
    if (is_string($node)) {
90
        return $node;
91
    }
92
93
    if ($node instanceof \PhpParser\Node\Name\FullyQualified) {
94
        return (string)$node;
95
    }
96
    if ($node instanceof \PhpParser\Node\Expr\New_) {
97
        return getNameOfNode($node->class);
98
    }
99
100
    if (isset($node->class)) {
0 ignored issues
show
Bug introduced by
Accessing class on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
101
        return getNameOfNode($node->class);
0 ignored issues
show
Bug introduced by
Accessing class on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
    }
103
104
    if ($node instanceof \PhpParser\Node\Name) {
105
        return (string)implode($node->parts);
106
    }
107
108
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
109
        return getNameOfNode($node->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
110
    }
111
112
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
        return getNameOfNode($node->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
114
    }
115
116
    if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
117
        return getNameOfNode($node->var);
118
    }
119
120
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
121
        return get_class($node->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
    }
123
124
    if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) {
125
        return getNameOfNode($node->var);
126
    }
127
128
    if (isset($node->name) && !is_string($node->name)) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
        return getNameOfNode($node->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
130
    }
131
132
    if (isset($node->name) && null === $node->name) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
133
        return 'anonymous@' . spl_object_hash($node);
134
    }
135
136
    if (isset($node->name)) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
137
        return (string)$node->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
138
    }
139
140
    return null;
141
}
142
143
/**
144
 * @param string $src
145
 * @param string $dst
146
 *
147
 * @return void
148
 */
149
function recurse_copy($src, $dst)
150
{
151
    $dir = opendir($src);
152
    if ($dir === false) {
153
        return;
154
    }
155
156
    if (!file_exists($dst)) {
157
        mkdir($dst);
158
    }
159
    while (false !== ($file = readdir($dir))) {
160
        if (($file != '.') && ($file != '..')) {
161
            if (is_dir($src . '/' . $file)) {
162
                recurse_copy($src . '/' . $file, $dst . '/' . $file);
163
            } else {
164
                copy($src . '/' . $file, $dst . '/' . $file);
165
            }
166
        }
167
    }
168
    closedir($dir);
169
}
170
171
/**
172
 * @return string
173
 */
174
function getVersion()
175
{
176
    return 'v2.7.4';
177
}
178
179
/**
180
 * @param mixed[] $array
181
 * @param string $attribute
182
 * @param mixed $currentValue
183
 * @return float
184
 */
185
function gradientAlphaFor($array, $attribute, $currentValue)
186
{
187
    // memory cache
188
    static $caches;
189
    if(null === $caches) {
190
        $caches = [];
191
    }
192
193
    if(!isset($caches[$attribute])) {
194
        // avoid to iterate over array too many times
195
        $max = 0;
196
        $min = 1;
197
        foreach($array as $item) {
198
            if(!isset($item[$attribute])) {
199
                continue;
200
            }
201
202
            $max = max($max, $item[$attribute]);
203
            $min = min($min, $item[$attribute]);
204
        }
205
206
        $caches[$attribute]['max'] = $max;
207
        $caches[$attribute]['min'] = $min;
208
    }
209
210
    $max = $caches[$attribute]['max'];
211
    $min = $caches[$attribute]['min'];
212
213
    $percent = (($currentValue - $min) * 100) / (max(1, $max - $min));
214
    return round($percent / 100, 2);
215
}
216
217
/**
218
 * Style an element according its position in range
219
 *
220
 * @param mixed[] $array
221
 * @param string $attribute
222
 * @param mixed $currentValue
223
 * @return string
224
 */
225
function gradientStyleFor($array, $attribute, $currentValue) {
226
    return sprintf(' style="background-color: hsla(203, 82%%, 76%%, %s);"', gradientAlphaFor($array, $attribute, $currentValue));
227
}
228
229
/**
230
 * Calculate percentalies
231
 *
232
 * @param float[]|int[] $arr
233
 * @param float $percentile
234
 * @return mixed
235
 */
236
function percentile($arr, $percentile = 0.95)
237
{
238
    sort($arr);
239
    return $arr[max(round($percentile * count($arr) - 1.0 - $percentile), 0)];
240
}
241