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 — gh-pages ( ef0aaf...67b915 )
by Zordius
11s
created

Exporter::stringobject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/*
3
4
MIT License
5
Copyright 2013-2018 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 79
    protected static function closure($context, $closure) {
39 79
        if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
40 2
            $ref = new \ReflectionMethod($matched[1], $matched[2]);
41
        } else {
42 78
            $ref = new \ReflectionFunction($closure);
43
        }
44 79
        $meta = static::getMeta($ref);
45
46 79
        return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*\\((.+)\\}.*?\s*$/s', 'function($2}', static::replaceSafeString($context, $meta['code']));
47
    }
48
49
    /**
50
     * Export required custom helper functions
51
     *
52
     * @param array<string,array|string|integer> $context current compile context
53
     *
54
     * @return string
55
     */
56 714
    public static function helpers($context) {
57 714
        $ret = '';
58 714
        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...
59 201
            if (!isset($context['usedCount']['helpers'][$name])) {
60 36
                continue;
61
            }
62 186
            if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) {
63 78
                $ret .= ("            '$name' => " . static::closure($context, $func) . ",\n");
64 78
                continue;
65
            }
66 108
            $ret .= "            '$name' => '$func',\n";
67
        }
68
69 714
        return "array($ret)";
70
    }
71
72
    /**
73
     * Replace SafeString class with alias class name
74
     *
75
     * @param array<string,array|string|integer> $context current compile context
76
     * @param string $str the PHP code to be replaced
77
     *
78
     * @return string
79
     */
80 579
    protected static function replaceSafeString($context, $str) {
81 579
        return $context['flags']['standalone'] ? str_replace($context['safestring'], $context['safestringalias'], $str) : $str;
82
    }
83
84
    /**
85
     * Get methods from ReflectionClass
86
     *
87
     * @param array<string,array|string|integer> $context current compile context
88
     * @param \ReflectionClass $class instance of the ReflectionClass
89
     *
90
     * @return array
91
     */
92 513
    public static function getClassMethods($context, $class) {
93 513
        $methods = array();
94
95 513
        foreach ($class->getMethods() as $method) {
96 513
            $meta = static::getMeta($method);
97 513
            $methods[$meta['name']] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", $meta['code']), $meta['code']);
98
        }
99
100 513
        return $methods;
101
    }
102
103
    /**
104
     * Get statics code from ReflectionClass
105
     *
106
     * @param \ReflectionClass $class instance of the ReflectionClass
107
     *
108
     * @return string
109
     */
110 336
    public static function getClassStatics($class) {
111 336
        $ret = '';
112
113 336
        foreach ($class->getStaticProperties() as $name => $value) {
114 336
            $ret .= " public static \${$name} = " . var_export($value, true) . ";\n";
115
        }
116
117 336
        return $ret;
118
    }
119
120
121
122
123
124
    /**
125
     * Get metadata from ReflectionObject
126
     *
127
     * @param object $refobj instance of the ReflectionObject
128
     *
129
     * @return array
130
     */
131 579
    public static function getMeta($refobj) {
132 579
        $fname = $refobj->getFileName();
133 579
        $lines = file_get_contents($fname);
134 579
        $file = new \SplFileObject($fname);
135 579
        $file->seek($refobj->getStartLine() - 2);
136 579
        $spos = $file->ftell();
137 579
        $file->seek($refobj->getEndLine() - 1);
138 579
        $epos = $file->ftell();
139 579
        unset($file);
140
        return array(
141 579
            'name' => $refobj->getName(),
142 579
            'code' => substr($lines, $spos, $epos - $spos)
143
        );
144
    }
145
146
    /**
147
     * Export SafeString class as string
148
     *
149
     * @param array<string,array|string|integer> $context current compile context
150
     *
151
     * @return string
152
     */
