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 ( 24d0ed...428a72 )
by Zordius
03:29
created

Exporter::replaceSafeString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
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 64
    protected static function closure($context, $closure) {
40 64
        if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
41 2
            $ref = new \ReflectionMethod($matched[1], $matched[2]);
42
        } else {
43 63
            $ref = new \ReflectionFunction($closure);
44
        }
45 64
        $fname = $ref->getFileName();
46
47 64
        $lines = file_get_contents($fname);
48 64
        $file = new \SplFileObject($fname);
49 64
        $file->seek($ref->getStartLine() - 2);
50 64
        $spos = $file->ftell();
51 64
        $file->seek($ref->getEndLine() - 1);
52 64
        $epos = $file->ftell();
53
54 64
        return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*?\\((.+?)\\}[,\\s]*;?$/s', 'function($2}', static::replaceSafeString($context, substr($lines, $spos, $epos - $spos)));
55
    }
56
57
    /**
58
     * Export required custom helper functions
59
     *
60
     * @param array<string,array|string|integer> $context current compile context
61
     *
62
     * @return string
63
     */
64 652
    public static function helpers($context) {
65 652
        $ret = '';
66 652
        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...
67 185
            if (!isset($context['usedCount']['helpers'][$name])) {
68 33
                continue;
69
            }
70 171
            if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) {
71 63
                $ret .= ("            '$name' => " . static::closure($context, $func) . ",\n");
72 63
                continue;
73
            }
74 108
            $ret .= "            '$name' => '$func',\n";
75
        }
76
77 652
        return "array($ret)";
78
    }
79
80
    /**
81
     * Replace SafeString class with alias class name
82
     *
83
     * @param array<string,array|string|integer> $context current compile context
84
     * @param string $str the PHP code to be replaced
85
     *
86
     * @return string
87
     */
88 567
    protected static function replaceSafeString($context, $str) {
89 567
        return $context['flags']['standalone'] ? str_replace($context['safestring'], $context['safestringalias'], $str) : $str;
90
    }
91
92
    /**
93
     * Export SafeString class as string
94
     *
95
     * @param array<string,array|string|integer> $context current compile context
96
     *
97
     * @return string
98
     */
99 342
    public static function safestring($context) {
100 342
        $class = new \ReflectionClass($context['safestring']);
101 342
        $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...
102 342
        $ret = "if (!class_exists(\"" . addslashes($context['safestringalias']) . "\")) {\nclass {$context['safestringalias']} {\n";
103
104 342
        foreach ($class->getMethods() as $method) {
105 342
            $C = $method->getDeclaringClass();
106 342
            $fname = $C->getFileName();
107 342
            $lines = file_get_contents($fname);
108 342
            $file = new \SplFileObject($fname);
109 342
            $name = $method->getName();
110 342
            if ($name === 'stripExtendedComments') {
111 342
                continue;
112
            }
113 342
            $file->seek($method->getStartLine() - 2);
114 342
            $spos = $file->ftell();
115 342
            $file->seek($method->getEndLine() - 1);
116 342
            $epos = $file->ftell();
117 342
            $ret .= substr($lines, $spos, $epos - $spos);
118
        }
119 342
        unset($file);
120
121 342
        return "$ret}\n}\n";
122
    }
123
124
    /**
125
     * Export required standalone Runtime methods
126
     *
127
     * @param array<string,array|string|integer> $context current compile context
128
     *
129
     * @return string
130
     */
131 519
    public static function runtime($context) {
132 519
        $class = new \ReflectionClass($context['runtime']);
133 519
        $methods = array();
134 519
        $ret = '';
135
136 519
        foreach ($class->getMethods() as $method) {
137 519
            $C = $method->getDeclaringClass();
138 519
            $fname = $C->getFileName();
139 519
            $lines = file_get_contents($fname);
140 519
            $file = new \SplFileObject($fname);
141 519
            $name = $method->getName();
142 519
            $file->seek($method->getStartLine() - 2);
143 519
            $spos = $file->ftell();
144 519
            $file->seek($method->getEndLine() - 2);
145 519
            $epos = $file->ftell();
146 519
            $methods[$name] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", substr($lines, $spos, $epos - $spos)));
147
        }
148 519
        unset($file);
149
150 519
        $exports = array_keys($context['usedCount']['runtime']);
151
152 519
        while (true) {
153
            if (array_sum(array_map(function ($name) use (&$exports, $methods) {
154 477
                $n = 0;
155 477
                foreach ($methods[$name][1] as $child => $count) {
156 474
                    if (!in_array($child, $exports)) {
157 474
                       $exports[] = $child;
158 474
                       $n++;
159
                    }
160
                }
161 477
                return $n;
162 519
            }, $exports)) == 0) {
163 519
                break;
164
            }
165
        }
166
167 519
        foreach ($exports as $export) {
168 477
            $ret .= ($methods[$export][0] . " }\n");
169
        }
170
171 519
        return $ret;
172
    }
173
174
    /**
175
     * Export Runtime constants
176
     *
177
     * @param array<string,array|string|integer> $context current compile context
178
     *
179
     * @return string
180
     */
181 652
    public static function constants($context) {
182 652
        if ($context['flags']['standalone'] == 0) {
183 623
            return 'array()';
184
        }
185
186 519
        $class = new \ReflectionClass($context['runtime']);
187 519
        $constants = $class->getConstants();
188 519
        $ret = " array(\n";
189 519
        foreach($constants as $name => $value) {
190 519
            $ret .= "            '$name' => ".  (is_string($value) ? "'$value'" : $value ) . ",\n";
191
        }
192 519
        $ret .= "        )";
193 519
        return $ret;
194
    }
195
196
    /**
197
     * Scan for required standalone functions
198
     *
199
     * @param array<string,array|string|integer> $context current compile context
200
     * @param string $code PHP code string of the method
201
     *
202
     * @return array<string|array> list of converted code and children array
203
     */
204 519
    protected static function scanDependency($context, $code) {
205 519
        $child = array();
206
207 519
        $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) {
208 519
            if (!isset($child[$matches[1]])) {
209 519
                $child[$matches[1]] = 0;
210
            }
211 519
            $child[$matches[1]]++;
212
213 519
            return "{$context['funcprefix']}{$matches[1]}(";
214 519
        }, $code);
215
216
        // replace the constants
217 519
        $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code);
218
219
        // compress space
220 519
        $code = preg_replace('/    /', ' ', $code);
221
222 519
        return array(static::replaceSafeString($context, $code), $child);
223
    }
224
}
225
226