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 ( 096e96...a1f699 )
by Zordius
19:00
created

LightnCandy   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.84%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 20
c 3
b 0
f 1
lcom 1
cbo 5
dl 0
loc 136
ccs 33
cts 38
cp 0.8684
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 18 3
A compilePartial() 0 17 3
A handleError() 0 14 4
A getContext() 0 3 1
D prepare() 0 31 9
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
 * the major file of LightnCandy compiler
16
 *
17
 * @package    LightnCandy
18
 * @author     Zordius <[email protected]>
19
 */
20
21
namespace LightnCandy;
22
23
use \LightnCandy\Context;
24
use \LightnCandy\Compiler;
25
use \LightnCandy\Partial;
26
27
/**
28
 * LightnCandy major static class
29
 */
30
class LightnCandy extends Flags
31
{
32
    protected static $lastContext;
33
    public static $lastParsed;
34
35
    /**
36
     * Compile handlebars template into PHP code.
37
     *
38
     * @param string $template handlebars template string
39
     * @param array<string,array|string|integer> $options LightnCandy compile time and run time options, default is array('flags' => LightnCandy::FLAG_BESTPERFORMANCE)
40
     *
41
     * @return string|false Compiled PHP code when successed. If error happened and compile failed, return false.
42 719
     */
43 719
    public static function compile($template, $options = array('flags' => self::FLAG_BESTPERFORMANCE)) {
44
        $context = Context::create($options);
45 719
46 2
        if (static::handleError($context)) {
47
            return false;
48
        }
49 717
50 717
        $code = Compiler::compileTemplate($context, SafeString::escapeTemplate($template));
51
        static::$lastParsed = Compiler::$lastParsed;
52
53 717
        // return false when fatal error
54 60
        if (static::handleError($context)) {
55
            return false;
56
        }
57
58 647
        // Or, return full PHP render codes as string
59
        return Compiler::composePHPRender($context, $code);
60
    }
61
62
    /**
63
     * Compile handlebars partial into PHP function code.
64
     *
65
     * @param string $template handlebars template string
66
     * @param array<string,array|string|integer> $options LightnCandy compile time and run time options, default is array('flags' => LightnCandy::FLAG_BESTPERFORMANCE)
67
     *
68
     * @return string|false Compiled PHP code when successed. If error happened and compile failed, return false.
69
     */
70
    public static function compilePartial($template, $options = array('flags' => self::FLAG_BESTPERFORMANCE)) {
71
        $context = Context::create($options);
72 720
73 720
        if (static::handleError($context)) {
74
            return false;
75 720
        }
76 73
77 1
        $code = Partial::compile($context, $template);
78
        static::$lastParsed = Compiler::$lastParsed;
79 73
80 10
        // return false when fatal error
81
        if (static::handleError($context)) {
82 63
            return false;
83
        }
84 718
85
        return $code;
86
    }
87
88
    /**
89
     * Handle exists error and return error status.
90
     *
91
     * @param array<string,array|string|integer> $context Current context of compiler progress.
92 214
     *
93 214
     * @throws \Exception
94
     * @return boolean True when error detected
95
     *
96
     * @expect false when input array('error' => array())
97
     * @expect true when input array('error' => array('some error'), 'flags' => array('errorlog' => 0, 'exception' => 0))
98
     */
99
    protected static function handleError(&$context) {
100
        static::$lastContext = $context;
101
102
        if (count($context['error'])) {
103
            if ($context['flags']['errorlog']) {
104
                error_log(implode("\n", $context['error']));
105
            }
106
            if ($context['flags']['exception']) {
107 616
                throw new \Exception(implode("\n", $context['error']));
108 616
            }
109
            return true;
110 616
        }
111 616
        return false;
112 616
    }
113
114
    /**
115
     * Get last compiler context.
116 616
     *
117 616
     * @return array<string,array|string|integer> Context data
118 616
     */
119
    public static function getContext() {
120
        return static::$lastContext;
121
    }
122 616
123
    /**
124
     * Get a working render function by a string of PHP code. This method may requires php setting allow_url_include=1 and allow_url_fopen=1 , or access right to tmp file system.
125
     *
126
     * @param string      $php PHP code
127 616
     * @param string|null $tmpDir Optional, change temp directory for php include file saved by prepare() when cannot include PHP code with data:// format.
128
     * @param boolean     $delete Optional, delete temp php file when set to tru. Default is true, set it to false for debug propose
129 616
     *
130 242
     * @return Closure|false result of include()
131
     *
132
     * @deprecated
133 616
     */
134
    public static function prepare($php, $tmpDir = null, $delete = true) {
135
        $php = "<?php $php ?>";
136
137
        if (!ini_get('allow_url_include') || !ini_get('allow_url_fopen')) {
138
            if (!is_string($tmpDir) || !is_dir($tmpDir)) {
139
                $tmpDir = sys_get_temp_dir();
140
            }
141
        }
142
143
        if (is_dir($tmpDir)) {
144
            $fn = tempnam($tmpDir, 'lci_');
145
            if (!$fn) {
146
                error_log("Can not generate tmp file under $tmpDir!!\n");
147
                return false;
148
            }
149
            if (!file_put_contents($fn, $php)) {
150
                error_log("Can not include saved temp php code from $fn, you should add $tmpDir into open_basedir!!\n");
151
                return false;
152
            }
153
154
            $phpfunc = include($fn);
155
156
            if ($delete) {
157
                unlink($fn);
158
            }
159
160
            return $phpfunc;
161
        }
162
163
        return include('data://text/plain,' . urlencode($php));
164
    }
165
}
166
167