153 336
    public static function safestring($context) {
154 336
        $class = new \ReflectionClass($context['safestring']);
155
156
        return array_reduce(static::getClassMethods($context, $class), function ($in, $cur) {
157 336
            return $in . $cur[2];
158 336
        }, "if (!class_exists(\"" . addslashes($context['safestringalias']) . "\")) {\nclass {$context['safestringalias']} {\n" . static::getClassStatics($class)) . "}\n}\n";
159
    }
160
161
    /**
162
     * Export StringObject class as string
163
     *
164
     * @param array<string,array|string|integer> $context current compile context
165
     *
166
     * @return string
167
     */
168 380
    public static function stringobject($context) {
169 380
      if ($context['flags']['standalone'] == 0) {
170 380
        return 'use \\LightnCandy\\StringObject as StringObject;';
171
      }
172 373
      $class = new \ReflectionClass('\\LightnCandy\\StringObject');
173 373
      $meta = static::getMeta($class);
174 373
      $methods = array();
0 ignored issues
show
Unused Code introduced by
$methods is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175 373
      return "if (!class_exists(\"StringObject\")) {\n{$meta['code']}}\n";
176
    }
177
178
    /**
179
     * Export required standalone Runtime methods
180
     *
181
     * @param array<string,array|string|integer> $context current compile context
182
     *
183
     * @return string
184
     */
185 513
    public static function runtime($context) {
186 513
        $class = new \ReflectionClass($context['runtime']);
187 513
        $ret = '';
188 513
        $methods = static::getClassMethods($context, $class);
189
190 513
        $exports = array_keys($context['usedCount']['runtime']);
191
192 513
        while (true) {
193
            if (array_sum(array_map(function ($name) use (&$exports, $methods) {
194 469
                $n = 0;
195 469
                foreach ($methods[$name][1] as $child => $count) {
196 468
                    if (!in_array($child, $exports)) {
197 468
                       $exports[] = $child;
198 468
                       $n++;
199
                    }
200
                }
201 469
                return $n;
202 513
            }, $exports)) == 0) {
203 513
                break;
204
            }
205
        }
206
207 513
        foreach ($exports as $export) {
208 469
            $ret .= ($methods[$export][0] . "\n");
209
        }
210
211 513
        return $ret;
212
    }
213
214
    /**
215
     * Export Runtime constants
216
     *
217
     * @param array<string,array|string|integer> $context current compile context
218
     *
219
     * @return string
220
     */
221 714
    public static function constants($context) {
222 714
        if ($context['flags']['standalone'] == 0) {
223 690
            return 'array()';
224
        }
225
226 513
        $class = new \ReflectionClass($context['runtime']);
227 513
        $constants = $class->getConstants();
228 513
        $ret = " array(\n";
229 513
        foreach($constants as $name => $value) {
230 513
            $ret .= "            '$name' => ".  (is_string($value) ? "'$value'" : $value ) . ",\n";
231
        }
232 513
        $ret .= "        )";
233 513
        return $ret;
234
    }
235
236
    /**
237
     * Scan for required standalone functions
238
     *
239
     * @param array<string,array|string|integer> $context current compile context
240
     * @param string $code patched PHP code string of the method
241
     * @param string $ocode original PHP code string of the method
242
     *
243
     * @return array<string|array> list of converted code and children array
244
     */
245 513
    protected static function scanDependency($context, $code, $ocode) {
246 513
        $child = array();
247
248
        $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) {
249 513
            if (!isset($child[$matches[1]])) {
250 513
                $child[$matches[1]] = 0;
251
            }
252 513
            $child[$matches[1]]++;
253
254 513
            return "{$context['funcprefix']}{$matches[1]}(";
255 513
        }, $code);
256
257
        // replace the constants
258 513
        $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code);
259
260
        // compress space
261 513
        $code = preg_replace('/    /', ' ', $code);
262
263 513
        return array(static::replaceSafeString($context, $code), $child, $ocode);
264
    }
265
}
266
267