HelperTrait::staticTrace()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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 9 and the first side effect is on line 9.

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
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
\defined('BASE_PATH') || \define('BASE_PATH', \dirname(__DIR__, 2));
10
11
/**
12
 * @file
13
 * A trait with helpers methods to debug, halt the app and format text to html
14
 */
15
16
/**
17
 * A trait with helpers methods to debug, halt the app and format text to html.
18
 */
19
trait HelperTrait
20
{
21
    /**
22
     * Halts the execution of the program. It's like calling exit() but using builtin Slim Exceptions.
23
     *
24
     * @param string $msg The message to show to the user
25
     *
26
     * @throws \Slim\Exception\SlimException (description)
27
     */
28
    public function halt($msg = 'An error has happened'): void
29
    {
30
        $body = \responseInstance()->getBody();
31
        $body->write($msg);
32
33
        throw new \Slim\Exception\SlimException(\requestInstance(), \responseInstance());
34
    }
35
36
    public static function getBackTrace($offset = 0)
37
    {
38
        $i0 = $offset;
39
        $i1 = $offset + 1;
40
        $backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $offset + 3);
41
42
        return [
43
            'class' => 'Closure' === $backtrace[$i1]['class'] ?
44
            $backtrace[$i0]['file'] :
45
            $backtrace[$i1]['class'],
46
47
            'type' => $backtrace[$i1]['type'],
48
49
            'function' => '{closure}' === $backtrace[$i1]['function']
50
            ? $backtrace[$i0]['function'] :
51
            $backtrace[$i1]['function'],
52
53
            'spacer4' => ' ',
54
            'line' => $backtrace[$i0]['line'],
55
        ];
56
        //dump($backtrace);
57
    }
58
59
    /**
60
     * Converts an ADORecordSet to an array.
61
     *
62
     * @param \ADORecordSet $set   The set
63
     * @param string        $field optionally the field to query for
64
     *
65
     * @return array the parsed array
66
     */
67
    public static function recordSetToArray($set, $field = '')
68
    {
69
        $result = [];
70
71
        if (0 >= $set->recordCount()) {
72
            return $result;
73
        }
74
75
        while (!$set->EOF) {
76
            $result[] = $field ? $set->fields[$field] : $set;
77
            $set->moveNext();
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * Checks if a variable is defined, in which case assign its value to $var1
85
     * If it isn't and $set is true, assign the default value. Otherwise don't
86
     * assign anything to $var1.
87
     *
88
     * @param mixed $var1    The variable to manipulate if $set if true
89
     * @param mixed $var2    The value to assign to $var1 if it's defined
90
     * @param mixed $default The default value to set, it $set is true
91
     * @param bool  $set     True to set the default value if $var2 isn't defined
92
     *
93
     * @return mixed the value of $var2 is $var2 is set, or $default otherwise
94
     */
95
    public function setIfIsset(&$var1, $var2, $default = null, $set = true)
96
    {
97
        if (isset($var2)) {
98
            $var1 = $var2;
99
100
            return $var1;
101
        }
102
103
        if (true === $set) {
104
            $var1 = $default;
105
106
            return $var1;
107
        }
108
109
        return $default;
110
    }
111
112
    /**
113
     * Checks if the $key of an $array is set. If it isn't, optionally set it to
114
     * the default parameter.
115
     *
116
     * @param array      $array   The array to check
117
     * @param int|string $key     The key to check
118
     * @param mixed      $default The default value to set, it $set is true
119
     * @param bool       $set     True to set the default value if $key isn't
120
     *                            set
121
     *
122
     * @return array the original array
123
     */
124
    public function coalesceArr(&$array, $key, $default = null, $set = true)
125
    {
126
        if (!isset($array[$key]) && true === $set) {
127
            $array[$key] = $default;
128
        }
129
130
        return $array;
131
    }
132
133
    public static function formatSizeUnits($bytes, $lang)
134
    {
135
        if (-1 === $bytes) {
136
            $bytes = $lang['strnoaccess'];
137
        } elseif (1099511627776 <= $bytes) {
138
            $bytes = \sprintf('%s %s', \number_format($bytes / 1099511627776, 0), $lang['strtb']);
139
        } elseif (1073741824 <= $bytes) {
140
            $bytes = \sprintf('%s %s', \number_format($bytes / 1073741824, 0), $lang['strgb']);
141
        } elseif (1048576 <= $bytes) {
142
            $bytes = \sprintf('%s %s', \number_format($bytes / 1048576, 0), $lang['strmb']);
143
        } elseif (1024 <= $bytes) {
144
            $bytes = \sprintf('%s %s', \number_format($bytes / 1024, 0), $lang['strkb']);
145
        } else {
146
            $bytes = \sprintf('%s %s', $bytes, $lang['strbytes']);
147
        }
148
149
        return $bytes;
150
    }
151
152
    /**
153
     * Receives N parameters and sends them to the console adding where was it called from.
154
     *
155
     * @param array<int, mixed> $args
156
     */
157
    public function prtrace(...$args): void
158
    {
159
        if (\function_exists('\dump')) {
160
            \dump($args);
161
        }
162
    }
163
164
    /**
165
     * Just an alias of prtrace.
166
     *
167
     * @param array<int, mixed> $args The arguments
168
     */
169
    public function dump(...$args): void
170
    {
171
        if (\function_exists('\dump')) {
172
            \dump($args);
173
        }
174
    }
175
176
    /**
177
     * Just an alias of prtrace.
178
     *
179
     * @param array<int, mixed> $args The arguments
180
     */
181
    public function dumpAndDie(...$args): void
182
    {
183
        if (\function_exists('\dump')) {
184
            \dump($args);
185
        }
186
187
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
188
    }
189
190
    /**
191
     * Receives N parameters and sends them to the console adding where was it
192
     * called from.
193
     *
194
     * @param array<int, mixed> $args
195
     */
196
    public static function staticTrace(
197
        ...$args
198
    ): void {
199
        if (\function_exists('\dump')) {
200
            \dump($args);
201
        }
202
    }
203
}
204