Completed
Push — 0713 ( c637fa...b1d0cb )
by Mikael
02:55
created

functions.php ➔ getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 72.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * General functions to use in img.php.
4
 */
5
6
7
8
/**
9
 * Trace and log execution to logfile, useful for debugging and development.
10
 *
11
 * @param string $msg message to log to file.
12
 *
13
 * @return void
14
 */
15
function trace($msg)
0 ignored issues
show
Coding Style introduced by
trace 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...
16
{
17
    $file = "/tmp/cimage";
18
    if (!is_writable($file)) {
19
        die("Using trace without a writable logfile. Create the file '$file' and make it writable for the web server.");
0 ignored issues
show
Coding Style Compatibility introduced by
The function trace() 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...
20
    }
21
22
    $msg .= ":" . count(get_included_files());
23
    $msg .= ":" . round(memory_get_peak_usage()/1024/1024, 3) . "MB";
24
    $msg .= ":" . (string) round((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]), 6) . "ms";
25
    file_put_contents($file, "$msg\n", FILE_APPEND);
26
}
27
28
29
30
/**
31
 * Display error message.
32
 *
33
 * @param string $msg to display.
34
 * @param int $type of HTTP error to display.
35
 *
36
 * @return void
37
 */
38
function errorPage($msg, $type = 500)
39
{
40
    global $mode;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
41
42
    switch ($type) {
43
        case 403:
44
            $header = "403 Forbidden";
45
            break;
46
        case 404:
47
            $header = "404 Not Found";
48
            break;
49
        default:
50
            $header = "500 Internal Server Error";
51
    }
52
53
    if ($mode == "strict") {
54
        $header = "404 Not Found";
55
    }
56
57
    header("HTTP/1.0 $header");
58
59
    if ($mode == "development") {
60
        die("[img.php] $msg");
0 ignored issues
show
Coding Style Compatibility introduced by
The function errorPage() 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...
61
    }
62
63
    error_log("[img.php] $msg");
64
    die("HTTP/1.0 $header");
0 ignored issues
show
Coding Style Compatibility introduced by
The function errorPage() 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...
65
}
66
67
68
69
/**
70
 * Custom exception handler.
71
 */
72
set_exception_handler(function ($exception) {
73
    errorPage(
74
        "<p><b>img.php: Uncaught exception:</b> <p>"
75
        . $exception->getMessage()
76
        . "</p><pre>"
77
        . $exception->getTraceAsString()
78
        . "</pre>",
79
        500
80
    );
81
});
82
83
84
85
/**
86
 * Get input from query string or return default value if not set.
87
 *
88
 * @param mixed $key     as string or array of string values to look for in $_GET.
89
 * @param mixed $default value to return when $key is not set in $_GET.
90
 *
91
 * @return mixed value from $_GET or default value.
92
 */
93
function get($key, $default = null)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_GET 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...
94
{
95
    if (is_array($key)) {
96
        foreach ($key as $val) {
97
            if (isset($_GET[$val])) {
98
                return $_GET[$val];
99
            }
100
        }
101
    } elseif (isset($_GET[$key])) {
102
        return $_GET[$key];
103
    }
104
    return $default;
105
}
106
107
108
109
/**
110
 * Get input from query string and set to $defined if defined or else $undefined.
111
 *
112
 * @param mixed $key       as string or array of string values to look for in $_GET.
113
 * @param mixed $defined   value to return when $key is set in $_GET.
114
 * @param mixed $undefined value to return when $key is not set in $_GET.
115
 *
116
 * @return mixed value as $defined or $undefined.
117
 */
118
function getDefined($key, $defined, $undefined)
119
{
120
    return get($key) === null ? $undefined : $defined;
121
}
122
123
124
125
/**
126
 * Get value from config array or default if key is not set in config array.
127
 *
128
 * @param string $key    the key in the config array.
129
 * @param mixed $default value to be default if $key is not set in config.
130
 *
131
 * @return mixed value as $config[$key] or $default.
132
 */
133
function getConfig($key, $default)
134
{
135
    global $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
136
    return isset($config[$key])
137
        ? $config[$key]
138
        : $default;
139
}
140
141
142
143
/**
144
 * Log when verbose mode, when used without argument it returns the result.
145
 *
146
 * @param string $msg to log.
147
 *
148
 * @return void or array.
149
 */
150
function verbose($msg = null)
151
{
152
    global $verbose, $verboseFile;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
153
    static $log = array();
154
155
    if (!($verbose || $verboseFile)) {
156
        return;
157
    }
158
159
    if (is_null($msg)) {
160
        return $log;
161
    }
162
163
    $log[] = $msg;
164
}
165