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 ( 1f1050...426291 )
by Zordius
02:07
created

Partial::read()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
crap 5
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 partial methods
16
 *
17
 * @package    LightnCandy
18
 * @author     Zordius <[email protected]>
19
 */
20
21
namespace LightnCandy;
22
23
use \LightnCandy\Compiler;
24
use \LightnCandy\SafeString;
25
use \LightnCandy\Context;
26
27
/**
28
 * LightnCandy Partial handler
29
 */
30
class Partial
31
{
32
    public static $TMP_JS_FUNCTION_STR = "!!\aFuNcTiOn\a!!";
33
34
    /**
35
     * Include all partials when using dynamic partials
36
     */
37 678
    public static function handleDynamic(&$context) {
38 678
        if ($context['usedFeature']['dynpartial'] == 0) {
39 673
            return;
40
        }
41
42 5
        foreach ($context['partials'] as $name => $code) {
43 5
            static::read($context, $name);
44
        }
45 5
    }
46
47
    /**
48
     * Read partial file content as string and store in context
49
     *
50
     * @param array<string,array|string|integer> $context Current context of compiler progress.
51
     * @param string $name partial name
52
     *
53
     * @return string|null $code compiled PHP code when success
54
     */
55 97
    public static function read(&$context, $name) {
56 97
        $isPB = ($name === '@partial-block');
57 97
        $context['usedFeature']['partial']++;
58
59 97
        if (isset($context['usedPartial'][$name])) {
60 24
            return;
61
        }
62
63 91
        $cnt = static::resolve($context, $name);
64
65 91
        if ($cnt !== null) {
66 85
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
67 85
            return static::compileDynamic($context, $name);
68
        }
69
70 7
        if (!$context['flags']['skippartial'] && !$isPB) {
71 5
            $context['error'][] = "Can not find partial for '$name', you should provide partials or partialresolver in options";
72
        }
73 7
    }
74
75
    /**
76
     * preprocess partial template before it be stored into context
77
     *
78
     * @param array<string,array|string|integer> $context Current context of compiler progress.
79
     * @param string $tmpl partial template
80
     * @param string $name partial name
81
     *
82
     * @return string|null $content processed partial template
83
     *
84
     * @expect 'hey' when input Array('prepartial' => false), 'hey', 'haha'
85
     * @expect 'haha-hoho' when input Array('prepartial' => function ($cx, $tmpl, $name) {return "$name-$tmpl";}), 'hoho', 'haha'
86
     */
87 86
    protected static function prePartial(&$context, $tmpl, &$name) {
88 86
        return $context['prepartial'] ? $context['prepartial']($context, $tmpl, $name) : $tmpl;
89
    }
90
91
    /**
92
     * resolve partial, return the partial content
93
     *
94
     * @param array<string,array|string|integer> $context Current context of compiler progress.
95
     * @param string $name partial name
96
     *
97
     * @return string|null $content partial content
98
     */
99 95
    public static function resolve(&$context, &$name) {
100 95
        if ($name === '@partial-block') {
101 11
            $name = "@partial-block{$context['usedFeature']['pblock']}";
102
        }
103 95
        if (isset($context['partials'][$name])) {
104 84
            return static::prePartial($context, $context['partials'][$name], $name);
105
        }
106
107 14
        return static::resolver($context, $name);
108
    }
109
110
    /**
111
     * use partialresolver to resolve partial, return the partial content
112
     *
113
     * @param array<string,array|string|integer> $context Current context of compiler progress.
114
     * @param string $name partial name
115
     *
116
     * @return string|null $content partial content
117
     */
118 14
    public static function resolver(&$context, &$name) {
119 14
        if ($context['partialresolver']) {
120 1
            $cnt = $context['partialresolver']($context, $name);
121 1
            return static::prePartial($context, $cnt, $name);
122
        }
123 13
    }
124
125
    /**
126
     * compile a partial to static embed PHP code
127
     *
128
     * @param array<string,array|string|integer> $context Current context of compiler progress.
129
     * @param string $name partial name
130
     *
131
     * @return string|null $code PHP code string
132
     */
133 9
    public static function compileStatic(&$context, $name) {
134
        // Check for recursive partial
135 9
        if (!$context['flags']['runpart']) {
136 9
            $context['partialStack'][] = $name;
137 9
            $diff = count($context['partialStack']) - count(array_unique($context['partialStack']));
138 9
            if ($diff) {
139 1
                $context['error'][] = 'I found recursive partial includes as the path: ' . implode(' -> ', $context['partialStack']) . '! You should fix your template or compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag.';
140
            }
141
        }
142
143 9
        $code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name]));
144
145 9
        if (!$context['flags']['runpart']) {
146 9
            array_pop($context['partialStack']);
147
        }
148
149 9
        return $code;
150
    }
151
152
    /**
153
     * compile partial as closure, stored in context
154
     *
155
     * @param array<string,array|string|integer> $context Current context of compiler progress.
156
     * @param string $name partial name
157
     *
158
     * @return string|null $code compiled PHP code when success
159
     */
160 97
    public static function compileDynamic(&$context, $name) {
161 97
        if (!$context['flags']['runpart']) {
162 11
            return;
163
        }
164
165 86
        $func = static::compile($context, $context['usedPartial'][$name], $name);
166
167 86
        if (!isset($context['partialCode'][$name]) && $func) {
168 86
            $context['partialCode'][$name] = "'$name' => $func";
169
        }
170
171 86
        return $func;
172
    }
173
174
    /**
175
     * compile a template into a closure function
176
     *
177
     * @param array<string,array|string|integer> $context Current context of compiler progress.
178
     * @param string $template template string
179
     * @param string,integer $name partial name or 0
0 ignored issues
show
Documentation introduced by
The doc-type string,integer could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
180
     *
181
     * @return string $code compiled PHP code
182
     */
183 86
    public static function compile(&$context, $template, $name = 0) {
184 86
        if ((end($context['partialStack']) === $name) && (substr($name, 0, 14) === '@partial-block')) {
185 1
            return;
186
        }
187
188 86
        $tmpContext = $context;
189 86
        $tmpContext['inlinepartial'] = array();
190 86
        $tmpContext['partialblock'] = array();
191
192 86
        if ($name !== 0) {
193 86
            $tmpContext['partialStack'][] = $name;
194
        }
195
196 86
        $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
197 86
        Context::merge($context, $tmpContext);
198 86
        if (!$context['flags']['noind']) {
199 81
            $sp = ', $sp';
200 81
            $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
201
            // callbacks inside partial should be aware of $sp
202 81
            $code = preg_replace('/\bfunction\s*\(([^\(]*?)\)\s*{/', 'function(\\1)use($sp){', $code);
203 81
            $code = preg_replace('/function\(\$cx, \$in, \$sp\)use\(\$sp\){/', 'function($cx, $in)use($sp){', $code);
204
        } else {
205 5
            $sp = '';
206
        }
207 86
        $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
208 86
        return "function (\$cx, \$in{$sp}) {{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
209
    }
210
}
211
212