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 ( 4347a7...178f3d )
by Zordius
02:13
created

Partial   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 23
c 7
b 1
f 0
lcom 1
cbo 3
dl 0
loc 154
ccs 52
cts 52
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handleDynamicPartial() 0 9 3
B readPartial() 0 22 5
A prePartial() 0 3 2
B resolvePartial() 0 15 5
A compileStatic() 0 18 4
A compileDynamic() 0 8 2
A compileLocal() 0 17 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 634
    public static function handleDynamicPartial(&$context) {
38 634
        if ($context['usedFeature']['dynpartial'] == 0) {
39 630
            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 75
    public static function readPartial($name, &$context) {
54 75
        $context['usedFeature']['partial']++;
55
56 75
        if (isset($context['usedPartial'][$name])) {
57 15
            return;
58
        }
59
60 75
        if (isset($context['inlines'][$name])) {
61
            return;
62 75
        }
63 70
64 70
        $cnt = static::resolvePartial($name, $context);
65
66
        if ($cnt !== null) {
67 5
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
68 4
            return static::compileDynamic($name, $context);
69
        }
70 5
71
        if (!$context['flags']['skippartial']) {
72
            $context['error'][] = "Can not find partial file for '$name', you should set correct basedir and fileext in options";
73
        }
74
    }
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 71
     *
85 71
     * @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
    protected static function prePartial($tmpl, &$name, &$context) {
89
        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 78
     * @param array<string,array|string|integer> $context Current context of compiler progress.
97 78
     *
98 63
     * @return string|null $content partial content
99
     */
100
    public static function resolvePartial(&$name, &$context) {
101 15
        if (isset($context['partials'][$name])) {
102 14
            return static::prePartial($context['partials'][$name], $name, $context);
103 14
        }
104 14
105 14
        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
            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
                $fn = "$dir/$name$ext";
108
                if (file_exists($fn)) {
109 8
                    return static::prePartial(file_get_contents($fn), $name, $context);
110
                }
111
            }
112
        }
113
        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 7
     * @param string $name partial name
121
     *
122 7
     * @return string|null $code PHP code string
123 7
     */
124 7
    public static function compileStatic(&$context, $name) {
125 7
        // Check for recursive partial
126 1
        if (!$context['flags']['runpart']) {
127
            $context['partialStack'][] = $name;
128
            $diff = count($context['partialStack']) - count(array_unique($context['partialStack']));
129
            if ($diff) {
130 7
                $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 7
        }
133 7
134
        $code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name]));
135
136 7
        if (!$context['flags']['runpart']) {
137
            array_pop($context['partialStack']);
138
        }
139
140
        return $code;
141
    }
142
143
    /**
144
     * compile partial file, stored in context
145 73
     *
146 73
     * @param string $name partial name
147 8
     * @param array<string,array|string|integer> $context Current context of compiler progress.
148
     */
149
    public static function compileDynamic($name, &$context) {
150 65
        if (!$context['flags']['runpart']) {
151 65
            return;
152 65
        }
153 65
154 60
        $func = static::compileLocal($context, $context['usedPartial'][$name]);
155 60
        $context['partialCode'] .= "'$name' => $func,";
156
    }
157 60
158
    /**
159 5
     * compile a template into a closure function
160
     *
161 65
     * @param array<string,array|string|integer> $context Current context of compiler progress.
162 65
     * @param string $template template string
163 65
     *
164
     * @return array<string> $content array of code and space param string
165
     */
166
    public static function compileLocal(&$context, $template) {
167
        $tmpContext = $context;
168
        $tmpContext['inlinepartial'] = array();
169
        $tmpContext['partialblock'] = array();
170
        $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
171
        Context::merge($context, $tmpContext);
172
        if (!$context['flags']['noind']) {
173
            $sp = ', $sp';
174
            $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
175
            // callbacks inside partial should be aware of $sp
176
            $code = preg_replace('/\bfunction\s*\((.*?)\)\s*{/', 'function(\\1)use($sp){', $code);
177
        } else {
178
            $sp = '';
179
        }
180
        $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
181
        return "function (\$cx, \$in{$sp}) {{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
182
    }
183
}
184
185