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.

Exporter   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 0
dl 0
loc 251
ccs 87
cts 87
cp 1
rs 9.92
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A closure() 0 11 3
A helpers() 0 16 6
A replaceSafeString() 0 4 2
A getClassMethods() 0 11 2
A getClassStatics() 0 10 2
A getMeta() 0 15 1
A safestring() 0 8 1
B runtime() 0 29 6
A constants() 0 15 4
A scanDependency() 0 21 2
A stringobject() 0 9 2
1
<?php
2
/*
3
4
MIT License
5
Copyright 2013-2020 Zordius Chen. All Rights Reserved.
6
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:
7
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
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.
9
10
Origin: https://github.com/zordius/lightncandy
11
*/
12
13
/**
14
 * file to keep LightnCandy Exporter
15
 *
16
 * @package    LightnCandy
17
 * @author     Zordius <[email protected]>
18
 */
19
20
namespace LightnCandy;
21
22
/**
23
 * LightnCandy major static class
24
 */
25
class Exporter
26
{
27
    /**
28
     * Get PHP code string from a closure of function as string
29
     *
30
     * @param array<string,array|string|integer> $context current compile context
31
     * @param object $closure Closure object
32
     *
33
     * @return string
34
     *
35
     * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)),  function ($a) {return;}
36
     * @expect 'function($a) {return;}' when input array('flags' => array('standalone' => 0)),   function ($a) {return;}
37
     */
38 80
    protected static function closure($context, $closure)
39
    {
40 80
        if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
41 2
            $ref = new \ReflectionMethod($matched[1], $matched[2]);
42
        } else {
43 79
            $ref = new \ReflectionFunction($closure);
44
        }
45 80
        $meta = static::getMeta($ref);
46
47 80
        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 717
    public static function helpers($context)
58
    {
59 717
        $ret = '';
60 717
        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...
61 202
            if (!isset($context['usedCount']['helpers'][$name])) {
62 36
                continue;
63
            }
64 187
            if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) {
65 79
                $ret .= ("            '$name' => " . static::closure($context, $func) . ",\n");
66 79
                continue;
67
            }
68 108
            $ret .= "            '$name' => '$func',\n";
69
        }
70
71 717
        return "array($ret)";
72
    }
73
74
    /**
75
     * Replace SafeString class with alias class name
76
     *
77
     * @param array<string,array|string|integer> $context current compile context
78
     * @param string $str the PHP code to be replaced
79
     *
80
     * @return string
81
     */
82 580
    protected static function replaceSafeString($context, $str)
83
    {
84 580
        return $context['flags']['standalone'] ? str_replace($context['safestring'], $context['safestringalias'], $str) : $str;
85
    }
86
87
    /**
88
     * Get methods from ReflectionClass
89
     *
90
     * @param array<string,array|string|integer> $context current compile context
91
     * @param \ReflectionClass $class instance of the ReflectionClass
92
     *
93
     * @return array
94
     */
95 513
    public static function getClassMethods($context, $class)
96
    {
97 513
        $methods = array();
98
99 513
        foreach ($class->getMethods() as $method) {
100 513
            $meta = static::getMeta($method);
101 513
            $methods[$meta['name']] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", $meta['code']), $meta['code']);
102
        }
103
104 513
        return $methods;
105
    }
106
107
    /**
108
     * Get statics code from ReflectionClass
109
     *
110
     * @param \ReflectionClass $class instance of the ReflectionClass
111
     *
112
     * @return string
113
     */
114 336
    public static function getClassStatics($class)
115
    {
116 336
        $ret = '';
117
118 336
        foreach ($class->getStaticProperties() as $name => $value) {
119 336
            $ret .= " public static \${$name} = " . var_export($value, true) . ";\n";
120
        }
121
122 336
        return $ret;
123
    }
124
125
126
127
128
129
    /**
130
     * Get metadata from ReflectionObject
131
     *
132
     * @param object $refobj instance of the ReflectionObject
133
     *
134
     * @return array
135
     */
136 580
    public static function getMeta($refobj)
137
    {
138 580
        $fname = $refobj->getFileName();
139 580
        $lines = file_get_contents($fname);
140 580
        $file = new \SplFileObject($fname);
141 580
        $file->seek($refobj->getStartLine() - 2);
142 580
        $spos = $file->ftell();
143 580
        $file->seek($refobj->getEndLine() - 1);
144 580
        $epos = $file->ftell();
145 580
        unset($file);
146
        return array(
147 580
            'name' => $refobj->getName(),
148 580
            'code' => substr($lines, $spos, $epos - $spos)
149
        );
150
    }
