Completed
Push — master ( f91332...b54280 )
by Lars
03:09
created

KintBootup.php ➔ dump()   B

Complexity

Conditions 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 4
c 1
b 1
f 1
dl 0
loc 30
ccs 0
cts 19
cp 0
crap 20
rs 8.5806
1
<?php
2
3
namespace kint;
4
5
/**
6
 * Class KintBootup
7
 *
8
 * @package kint
9
 */
10
class KintBootup
11
{
12
  /**
13
   * init
14
   */
15
  public static function init()
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $GLOBALS 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
    if (defined('KINT_DIR')) {
18
      return;
19
    }
20
21
    define('KINT_DIR', __DIR__ . '/');
22
23
    require KINT_DIR . 'config.default.php';
24
25
    # init settings
26
    if (!empty($GLOBALS['_kint_settings'])) {
27
      Kint::enabled($GLOBALS['_kint_settings']['enabled']);
28
29
      foreach ($GLOBALS['_kint_settings'] as $key => $val) {
30
        /** @noinspection PhpVariableVariableInspection */
31
        property_exists('Kint', $key) and Kint::$$key = $val;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
32
      }
33
34
      unset($GLOBALS['_kint_settings'], $key, $val);
35
    }
36
  }
37
38
  public static function initFunctions()
39
  {
40
    /**
41
     * quick-debug: print the variable $var [exit] [echo || return]
42
     *
43
     * @param mixed   $var
44
     * @param boolean $exit      exit after debug-output? (only via "echo")
45
     * @param boolean $echo      true => use "echo", otherwise use "return"
46
     * @param boolean $plaintext use a simple "print_r()" instead of kind?
47
     *
48
     * @return string
49
     */
50
    function dump($var, $exit = true, $echo = true, $plaintext = false)
51
    {
52
      Kint::enabled(true);
53
      $stash = Kint::settings();
54
      Kint::$cliDetection = true;
55
      Kint::$delayedMode = true;
56
      Kint::$expandedByDefault = true;
57
58
      if ($plaintext === true) {
59
        Kint::enabled(Kint::MODE_WHITESPACE);
60
        $output = Kint::dump($var);
61
      } else {
62
        $output = Kint::dump($var);
63
      }
64
65
      Kint::settings($stash);
66
      Kint::enabled(false);
67
68
      if ($echo === true) {
69
        echo $output;
70
71
        if ($exit) {
72
          exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function dump() 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...
73
        }
74
75
        return '';
76
      }
77
78
      return $output;
79
    }
80
81
    /**
82
     * Alias of Kint::dump()
83
     *
84
     * @return string
85
     */
86
    function d()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
    {
88
      return call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
89
    }
90
91
92
    /**
93
     * Alias of Kint::dump()
94
     * [!!!] IMPORTANT: execution will halt after call to this function
95
     *
96
     * @return string
97
     * @deprecated
98
     */
99
    function dd()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
      if (!Kint::enabled()) {
102
        return '';
103
      }
104
105
      echo "<pre>Kint: dd() is being deprecated, please use ddd() instead</pre>\n";
106
      call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
107
      exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function dd() 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...
108
    }
109
110
    /**
111
     * Alias of Kint::dump()
112
     * [!!!] IMPORTANT: execution will halt after call to this function
113
     *
114
     * @return string
115
     */
116
    function ddd()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
    {
118
      if (!Kint::enabled()) {
119
        return '';
120
      }
121
122
      call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
123
      exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function ddd() 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...
124
    }
125
126
    /**
127
     * Alias of Kint::dump(), however the output is delayed until the end of the script
128
     *
129
     * @see d();
130
     *
131
     * @return string
132
     */
133
    function de()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
      $stash = Kint::settings();
136
      Kint::$delayedMode = true;
137
      $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
138
      Kint::settings($stash);
139
140
      return $out;
141
    }
142
143
    /**
144
     * Alias of Kint::dump(), however the output is in plain html-escaped text and some minor visibility enhancements
145
     * added. If run in CLI mode, output is pure whitespace.
146
     *
147
     * To force rendering mode without auto-detecting anything:
148
     *
149
     *  Kint::enabled( Kint::MODE_PLAIN );
150
     *  Kint::dump( $variable );
151
     *
152
     * [!!!] IMPORTANT: execution will halt after call to this function
153
     *
154
     * @return string
155
     */
156
    function s()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
157
    {
158
      if (!Kint::enabled()) {
159
        return '';
160
      }
161
162
      $stash = Kint::settings();
163
164
      if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
165
        Kint::enabled(Kint::MODE_PLAIN);
166
        if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
167
          Kint::enabled(Kint::MODE_CLI);
168
        }
169
      }
170
171
      $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
172
173
      Kint::settings($stash);
174
175
      return $out;
176
    }
177
178
    /**
179
     * @see s()
180
     *
181
     * [!!!] IMPORTANT: execution will halt after call to this function
182
     *
183
     * @return string
184
     */
185
    function sd()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
186
    {
187
      if (!Kint::enabled()) {
188
        return '';
189
      }
190
191
      if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
192
        Kint::enabled(Kint::MODE_PLAIN);
193
        if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
194
          Kint::enabled(Kint::MODE_CLI);
195
        }
196
      }
197
198
      call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
199
      exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function sd() 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...
200
    }
201
202
    /**
203
     * @see s()
204
     * @see de()
205
     *
206
     * @return string
207
     */
208
    function se()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
209
    {
210
      if (!Kint::enabled()) {
211
        return '';
212
      }
213
214
      $stash = Kint::settings();
215
216
      Kint::$delayedMode = true;
217
218
      if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
219
        Kint::enabled(Kint::MODE_PLAIN);
220
        if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
221
          Kint::enabled(Kint::MODE_CLI);
222
        }
223
      }
224
225
      $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
226
227
      Kint::settings($stash);
228
229
      return $out;
230
    }
231
232
    /**
233
     * Alias of Kint::dump(), however the output is dumped to the javascript console and
234
     * added to the global array `kintDump`. If run in CLI mode, output is pure whitespace.
235
     *
236
     * To force rendering mode without autodetecting anything:
237
     *
238
     *  Kint::enabled( Kint::MODE_JS );
239
     *  Kint::dump( $variable );
240
     *
241
     * @return string
242
     */
243
    function j()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
244
    {
245
      if (!Kint::enabled()) {
246
        return '';
247
      }
248
249
      $stash = Kint::settings();
250
251
      Kint::enabled(
252
          PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
253
      );
254
255
      $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
256
257
      Kint::settings($stash);
258
259
      return $out;
260
    }
261
262
    /**
263
     * @see j()
264
     *
265
     * [!!!] IMPORTANT: execution will halt after call to this function
266
     *
267
     * @return string
268
     */
269
    function jd()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
270
    {
271
      if (!Kint::enabled()) {
272
        return '';
273
      }
274
275
      Kint::enabled(
276
          PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
277
      );
278
279
      call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
280
281
      exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jd() 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...
282
    }
283
284
    /**
285
     * @see j()
286
     * @see de()
287
     *
288
     * @return string
289
     */
290
    function je()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
291
    {
292
      if (!Kint::enabled()) {
293
        return '';
294
      }
295
296
      $stash = Kint::settings();
297
298
      Kint::$delayedMode = true;
299
300
      Kint::enabled(
301
          PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
302
      );
303
304
      $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
305
306
      Kint::settings($stash);
307
308
      return $out;
309
    }
310
  }
311
312
}
313