Completed
Push — master ( 61aed0...6cabfa )
by Lars
05:09
created

KintBootup.php ➔ sd()   B

Complexity

Conditions 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 5
c 3
b 0
f 2
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 30
rs 8.5454
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 function initFunctions()
39
  {
40
    if (!function_exists('d')) {
41
      /**
42
       * Alias of Kint::dump()
43
       *
44
       * @return string
45
       */
46
      function d()
47
      {
48
        return call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
49
      }
50
    }
51
52 View Code Duplication
    if (!function_exists('dd')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
      /**
54
       * Alias of Kint::dump()
55
       * [!!!] IMPORTANT: execution will halt after call to this function
56
       *
57
       * @return string
58
       * @deprecated
59
       */
60
      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...
61
      {
62
        if (!Kint::enabled()) {
63
          return '';
64
        }
65
66
        echo "<pre>Kint: dd() is being deprecated, please use ddd() instead</pre>\n";
67
        call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
68
        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...
69
      }
70
    }
71
72 View Code Duplication
    if (!function_exists('ddd')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
      /**
74
       * Alias of Kint::dump()
75
       * [!!!] IMPORTANT: execution will halt after call to this function
76
       *
77
       * @return string
78
       */
79
      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...
80
      {
81
        if (!Kint::enabled()) {
82
          return '';
83
        }
84
85
        call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
86
        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...
87
      }
88
    }
89
90
    if (!function_exists('de')) {
91
      /**
92
       * Alias of Kint::dump(), however the output is delayed until the end of the script
93
       *
94
       * @see d();
95
       *
96
       * @return string
97
       */
98
      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...
99
      {
100
        $stash = Kint::settings();
101
        Kint::$delayedMode = true;
102
        $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
103
        Kint::settings($stash);
104
105
        return $out;
106
      }
107
    }
108
109 View Code Duplication
    if (!function_exists('s')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
      /**
111
       * Alias of Kint::dump(), however the output is in plain html-escaped text and some minor visibility enhancements
112
       * added. If run in CLI mode, output is pure whitespace.
113
       *
114
       * To force rendering mode without auto-detecting anything:
115
       *
116
       *  Kint::enabled( Kint::MODE_PLAIN );
117
       *  Kint::dump( $variable );
118
       *
119
       * [!!!] IMPORTANT: execution will halt after call to this function
120
       *
121
       * @return string
122
       */
123
      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...
124
      {
125
        if (!Kint::enabled()) {
126
          return '';
127
        }
128
129
        $stash = Kint::settings();
130
131
        if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
132
          Kint::enabled(Kint::MODE_PLAIN);
133
          if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
134
            Kint::enabled(Kint::MODE_CLI);
135
          }
136
        }
137
138
        $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
139
140
        Kint::settings($stash);
141
142
        return $out;
143
      }
144
    }
145
146
    if (!function_exists('sd')) {
147
      /**
148
       * @see s()
149
       *
150
       * [!!!] IMPORTANT: execution will halt after call to this function
151
       *
152
       * @return string
153
       */
154
      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...
155
      {
156
        if (!Kint::enabled()) {
157
          return '';
158
        }
159
160
        if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
161
          Kint::enabled(Kint::MODE_PLAIN);
162
          if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
163
            Kint::enabled(Kint::MODE_CLI);
164
          }
165
        }
166
167
        call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
168
        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...
169
      }
170
    }
171
172 View Code Duplication
    if (!function_exists('se')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
      /**
174
       * @see s()
175
       * @see de()
176
       *
177
       * @return string
178
       */
179
      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...
180
      {
181
        if (!Kint::enabled()) {
182
          return '';
183
        }
184
185
        $stash = Kint::settings();
186
187
        Kint::$delayedMode = true;
188
189
        if (Kint::enabled() !== Kint::MODE_WHITESPACE) {
190
          Kint::enabled(Kint::MODE_PLAIN);
191
          if (PHP_SAPI === 'cli' && Kint::$cliDetection === true) {
192
            Kint::enabled(Kint::MODE_CLI);
193
          }
194
        }
195
196
        $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
197
198
        Kint::settings($stash);
199
200
        return $out;
201
      }
202
    }
203
204 View Code Duplication
    if (!function_exists('j')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
      /**
206
       * Alias of Kint::dump(), however the output is dumped to the javascript console and
207
       * added to the global array `kintDump`. If run in CLI mode, output is pure whitespace.
208
       *
209
       * To force rendering mode without autodetecting anything:
210
       *
211
       *  Kint::enabled( Kint::MODE_JS );
212
       *  Kint::dump( $variable );
213
       *
214
       * @return string
215
       */
216
      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...
217
      {
218
        if (!Kint::enabled()) {
219
          return '';
220
        }
221
222
        $stash = Kint::settings();
223
224
        Kint::enabled(
225
            PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
226
        );
227
228
        $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
229
230
        Kint::settings($stash);
231
232
        return $out;
233
      }
234
    }
235
236
    if (!function_exists('jd')) {
237
      /**
238
       * @see j()
239
       *
240
       * [!!!] IMPORTANT: execution will halt after call to this function
241
       *
242
       * @return string
243
       */
244
      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...
245
      {
246
        if (!Kint::enabled()) {
247
          return '';
248
        }
249
250
        Kint::enabled(
251
            PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
252
        );
253
254
        call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
255
256
        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...
257
      }
258
    }
259
260 View Code Duplication
    if (!function_exists('je')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
      /**
262
       * @see j()
263
       * @see de()
264
       *
265
       * @return string
266
       */
267
      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...
268
      {
269
        if (!Kint::enabled()) {
270
          return '';
271
        }
272
273
        $stash = Kint::settings();
274
275
        Kint::$delayedMode = true;
276
277
        Kint::enabled(
278
            PHP_SAPI === 'cli' && Kint::$cliDetection === true ? Kint::MODE_CLI : Kint::MODE_JS
279
        );
280
281
        $out = call_user_func_array(array('kint\Kint', 'dump'), func_get_args());
282
283
        Kint::settings($stash);
284
285
        return $out;
286
      }
287
    }
288
  }
289
}
290