GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9e1348...84c946 )
by Zordius
02:19
created

Exporter::safestring()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/*
3
4
Copyrights for code authored by Yahoo! Inc. is licensed under the following terms:
5
MIT License
6
Copyright (c) 2013-2016 Yahoo! Inc. All Rights Reserved.
7
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
11
Origin: https://github.com/zordius/lightncandy
12
*/
13
14
/**
15
 * file to keep LightnCandy Exporter
16
 *
17
 * @package    LightnCandy
18
 * @author     Zordius <[email protected]>
19
 */
20
21
namespace LightnCandy;
22
23
/**
24
 * LightnCandy major static class
25
 */
26
class Exporter
27
{
28
    /**
29
     * Get PHP code string from a closure of function as string
30
     *
31
     * @param array<string,array|string|integer> $context current compile context
32
     * @param object $closure Closure object
33
     *
34
     * @return string
35
     *
36
     * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)),  function ($a) {return;}
37
     * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)),   function ($a) {return;}
38
     */
39 73
    protected static function closure($context, $closure) {
40 73
        if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
41 2
            $ref = new \ReflectionMethod($matched[1], $matched[2]);
42
        } else {
43 72
            $ref = new \ReflectionFunction($closure);
44
        }
45 73
        $meta = static::getMeta($ref);
46
47 73
        return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*\\((.+)\\}.*?\s*$/s', 'function($2}', static::replaceSafeString($context, $meta['code']));
48
    }
49
50
    /**
51
     * Export required custom helper functions
52
     *
53
     * @param array<string,array|string|integer> $context current compile context
54
     *
55
     * @return string
56
     */
57 675
    public static function helpers($context) {
58 675
        $ret = '';
59 675
        foreach ($context['helpers'] as $name => $func) {
0 ignored issues
show
Bug introduced by
The expression $context['helpers'] of type array|string|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
60 194
            if (!isset($context['usedCount']['helpers'][$name])) {
61 35
                continue;
62
            }
63 180
            if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) {
64 72
                $ret .= ("            '$name' => " . static::closure($context, $func) . ",\n");
65 72
                continue;
66
            }
67 108
            $ret .= "            '$name' => '$func',\n";
68
        }
69
70 675
        return "array($ret)";
71
    }
72
73
    /**
74
     * Replace SafeString class with alias class name
75
     *
76
     * @param array<string,array|string|integer> $context current compile context
77
     * @param string $str the PHP code to be replaced
78
     *
79
     * @return string
80
     */
81 573
    protected static function replaceSafeString($context, $str) {
82 573
        return $context['flags']['standalone'] ? str_replace($context['safestring'], $context['safestringalias'], $str) : $str;
83
    }
84
85
    /**
86
     * Get methods from ReflectionClass
87
     *
88
     * @param array<string,array|string|integer> $context current compile context
89
     * @param object $class instance of the ReflectionClass
90
     *
91
     * @return array
92
     */
93 513
    public static function getClassMethods($context, $class) {
94 513
        $methods = array();
95
96 513
        foreach ($class->getMethods() as $method) {
97 513
            $meta = static::getMeta($method);
98 513
            $methods[$meta['name']] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", $meta['code']));
99
        }
100
101 513
        return $methods;
102
    }
103
104
    /**
105
     * Get statics code from ReflectionClass
106
     *
107
     * @param object $class instance of the ReflectionClass
108
     *
109
     * @return string
110
     */
111 336
    public static function getClassStatics($class) {
112 336
        $ret = '';
113
114 336
        foreach ($class->getStaticProperties() as $name => $value) {
115 336
            $ret .= " public static \${$name} = " . var_export($value, true) . ";\n";
116
        }
117
118 336
        return $ret;
119
    }
120
121
122
123
124
125
    /**
126
     * Get metadata from ReflectionObject
127
     *
128
     * @param object $refobj instance of the ReflectionObject
129
     *
130
     * @return array
131
     */
