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 ( 8e76f6...eb031e )
by Zordius
02:19
created

Exporter::helpers()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 10
nc 4
nop 2
crap 6
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-2015 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 object $closure Closure object
32
     *
33
     * @return string
34
     *
35
     * @expect 'function($a) {return;}' when input function ($a) {return;}
36
     * @expect 'function($a) {return;}' when input    function ($a) {return;}
37
     */
38 67
    protected static function closure($closure) {
39 67
        if (is_string($closure) && preg_match('/(.+)::(.+)/', $closure, $matched)) {
40 2
            $ref = new \ReflectionMethod($matched[1], $matched[2]);
41
        } else {
42 66
            $ref = new \ReflectionFunction($closure);
43
        }
44 67
        $fname = $ref->getFileName();
45
46 67
        $lines = file_get_contents($fname);
47 67
        $file = new \SplFileObject($fname);
48 67
        $file->seek($ref->getStartLine() - 2);
49 67
        $spos = $file->ftell();
50 67
        $file->seek($ref->getEndLine() - 1);
51 67
        $epos = $file->ftell();
52
53 67
        return preg_replace('/^.*?function(\s+[^\s\\(]+?)?\s*?\\((.+?)\\}[,\\s]*;?$/s', 'function($2}', substr($lines, $spos, $epos - $spos));
54
    }
55
56
    /**
57
     * Export required custom helper functions
58
     *
59
     * @param string $tname   helper table name
60
     * @param array<string,array|string|integer> $context current compile context
61
     *
62
     * @return string
63
     */
64 650
    public static function helpers($context, $tname = 'helpers') {
65 650
        $ret = '';
66 650
        foreach ($context[$tname] as $name => $func) {
67 188
            if (!isset($context['usedCount'][$tname][$name])) {
68 33
                continue;
69
            }
70 174
            if ((is_object($func) && ($func instanceof \Closure)) || ($context['flags']['exhlp'] == 0)) {
71 66
                $ret .= ("            '$name' => " . static::closure($func) . ",\n");
72 66
                continue;
73
            }
74 108
            $ret .= "            '$name' => '$func',\n";
75
        }
76
77 650
        return "array($ret)";
78
    }
79
80
    /**
81
     * Export required standalone Runtime methods
82
     *
83
     * @param array<string,array|string|integer> $context current compile context
84
     *
85
     * @return string
86
     */
87 490
    public static function runtime($context) {
88 490
        if ($context['flags']['standalone'] == 0) {
89
            return '';
90
        }
91
92 490
        $class = new \ReflectionClass($context['runtime']);
93 490
        $fname = $class->getFileName();
94 490
        $lines = file_get_contents($fname);
95 490
        $file = new \SplFileObject($fname);
96 490
        $methods = array();
97 490
        $ret = '';
98
99 490
        foreach ($class->getMethods() as $method) {
100 490
            $name = $method->getName();
101 490
            $file->seek($method->getStartLine() - 2);
102 490
            $spos = $file->ftell();
103 490
            $file->seek($method->getEndLine() - 2);
104 490
            $epos = $file->ftell();
105 490
            $methods[$name] = static::scanDependency($context, preg_replace('/public static function (.+)\\(/', "function {$context['funcprefix']}\$1(", substr($lines, $spos, $epos - $spos)));
106
        }
107 490
        unset($file);
108
109 490
        $exports = array_keys($context['usedCount']['runtime']);
110
111 490
        while (true) {
112
            if (array_sum(array_map(function ($name) use (&$exports, $methods) {
113 453
                $n = 0;
114 453
                foreach ($methods[$name][1] as $child => $count) {
115 453
                    if (!in_array($child, $exports)) {
116 453
                       $exports[] = $child;
117 453
                       $n++;
118
                    }
119
                }
120 453
                return $n;
121 490
            }, $exports)) == 0) {
122 490
                break;
123
            }
124
        }
125
126 490
        foreach ($exports as $export) {
127 453
            $ret .= ($methods[$export][0] . "}\n");
128
        }
129
130 490
        return $ret;
131
    }
132
133
    /**
134
     * Export Runtime constants
135
     *
136
     * @param array<string,array|string|integer> $context current compile context
137
     *
138
     * @return string
139
     */
140 650
    public static function constants($context) {
141 650
        if ($context['flags']['standalone'] == 0) {
142 650
            return 'array()';
143
        }
144
145 490
        $class = new \ReflectionClass($context['runtime']);
146 490
        $constants = $class->getConstants();
147 490
        $ret = " array(\n";
148 490
        foreach($constants as $name => $value) {
149 490
            $ret .= "            '$name' => ".  (is_string($value) ? "'$value'" : $value ) . ",\n";
150
        }
151 490
        $ret .= "        )";
152 490
        return $ret;
153
    }
154
155
    /**
156
     * Scan for required standalone functions
157
     *
158
     * @param array<string,array|string|integer> $context current compile context
159
     * @param string $code PHP code string of the method
160
     *
161
     * @return array<string|array> list of converted code and children array
162
     */
163 490
    protected static function scanDependency($context, $code) {
164 490
        $child = array();
165
166 490
        $code = preg_replace_callback('/static::(\w+?)\s*\(/', function ($matches) use ($context, &$child) {
167 490
            if (!isset($child[$matches[1]])) {
168 490
                $child[$matches[1]] = 0;
169
            }
170 490
            $child[$matches[1]]++;
171
172 490
            return "{$context['funcprefix']}{$matches[1]}(";
173 490
        }, $code);
174
175
        // replace the constants
176 490
        $code = preg_replace('/static::([A-Z0-9_]+)/', "\$cx['constants']['$1']", $code);
177 490
        return array($code, $child);
178
    }
179
}
180
181