Passed
Push — master ( dd8878...4211d7 )
by Mikael
02:04
created

functions.php ➔ getDefined()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
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 = CIMAGE_DEBUG_FILE;
18
    if (!is_writable($file)) {
19
        return;
20
    }
21
22
    $timer = number_format((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]), 6);
23
    $details  = "{$timer}ms";
24
    $details .= ":" . round(memory_get_peak_usage()/1024/1024, 3) . "MB";
25
    $details .= ":" . count(get_included_files());
26
    file_put_contents($file, "$details:$msg\n", FILE_APPEND);
27
}
28
29
30
31
/**
32
 * Display error message.
33
 *
34
 * @param string $msg to display.
35
 * @param int $type of HTTP error to display.
36
 *
37
 * @return void
38
 */
39
function errorPage($msg, $type = 500)
40
{
41
    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...
42
43
    switch ($type) {
44
        case 403:
45
            $header = "403 Forbidden";
46
            break;
47
        case 404:
48
            $header = "404 Not Found";
49
            break;
50
        default:
51
            $header = "500 Internal Server Error";
52
    }
53
54
    if ($mode == "strict") {
55
        $header = "404 Not Found";
56
    }
57
58
    header("HTTP/1.0 $header");
59
60
    if ($mode == "development") {
61
        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...
62
    }
63
64
    error_log("[img.php] $msg");
65
    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...
66
}
67
68
69
70
/**
71
 * Get input from query string or return default value if not set.
72
 *
73
 * @param mixed $key     as string or array of string values to look for in $_GET.
74
 * @param mixed $default value to return when $key is not set in $_GET.
75
 *
76
 * @return mixed value from $_GET or default value.
77
 */
78
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...
79
{
80
    if (is_array($key)) {
81
        foreach ($key as $val) {
82
            if (isset($_GET[$val])) {
83
                return $_GET[$val];
84
            }
85
        }
86
    } elseif (isset($_GET[$key])) {
87
        return $_GET[$key];
88
    }
89
    return $default;
90
}
91
92
93
94
/**
95
 * Get input from query string and set to $defined if defined or else $undefined.
96
 *
97
 * @param mixed $key       as string or array of string values to look for in $_GET.
98
 * @param mixed $defined   value to return when $key is set in $_GET.
99
 * @param mixed $undefined value to return when $key is not set in $_GET.
100
 *
101
 * @return mixed value as $defined or $undefined.
102
 */
103
function getDefined($key, $defined, $undefined)
104
{
105
    return get($key) === null ? $undefined : $defined;
106
}
107
108
109
110
/**
111
 * Get value from config array or default if key is not set in config array.
112
 *
113
 * @param string $key    the key in the config array.
114
 * @param mixed $default value to be default if $key is not set in config.
115
 *
116
 * @return mixed value as $config[$key] or $default.
117
 */
118
function getConfig($key, $default)
119
{
120
    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...
121
    return isset($config[$key])
122
        ? $config[$key]
123
        : $default;
124
}
125
126
127
128
/**
129
 * Log when verbose mode, when used without argument it returns the result.
130
 *
131
 * @param string $msg to log.
132
 *
133
 * @return void or array.
134
 */
135
function verbose($msg = null)
136
{
137
    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...
138
    static $log = array();
139
140
    if (!($verbose || $verboseFile)) {
141
        return;
142
    }
143
144
    if (is_null($msg)) {
145
        return $log;
146
    }
147
148
    $log[] = $msg;
149
}
150
151
152
153
/**
154
 * Log when verbose mode, when used without argument it returns the result.
155
 *
156
 * @param string $msg to log.
0 ignored issues
show
Bug introduced by
There is no parameter named $msg. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
157
 *
158
 * @return void or array.
159
 */
160
function checkExternalCommand($what, $enabled, $commandString)
161
{
162
    $no = $enabled ? null : 'NOT';
163
    $text = "Post processing $what is $no enabled.<br>";
164
165
    list($command) = explode(" ", $commandString);
166
    $no = is_executable($command) ? null : 'NOT';
167
    $text .= "The command for $what is $no an executable.<br>";
168
169
    return $text;
170
}
171