Passed
Push — develop ( 0cc231...3902ed )
by Felipe
05:42
created

HelperTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 210
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A halt() 0 6 1
A addFlash() 0 17 2
A recordSetToArray() 0 12 4
B prtrace() 0 31 1
A setIfIsset() 0 10 3
A statictrace() 0 15 1
A coalesceArr() 0 6 3
B formatSizeUnits() 0 15 5
A br2ln() 0 3 1
A dump() 0 3 1
A dumpAndDie() 0 10 1
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.43
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * @file
11
 * A trait with helpers methods to debug, halt the app and format text to html
12
 */
13
14
/**
15
 * A trait with helpers methods to debug, halt the app and format text to html.
16
 *
17
 * @package PHPPgAdmin
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')
29
    {
30
        $body = $this->container->responseobj->getBody();
31
        $body->write($msg);
32
33
        throw new \Slim\Exception\SlimException($this->container->requestobj, $this->container->responseobj);
34
    }
35
36
    /**
37
     * Adds a flash message to the session that will be displayed on the next request
38
     *
39
     * @param mixed  $content  msg content (can be object, array, etc)
40
     * @param string $key      The key to associate with the message. Defaults to the stack
41
     *                         trace of the closure or method that called addFlassh
42
     */
43
    public function addFlash($content, $key = '')
44
    {
45
        if ($key === '') {
46
            $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
47
48
            $btarray0 = ([
49
                'class2'    => $backtrace[1]['class'],
50
                'type2'     => $backtrace[1]['type'],
51
                'function2' => $backtrace[1]['function'],
52
                'spacer4'   => ' ',
53
                'line2'     => $backtrace[0]['line'],
54
            ]);
55
56
            $key = implode('', $btarray0);
57
        }
58
59
        $this->container->flash->addMessage($key, $content);
60
    }
61
62
    /**
63
     * Receives N parameters and sends them to the console adding where was it called from.
64
     */
65
    public function prtrace()
66
    {
67
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
68
69
        $btarray0 = ([
70
            /*'class0'    => $backtrace[3]['class'],
71
            'type0'     => $backtrace[3]['type'],
72
            'function0' => $backtrace[3]['function'],
73
            'spacer0'   => ' ',
74
            'line0'     => $backtrace[2]['line'],
75
76
            'spacer1'   => "\n",
77
78
            'class1'    => $backtrace[2]['class'],
79
            'type1'     => $backtrace[2]['type'],
80
            'function1' => $backtrace[2]['function'],
81
            'spacer2'   => ' ',
82
            'line1'     => $backtrace[1]['line'],
83
84
            'spacer3'   => "\n",*/
85
86
            'class2'    => $backtrace[1]['class'],
87
            'type2'     => $backtrace[1]['type'],
88
            'function2' => $backtrace[1]['function'],
89
            'spacer4'   => ' ',
90
            'line2'     => $backtrace[0]['line'],
91
        ]);
92
93
        $tag = implode('', $btarray0);
94
95
        \PC::debug(func_get_args(), $tag);
96
    }
97
98
    /**
99
     * Converts an ADORecordSet to an array
100
     *
101
     * @param \PHPPgAdmin\ADORecordSet  $set  The set
102
     * @param string $field optionally the field to query for
103
     *
104
     * @return array the parsed array
105
     */
106
    public static function recordSetToArray($set, $field = '')
107
    {
108
        $result = [];
109
110
        if ($set->recordCount() <= 0) {
111
            return $result;
112
        }
113
        while (!$set->EOF) {
114
            $result[] = $field ? $set->fields[$field] : $set;
115
            $set->moveNext();
116
        }
117
        return $result;
118
    }
119
120
    /**
121
     * Checks if a variable is defined, in which case assign its value to $var1
122
     * If it isn't and $set is true, assign the default value. Otherwise don't
123
     * assign anything to $var1
124
     *
125
     * @param mixed       $var1    The variable to manipulate if $set if true
126
     * @param mixed       $var2    The value to assign to $var1 if it's defined
127
     * @param mixed       $default  The default value to set, it $set is true
128
     * @param bool        $set      True to set the default value if $var2 isn't defined
129
     *
130
     * @return mixed  the value of $var2 is $var2 is set, or $default otherwise
131
     */
132
    public function setIfIsset(&$var1, $var2, $default = null, $set = true)
133
    {
134
        if (isset($var2)) {
135
            $var1 = $var2;
136
            return $var1;
137
        } elseif ($set === true) {
138
            $var1 = $default;
139
            return $var1;
140
        }
141
        return $default;
142
    }
143
144
    /**
145
     * Checks if the $key of an $array is set. If it isn't, optionally set it to
146
     * the default parameter
147
     *
148
     * @param array       $array    The array to check
149
     * @param string|int  $key      The key to check
150
     * @param mixed       $default  The default value to set, it $set is true
151
     * @param bool        $set      True to set the default value if $key isn't
152
     *                              set
153
     *
154
     * @return array  the original array
155
     */
156
    public function coalesceArr(&$array, $key, $default = null, $set = true)
157
    {
158
        if (!isset($array[$key]) && $set === true) {
159
            $array[$key] = $default;
160
        }
161
        return $array;
162
    }
163
164
    /**
165
     * Receives N parameters and sends them to the console adding where was it
166
     * called from.
167
     */
168
    public static function statictrace()
169
    {
170
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
171
172
        $btarray0 = [
173
            'class'    => $backtrace[1]['class'],
174
            'type'     => $backtrace[1]['type'],
175
            'function' => $backtrace[1]['function'],
176
            'spacer'   => ' ',
177
            'line'     => $backtrace[0]['line'],
178
        ];
179
180
        $tag = implode('', $btarray0);
181
182
        \PC::debug(func_get_args(), $tag);
183
    }
184
185
    public static function formatSizeUnits($bytes, $lang)
186
    {
187
        if ($bytes >= 1099511627776) {
188
            $bytes = sprintf('%s %s', number_format($bytes / 1099511627776, 0), $lang['strtb']);
189
        } elseif ($bytes >= 1073741824) {
190
            $bytes = sprintf('%s %s', number_format($bytes / 1073741824, 0), $lang['strgb']);
191
        } elseif ($bytes >= 1048576) {
192
            $bytes = sprintf('%s %s', number_format($bytes / 1048576, 0), $lang['strmb']);
193
        } elseif ($bytes >= 1024) {
194
            $bytes = sprintf('%s %s', number_format($bytes / 1024, 0), $lang['strkb']);
195
        } else {
196
            $bytes = sprintf('%s %s', $bytes, $lang['strbytes']);
197
        }
198
199
        return $bytes;
200
    }
201
202
    /**
203
     * Returns a string with html <br> variant replaced with a new line.
204
     *
205
     * @param string $msg message to parse (<br> separated)
206
     *
207
     * @return string parsed message (linebreak separated)
208
     */
209
    public static function br2ln($msg)
210
    {
211
        return str_replace(['<br>', '<br/>', '<br />'], "\n", $msg);
212
    }
213
214
    public function dump()
215
    {
216
        call_user_func_array('\Kint::dump', func_get_args());
217
    }
218
219
    public function dumpAndDie()
220
    {
221
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
222
223
        $folder = dirname(dirname(__DIR__));
224
        $file   = str_replace($folder, '', $backtrace[0]['file']);
225
        $line   = $backtrace[0]['line'];
226
227
        call_user_func_array('\Kint::dump', func_get_args());
228
        $this->halt('stopped by user at ' . $file . ' line ' . $line);
229
    }
230
}
231