Completed
Push — log-refactor ( f0253a )
by Wahiba
02:46
created

Log::echoDebug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class Log
6
{
7
    public static function console($data, $postpone = true)
8
    {
9
        $data = self::consoleDebug($data, $postpone);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as self::consoleDebug($data, $postpone) (which targets Flynt\Utils\Log::consoleDebug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
10
    }
11
12
    public static function error($data, $postpone = true)
13
    {
14
        $data = self::consoleDebug($data, $postpone, 'PHP', 'error');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as self::consoleDebug($data...stpone, 'PHP', 'error') (which targets Flynt\Utils\Log::consoleDebug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
15
    }
16
17
    public static function pp($data, $postpone = true)
18
    {
19
        $data = self::printDebug($data, $postpone);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as self::printDebug($data, $postpone) (which targets Flynt\Utils\Log::printDebug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
    }
21
22
    public static function consoleDebug($data, $postpone, $title = 'PHP', $logType = 'log')
23
    {
24
        $title .= '(' . self::getCallerFile(2) .'):';
25
        $type = gettype($data);
26
        if (is_array($data) || is_object($data)) {
27
            $output = json_encode($data);
28
            $result =  "<script>console.$logType('$title', '($type)', $output);</script>\n";
29
        } else {
30
            $result = "<script>console.$logType('$title', '($type)', '$data');</script>\n";
31
        }
32
        self::echoDebug($result, $postpone);
33
    }
34
35
    public static function printDebug($data, $postpone)
36
    {
37
        $type = gettype($data);
38
        $output = "<pre>";
39
        $output .= "(" . $type . ") ";
40
        ob_start();
41
        print_r($data);
42
        $output .= ob_get_clean();
43
        $output .= "<br />File: <strong>" . self::getCallerFile() . "</strong>";
44
        $output .= "</pre>\n";
45
        self::echoDebug($output, $postpone);
46
    }
47
48
    protected static function echoDebug($data, $postpone)
49
    {
50
        if ($postpone) {
51
            add_action('wp_footer', function () use ($data) {
52
                echo $data;
53
            }, 30);
54
        } else {
55
            echo $data;
56
        }
57
    }
58
59
    protected static function getCallerFile($depth = 1)
60
    {
61
        $debug = debug_backtrace();
62
        $fileName = $debug[$depth]['file'];
63
        $templateDir = get_template_directory() . '/';
64
        return str_replace($templateDir, '', $fileName) . '#' . $debug[$depth]['line'];
65
    }
66
}
67