151
152
    /**
153
     * Export SafeString class as string
154
     *
155
     * @param array<string,array|string|integer> $context current compile context
156
     *
157
     * @return string
158
     */
159 336
    public static function safestring($context)
160
    {
161 336
        $class = new \ReflectionClass($context['safestring']);
162
163
        return array_reduce(static::getClassMethods($context, $class), function ($in, $cur) {
164 336
            return $in . $cur[2];
165 336
        }, "if (!class_exists(\"" . addslashes($context['safestringalias']) . "\")) {\nclass {$context['safestringalias']} {\n" . static::getClassStatics($class)) . "}\n}\n";
166
    }
167
168
    /**
169
     * Export StringObject class as string
170
     *
171
     * @param array<string,array|string|integer> $context current compile context
172
     *
173
     * @return string
174
     */
175 380
    public static function stringobject($context)
176
    {
177 380
        if ($context['flags']['standalone'] == 0) {
178 380
            return 'use \\LightnCandy\\StringObject as StringObject;';
179
        }
180 373
        $class = new \ReflectionClass('\\LightnCandy\\StringObject');
181 373
        $meta = static::getMeta($class);
182 373
        return "if (!class_exists(\"StringObject\")) {\n{$meta['code']}}\n";
183
    }
184
185
    /**
186
     * Export required standalone Runtime methods
187
     *
188
     * @param array<string,array|string|integer> $context current compile context
189
     *
190
     * @return string
191
     */
192 513
    public static function runtime($context)
193
    {
194 513
        $class = new \ReflectionClass($context['runtime']);
195 513
        $ret = '';
196 513
        $methods = static::getClassMethods($context, $class);
197
198 513
        $exports = array_keys($context['usedCount']['runtime']);
199
200 513
        while (true) {
201
            if (array_sum(array_map(function ($name) use (&$exports, $methods) {
202 469
                $n = 0;
203 469
                foreach ($methods[$name][1] as $child => $count) {
204 468
                    if (!in_array($child, $exports)) {
205 468
                        $exports[] = $child;
206 468
                        $n++;
207
                    }
208
                }
209 469
                return $n;
210 513
            }, $exports)) == 0) {
211 513
                break;
212
            }
213
        }
214
215 513
        foreach ($exports as $export) {
216 469
            $ret .= ($methods[$export][0] . "\n");
217
        }
218
219 513
        return $ret;
220
    }
221
222
    /**
223
     * Export Runtime constants
224
     *
225
     * @param array<string,array|string|integer> $context current compile context
226
     *
227
     * @return string
228
     */
229 717
    public static function constants($context)
230
    {
231 717
        if ($context['flags']['standalone'] == 0) {
232 693
            return 'array()';
233
        }
234
235 513
        $class = new \ReflectionClass($context['runtime']);
236 513
        $constants = $class->getConstants();
237 513
        $ret = " array(\n";
238 513
        foreach ($constants as $name => $value) {
239 513
            $ret .= "            '$name' => ".  (is_string($value) ? "'$value'" : $value) . ",\n";
240
        }
241 513
        $ret .= "        )";
242 513
        return $ret;
243
    }
244
245
    /**
246
     * Scan for required standalone functions
247
     *
248
     * @param array<string,array|string|integer> $context current compile context
249
     * @param string $code patched PHP code string of the method
250
     * @param string $ocode original PHP code string of the method
251
     *
252
     * @return array<string|array> list of converted code and children array
253
     */
254 513
    protected static function scanDependency($context, $code, $ocode)
255
    {
256 513
        $child = array();
257
258
        $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) {
259 513
            if (!isset($child[$matches[1]])) {
260 513
                $child[$matches[1]] = 0;
261
            }
262 513
            $child[$matches[1]]++;
263
264 513
            return "{$context['funcprefix']}{$matches[1]}(";
265 513
        }, $code);
266
267
        // replace the constants
268 513
        $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code);
269
270
        // compress space
271 513
        $code = preg_replace('/    /', ' ', $code);
272
273 513
        return array(static::replaceSafeString($context, $code), $child, $ocode);
274
    }
275
}
276