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 — v0.89-develop ( 178f3d...f4df6d )
by Zordius
02:49
created

Partial::compileDynamic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6667
cc 2
eloc 6
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-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 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 643
    public static function handleDynamicPartial(&$context) {
38 643
        if ($context['usedFeature']['dynpartial'] == 0) {
39 639
            return;
40
        }
41
42 4
        foreach ($context['partials'] as $name => $code) {
43 4
            static::readPartial($name, $context);
44
        }
45 4
    }
46
47
    /**
48
     * Read partial file content as string and store in context
49
     *
50
     * @param string $name partial name
51
     * @param array<string,array|string|integer> $context Current context of compiler progress.
52
     */
53 84
    public static function readPartial($name, &$context) {
54 84
        $context['usedFeature']['partial']++;
55
56 84
        if (isset($context['usedPartial'][$name])) {
57 23
            return;
58
        }
59
60 78
        if (isset($context['inlines'][$name])) {
61
            return;
62
        }
63
64 78
        $cnt = static::resolvePartial($name, $context);
65
66 78
        if ($cnt !== null) {
67 72
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
68 72
            return static::compileDynamic($name, $context);
69
        }
70
71 6
        if (!$context['flags']['skippartial']) {
72 5
            $context['error'][] = "Can not find partial file for '$name', you should set correct basedir and fileext in options";
73
        }
74 6
    }
75
76
    /**
77
     * preprocess partial template before it be stored into context
78
     *
79
     * @param string $tmpl partial template
80
     * @param string $name partial name
81
     * @param array<string,array|string|integer> $context Current context of compiler progress.
82
     *
83
     * @return string|null $content processed partial template
84
     *
85
     * @expect 'hey' when input 'hey', 'haha', Array('prepartial' => false)
86
     * @expect 'haha-hoho' when input 'hoho', 'haha', Array('prepartial' => function ($tmpl, $name) {return "$name-$tmpl";})
87
     */
88 73
    protected static function prePartial($tmpl, &$name, &$context) {
89 73
        return $context['prepartial'] ? $context['prepartial']($tmpl, $name, $context) : $tmpl;
90
    }
91
92
    /**
93
     * locate partial file, return the file name
94
     *
95
     * @param string $name partial name
96
     * @param array<string,array|string|integer> $context Current context of compiler progress.
97
     *
98
     * @return string|null $content partial content
99
     */
100 82
    public static function resolvePartial(&$name, &$context) {
101 82
        if (isset($context['partials'][$name])) {
102 65
            return static::prePartial($context['partials'][$name], $name, $context);
103
        }
104
105 17
        foreach ($context['basedir'] as $dir) {
1 ignored issue
show
Bug introduced by
The expression $context['basedir'] 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...
106 15
            foreach ($context['fileext'] as $ext) {
1 ignored issue
show
Bug introduced by
The expression $context['fileext'] 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...
107 15
                $fn = "$dir/$name$ext";
108 15
                if (file_exists($fn)) {
109 15
                    return static::prePartial(file_get_contents($fn), $name, $context);
110
                }
111
            }
112
        }
113 10
        return null;
114
    }
115
116
    /**
117
     * compile a partial to static embed PHP code
118
     *
119
     * @param array<string,array|string|integer> $context Current context of compiler progress.
120
     * @param string $name partial name
121
     *
122
     * @return string|null $code PHP code string
123
     */
124 7
    public static function compileStatic(&$context, $name) {
125
        // Check for recursive partial
126 7
        if (!$context['flags']['runpart']) {
127 7
            $context['partialStack'][] = $name;
128 7
            $diff = count($context['partialStack']) - count(array_unique($context['partialStack']));
129 7
            if ($diff) {
130 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.';
131
            }
132
        }
133
134 7
        $code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name]));
135
136 7
        if (!$context['flags']['runpart']) {
137 7
            array_pop($context['partialStack']);
138
        }
139
140 7
        return $code;
141
    }
142
143
    /**
144
     * compile partial file, stored in context
145
     *
146
     * @param string $name partial name
147
     * @param array<string,array|string|integer> $context Current context of compiler progress.
148
     *
149
     * @return string $code compiled PHP code
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
150
     */
151 82
    public static function compileDynamic($name, &$context) {
152 82
        if (!$context['flags']['runpart']) {
153 8
            return;
154
        }
155
156 74
        $func = static::compileLocal($context, $context['usedPartial'][$name]);
157 74
        $context['partialCode'] .= "'$name' => $func,";
158 74
        return $func;
159
    }
160
161
    /**
162
     * compile a template into a closure function
163
     *
164
     * @param array<string,array|string|integer> $context Current context of compiler progress.
165
     * @param string $template template string
166
     *
167
     * @return string $code compiled PHP code
168
     */
169 74
    public static function compileLocal(&$context, $template) {
170 74
        $tmpContext = $context;
171 74
        $tmpContext['inlinepartial'] = array();
172 74
        $tmpContext['partialblock'] = array();
173 74
        $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
174 74
        Context::merge($context, $tmpContext);
175 74
        if (!$context['flags']['noind']) {
176 69
            $sp = ', $sp';
177 69
            $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
178
            // callbacks inside partial should be aware of $sp
179 69
            $code = preg_replace('/\bfunction\s*\((.*?)\)\s*{/', 'function(\\1)use($sp){', $code);
180
        } else {
181 5
            $sp = '';
182
        }
183 74
        $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
184 74
        return "function (\$cx, \$in{$sp}) {{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
185
    }
186
}
187
188