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 — integration ( 02db51...9a9ecd )
by Brendan
06:02
created

func.utilities.php ➔ precision_timer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 9.4285
1
<?php
2
3
/**
4
 * @package boot
5
 */
6
7
/**
8
 * Redirects the browser to a specified location. Safer than using a
9
 * direct header() call
10
 *
11
 * @throws SymphonyErrorPage if the headers have already been sent
12
 * @param string $location
13
 * @param string $status (optional)
14
 */
15
function redirect($location, $status = '302 Found')
16
{
17
    // throw exception if headers already sent
18
    if (headers_sent($filename, $line)) {
19
        // throw exception if headers already sent
20
        throw new SymphonyErrorPage(sprintf(
21
            'Cannot redirect to <a href="%s">%s</a>. Output has already started in %s on line %s.',
22
            $location, $location, $filename, $line
23
        ));
24
    }
25
26
    // convert idn back to ascii for redirect
27
    if (function_exists('idn_to_ascii')) {
28
        // convert idn back to ascii for redirect
29
        $host     = parse_url($location, PHP_URL_HOST);
30
        $location = str_replace($host, idn_to_ascii($host), $location);
31
    }
32
33
    header('Status: '   . $status);
34
    header('Location: ' . $location);
35
36
    exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
37
}
38
39
/**
40
 * Returns the current working directory, replacing any \
41
 *  with /. Use for Windows compatibility.
42
 *
43
 *  @return string
44
 */
45
function getcwd_safe()
46
{
47
    return str_replace('\\', '/', getcwd());
48
}
49
50
/**
51
 * Checks that a constant has not been defined before defining
52
 * it. If the constant is already defined, this function will do
53
 * nothing, otherwise, it will set the constant
54
 *
55
 * @param string $name
56
 *  The name of the constant to set
57
 * @param string $value
58
 *  The value of the desired constant
59
 */
60
function define_safe($name, $value)
61
{
62
    if (!defined($name)) {
63
        define($name, $value);
64
    }
65
}
66
67
/**
68
 * Returns the current URL string from within the Administration
69
 * context. It omits the Symphony directory from the current URL.
70
 *
71
 *  @return string
72
 */
73
function getCurrentPage()
74
{
75
    if (!isset($_GET['symphony-page']) || !is_string($_GET['symphony-page'])) {
76
        return null;
77
    }
78
79
    return '/' . filter_var(trim($_GET['symphony-page'], '/'), FILTER_SANITIZE_STRING) . '/';
80
}
81
82
/**
83
 * Used as a basic stopwatch for profiling. The default `$action`
84
 * starts the timer. Setting `$action` to 'stop' and passing the
85
 * start time returns the difference between now and that time.
86
 *
87
 *  @param string $action (optional)
88
 *  @param integer $start_time (optional)
89
 *  @return integer
90
 */
91
function precision_timer($action = 'start', $start_time = null)
92
{
93
    $currtime = microtime(true);
94
95
    if ($action === 'stop') {
96
        return $currtime - $start_time;
97
    }
98
99
    return $currtime;
100
}
101
102
/**
103
 * Convert php.ini size format to bytes
104
 *
105
 *  @param string $val (optional)
106
 *  @return integer
107
 */
108
function ini_size_to_bytes($val)
109
{
110
    $val = trim($val);
111
    $last = strtolower($val[strlen($val)-1]);
112
113 View Code Duplication
    switch ($last) {
114
        case 'g':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
115
            $val *= 1024;
116
        case 'm':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
117
            $val *= 1024;
118
        case 'k':
119
            $val *= 1024;
120
    }
121
122
    return $val;
123
}
124
125
/**
126
 * Cleans up Session Cookies. When there is no data in the session the cookie will be unset.
127
 * If there is data, the cookie will be renewed, expiring it in two weeks from now.
128
 * This will improve the interoperability with caches like Varnish and Squid.
129
 *
130
 * @since 2.3.3
131
 * @author creativedutchmen (Huib Keemink)
132
 * @param string $mode
133
 */