132 573
    public static function getMeta($refobj) {
133 573
        $fname = $refobj->getFileName();
134 573
        $lines = file_get_contents($fname);
135 573
        $file = new \SplFileObject($fname);
136 573
        $file->seek($refobj->getStartLine() - 2);
137 573
        $spos = $file->ftell();
138 573
        $file->seek($refobj->getEndLine() - 1);
139 573
        $epos = $file->ftell();
140 573
        unset($file);
141
        return array(
142 573
            'name' => $refobj->getName(),
143 573
            'code' => substr($lines, $spos, $epos - $spos)
144
        );
145
    }
146
147
    /**
148
     * Export SafeString class as string
149
     *
150
     * @param array<string,array|string|integer> $context current compile context
151
     *
152
     * @return string
153
     */
154 336
    public static function safestring($context) {
155 336
        $class = new \ReflectionClass($context['safestring']);
156
157
        return array_reduce(static::getClassMethods($context, $class), function ($in, $cur) {
158 336
            return $in . $cur[0];
159 336
        }, "if (!class_exists(\"" . addslashes($context['safestringalias']) . "\")) {\nclass {$context['safestringalias']} {\n" . static::getClassStatics($class)) . "}\n}\n";
160
    }
161
162
    /**
163
     * Export required standalone Runtime methods
164
     *
165
     * @param array<string,array|string|integer> $context current compile context
166
     *
167
     * @return string
168
     */
169 513
    public static function runtime($context) {
170 513
        $class = new \ReflectionClass($context['runtime']);
171 513
        $ret = '';
172 513
        $methods = static::getClassMethods($context, $class);
173
174 513
        $exports = array_keys($context['usedCount']['runtime']);
175
176 513
        while (true) {
177
            if (array_sum(array_map(function ($name) use (&$exports, $methods) {
178 469
                $n = 0;
179 469
                foreach ($methods[$name][1] as $child => $count) {
180 468
                    if (!in_array($child, $exports)) {
181 468
                       $exports[] = $child;
182 468
                       $n++;
183
                    }
184
                }
185 469
                return $n;
186 513
            }, $exports)) == 0) {
187 513
                break;
188
            }
189
        }
190
191 513
        foreach ($exports as $export) {
192 469
            $ret .= ($methods[$export][0] . "\n");
193
        }
194
195 513
        return $ret;
196
    }
197
198
    /**
199
     * Export Runtime constants
200
     *
201
     * @param array<string,array|string|integer> $context current compile context
202
     *
203
     * @return string
204
     */
205 675
    public static function constants($context) {
206 675
        if ($context['flags']['standalone'] == 0) {
207 651
            return 'array()';
208
        }
209
210 513
        $class = new \ReflectionClass($context['runtime']);
211 513
        $constants = $class->getConstants();
212 513
        $ret = " array(\n";
213 513
        foreach($constants as $name => $value) {
214 513
            $ret .= "            '$name' => ".  (is_string($value) ? "'$value'" : $value ) . ",\n";
215
        }
216 513
        $ret .= "        )";
217 513
        return $ret;
218
    }
219
220
    /**
221
     * Scan for required standalone functions
222
     *
223
     * @param array<string,array|string|integer> $context current compile context
224
     * @param string $code PHP code string of the method
225
     *
226
     * @return array<string|array> list of converted code and children array
227
     */
228 513
    protected static function scanDependency($context, $code) {
229 513
        $child = array();
230
231 513
        $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) {
232 513
            if (!isset($child[$matches[1]])) {
233 513
                $child[$matches[1]] = 0;
234
            }
235 513
            $child[$matches[1]]++;
236
237 513
            return "{$context['funcprefix']}{$matches[1]}(";
238 513
        }, $code);
239
240
        // replace the constants
241 513
        $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code);
242
243
        // compress space
244 513
        $code = preg_replace('/    /', ' ', $code);
245
246 513
        return array(static::replaceSafeString($context, $code), $child);
247
    }
248
}
249
250