Completed
Pull Request — develop (#291)
by Doğa
36:34 queued 25:07
created

functions.php ➔ consoleDebug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\ComponentLogServer;
4
5
use Flynt;
6
7
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\');
8
9
add_action('Flynt/afterRegisterFeatures', function () {
10
    $componentManager = Flynt\ComponentManager::getInstance();
11
    $componentWhitelist = [];
12
    if (isset($_GET['component']) && !empty($_GET['component'])) {
13
        $componentWhitelist = explode(',', $_GET['component']);
14
    }
15
    if (count($componentWhitelist) === 0) {
16
        foreach ($componentManager->getAll() as $name => $path) {
17
            add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
18
        }
19
    } else {
20
        foreach ($componentManager->getAll() as $name => $path) {
21
            if (in_array($name, $componentWhitelist)) {
22
                add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
23
            }
24
        }
25
    }
26
}, 11);
27
28
function addDebugInfo($data, $parentData, $config)
29
{
30
    if ((WP_ENV === 'development' || current_user_can('editor') || current_user_can('administrator')) && isset($_GET['log'])) {
31
        consoleDebug([
32
            'component' => $config['name'],
33
            'config' => $config,
34
            'data' => $data,
35
            'parentData' => $parentData,
36
        ]);
37
    }
38
39
    return $data;
40
}
41
42
function consoleDebug($data, $postpone = true)
43
{
44
    $type = gettype($data);
0 ignored issues
show
Unused Code introduced by
$type 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...
45
    $output = json_encode($data);
46
    $result =  "<script>console.log({$output});</script>\n";
47
    echoDebug($result, $postpone);
48
}
49
50
function echoDebug($data, $postpone)
51
{
52
    if ($postpone) {
53
        add_action('wp_footer', function () use ($data) {
54
            echo $data;
55
        }, 30);
56
    } else {
57
        echo $data;
58
    }
59
}
60