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 ( 876585...046de7 )
by Zordius
02:33
created

src/Partial.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 688
    public static function handleDynamic(&$context) {
38 688
        if ($context['usedFeature']['dynpartial'] == 0) {
39 683
            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 99
    public static function read(&$context, $name) {
56 99
        $isPB = ($name === '@partial-block');
57 99
        $context['usedFeature']['partial']++;
58
59 99
        if (isset($context['usedPartial'][$name])) {
60 25
            return;
61
        }
62
63 93
        $cnt = static::resolve($context, $name);
64
65 93
        if ($cnt !== null) {
66 87
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
67 87
            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 88
    protected static function prePartial(&$context, $tmpl, &$name) {
88 88
        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 97
    public static function resolve(&$context, &$name) {
100 97
        if ($name === '@partial-block') {
101 12
            $name = "@partial-block{$context['usedFeature']['pblock']}";
102
        }
103 97
        if (isset($context['partials'][$name])) {
104 86
            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 99
    public static function compileDynamic(&$context, $name) {
161 99
        if (!$context['flags']['runpart']) {
162 11
            return;
163
        }
164
165 88
        $func = static::compile($context, $context['usedPartial'][$name], $name);
166
167 88
        if (!isset($context['partialCode'][$name]) && $func) {
168 88
            $context['partialCode'][$name] = "'$name' => $func";
169
        }
170
171 88
        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
Consider making the type for parameter $name a bit more specific; maybe use integer.
Loading history...
180
     *
181
     * @return string $code compiled PHP code
182
     */
183 88
    public static function compile(&$context, $template, $name = 0) {
184 88
        if ((end($context['partialStack']) === $name) && (substr($name, 0, 14) === '@partial-block')) {
185 1
            return;
186
        }
187
188 88
        $tmpContext = $context;
189 88
        $tmpContext['inlinepartial'] = array();
190 88
        $tmpContext['partialblock'] = array();
191
192 88
        if ($name !== 0) {
193 88
            $tmpContext['partialStack'][] = $name;
194
        }
195
196 88
        $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
197 88
        Context::merge($context, $tmpContext);
198 88
        if (!$context['flags']['noind']) {
199 83
            $sp = ', $sp';
200 83
            $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
201
            // callbacks inside partial should be aware of $sp
202 83
            $code = preg_replace('/\bfunction\s*\(([^\(]*?)\)\s*{/', 'function(\\1)use($sp){', $code);
203 83
            $code = preg_replace('/function\(\$cx, \$in, \$sp\)use\(\$sp\){/', 'function($cx, $in)use($sp){', $code);
204
        } else {
205 5
            $sp = '';
206
        }
207 88
        $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
208 88
        return "function (\$cx, \$in{$sp}) {{$context['ops']['array_check']}{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
209
    }
210
}
211
212