134
function cleanup_session_cookies($mode)
135
{
136
    if (strtolower($mode) !== 'administration') {
137
        if (null === Symphony::$Cookies) {
138
            return;
139
        }
140
141
        if (is_session_empty() && Symphony::Cookies()->has(session_name())) {
142
            Symphony::Cookies()->remove(session_name());
143
        }
144
    }
145
}
146
147
/**
148
 * Function will loop over the $_SESSION and find out if it is empty or not
149
 *
150
 * @since Symphony 2.4
151
 * @return boolean
152
 */
153
function is_session_empty()
0 ignored issues
show
Coding Style introduced by
is_session_empty uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
154
{
155
    $session_is_empty = true;
156
    if (isset($_SESSION) && is_array($_SESSION)) {
157
        foreach ($_SESSION as $contents) {
158
            if (!empty($contents)) {
159
                $session_is_empty = false;
160
            }
161
        }
162
    }
163
    return $session_is_empty;
164
}
165
166
/**
167
 * Responsible for picking the launcher function and starting it.
168
 *
169
 *  @param string $mode (optional)
170
 */
171
function symphony($mode)
172
{
173
    $launcher = SYMPHONY_LAUNCHER;
174
    $launcher($mode);
175
}
176
177
/**
178
 * Responsible for launching a standard symphony instance and
179
 * sending output to the browser.
180
 *
181
 *  @param string $mode (optional)
182
 *  @return integer
183
 */
184
function symphony_launcher($mode)
0 ignored issues
show
Coding Style introduced by
symphony_launcher uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
185
{
186
    if (is_string($mode) && strtolower($mode) === 'administration') {
187
        $renderer = Administration::instance();
188
    } else {
189
        $renderer = Frontend::instance();
190
    }
191
192
    $output = $renderer->display(getCurrentPage());
193
194
    // #1808
195
    if (isset($_SERVER['HTTP_MOD_REWRITE'])) {
196
        $output = file_get_contents(GenericExceptionHandler::getTemplate('fatalerror.rewrite'));
197
        $output = str_replace('{ASSETS_URL}', ASSETS_URL, $output);
198
        $output = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $output);
199
        $output = str_replace('{URL}', URL, $output);
200
        echo $output;
201
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function symphony_launcher() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
202
    }
203
204
    cleanup_session_cookies($mode);
205
206
    Symphony::Cookies()->save();
207
208
    echo $output;
209
210
    return $renderer;
211
}
212
213
214
/**
215
 * The translation function accepts an English string and returns its translation
216
 * to the active system language. If the given string is not available in the
217
 * current dictionary the original English string will be returned. Given an optional
218
 * `$inserts` array, the function will replace translation placeholders using `vsprintf()`.
219
 * Since Symphony 2.3, it is also possible to have multiple translation of the same string
220
 * according to the page namespace (i.e. the value returned by Symphony's `getPageNamespace()`
221
 * method). In your lang file, use the `$dictionary` key as namespace and its value as an array
222
 * of context-aware translations, as shown below:
223
 *
224
 * $dictionary = array(
225
 *        [...]
226
 *
227
 *        'Create new' => 'Translation for Create New',
228
 *
229
 *        '/blueprints/datasources' => array(
230
 *            'Create new' =>
231
 *            'If we are inside a /blueprints/datasources/* page, this translation will be returned for the string'
232
 *        ),
233
 *
234
 *        [...]
235
 *  );
236
 *
237
 * @see core.Symphony#getPageNamespace()
238
 * @param string $string
239
 *  The string that should be translated
240
 * @param array $inserts (optional)
241
 *  Optional array used to replace translation placeholders, defaults to NULL
242
 * @return string
243
 *  Returns the translated string
244
 */
245
function __($string, $inserts = null)
246
{
247
    return Lang::translate($string, $inserts);
248
}
249