Passed
Push — master ( ae638f...067730 )
by Felipe
02:07
created

DebugHelper::prettify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Felipe Sayão Lobato Abreu <[email protected]>
4
 * @package CoiSA\ErrorHandler\Helper
5
 * @since 2017-07-25
6
 */
7
8
namespace CoiSA\ErrorHandler\Helper;
9
10
use Throwable;
11
12
/**
13
 * Class DebugHelper
14
 */
15
class DebugHelper
16
{
17
    /**
18
     * @param string $dump
19
     * @return string
20
     */
21
    public static function pre(string $dump): string
22
    {
23
        return "<pre>{$dump}</pre>";
24
    }
25
26
    /**
27
     * @param string $dump
28
     * @return string
29
     */
30
    public static function prettify(string $dump)
31
    {
32
        // @TODO highlight code
33
34
        return self::pre($dump);
35
    }
36
37
    /**
38
     * @param mixed $mixed
39
     * @param bool $pretty
40
     * @return string
41
     */
42
    public static function print_r($mixed, bool $pretty = true): string
43
    {
44
        $print = (string) print_r($mixed, true);
45
46
        return $pretty ? self::prettify($print) :
47
            $print;
48
    }
49
50
    /**
51
     * @param mixed $expression
52
     * @param bool $pretty
53
     * @return string
54
     */
55
    public static function export($expression, bool $pretty = true): string
56
    {
57
        $exported = var_export($expression, true) ?: $expression;
58
59
        return $pretty ? self::prettify($exported) :
0 ignored issues
show
Bug Best Practice introduced by
The expression return $pretty ? self::p...($exported) : $exported could return the type mixed which includes types incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
60
            $exported;
61
    }
62
63
    /**
64
     * @param mixed $expression
65
     * @param bool $pretty
66
     * @return string
67
     */
68
    public static function dump($expression, bool $pretty = true): string
69
    {
70
        ob_start();
71
        try {
72
            var_dump($expression);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($expression) looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
        } catch (\Throwable $exception) {
74
            // DO NOTHING
75
        }
76
77
        $dump = ob_get_clean();
78
79
        return $pretty ? self::prettify($dump) :
80
            $dump;
81
    }
82
}