Completed
Push — master ( 2f6da3...fdf5fc )
by Jacob
02:08
created

Display::setConsoleData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*****************************************
4
 * Title : Php Quick Profiler Display Class
5
 * Author : Created by Ryan Campbell
6
 * URL : http://particletree.com/features/php-quick-profiler/
7
 * Description : This is a hacky way of pushing profiling logic to the
8
 *  PQP HTML. This is great because it will just work in your project,
9
 *  but it is hard to maintain and read.
10
*****************************************/
11
12
namespace Particletree\Pqp;
13
14
class Display
15
{
16
17
    /** @var  array */
18
    protected $output;
19
20
    public function __construct()
21
    {
22
    }
23
24
    public function setConsole(Console $console)
25
    {
26
        $console_data = array(
27
            'messages' => array(),
28
            'count'    => array(
29
                'log'    => 0,
30
                'memory' => 0,
31
                'error'  => 0,
32
                'speed'  => 0
33
            )
34
        );
35
        foreach ($console->getLogs() as $log) {
36
            switch($log['type']) {
37 View Code Duplication
                case 'log':
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...
38
                    $message = array(
39
                        'data' => print_r($log['data'], true),
40
                        'type' => 'log'
41
                    );
42
                    $console_data['count']['log']++;
43
                    break;
44
                case 'memory':
45
                    $message = array(
46
                        'name' => $log['name'],
47
                        'data' => self::getReadableMemory($log['data']),
48
                        'type' => 'memory'
49
                    );
50
                    if (!empty($log['data_type'])) {
51
                        $message['data_type'] = $log['data_type'];
52
                    }
53
                    $console_data['count']['memory']++;
54
                    break;
55
                case 'error':
56
                    $message = array(
57
                        'data' => $log['data'],
58
                        'file' => $log['file'],
59
                        'line' => $log['line'],
60
                        'type' => 'error'
61
                    );
62
                    $console_data['count']['error']++;
63
                    break;
64
                case 'speed':
65
                    $elapsedTime = $log['data'] - $this->startTime;
0 ignored issues
show
Bug introduced by
The property startTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
                    $message = array(
67
                        'name' => $log['name'],
68
                        'data' => self::getReadableTime($elapsedTime),
69
                        'type' => 'speed'
70
                    );
71
                    $console_data['count']['speed']++;
72
                    break;
73 View Code Duplication
                default:
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...
74
                    $message = array(
75
                        'data' => "Unrecognized console log type: {$log['type']}",
76
                        'type' => 'error'
77
                    );
78
                    $console_data['count']['error']++;
79
                    break;
80
            }
81
            array_push($console_data['messages'], $message);
82
        }
83
        $this->output['console'] = $console_data;
84
    }
85
86
    public function setFileData(array $file_data)
87
    {
88
        $this->output['files'] = $file_data['files'];
89
        $this->output['fileTotals'] = $file_data['fileTotals'];
90
    }
91
92
    /**
93
     * Sets memory data
94
     *
95
     * @param array $data
96
     */
97
    public function setMemoryData(array $data)
98
    {
99
        $this->output['memory'] = array(
100
            'used'    => self::getReadableMemory($data['used']),
101
            'allowed' => $data['allowed']
102
        );
103
    }
104
105
    public function setQueryData(array $query_data)
0 ignored issues
show
Unused Code introduced by
The parameter $query_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        // the void
108
    }
109
110
    /**
111
     * Sets speed data
112
     *
113
     * @param array $data
114
     */
115
    public function setSpeedData(array $data)
116
    {
117
        $this->output['speed'] = array(
118
            'elapsed' => self::getReadableTime($data['elapsed']),
119
            'allowed' => self::getReadableTime($data['allowed'], 0)
120
        );
121
    }
122
123
    /**
124
     * Static formatter for human-readable time
125
     * Only handles time up to 60 minutes gracefully
126
     *
127
     * @param double  $time
128
     * @param integer $decimals
129
     * @return string
130
     */
131 View Code Duplication
    public static function getReadableTime($time, $decimals = 3)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
132
    {
133
        $unit = 's';
134
        if ($time < 1) {
135
            $time *= 1000;
136
            $unit = 'ms';
137
        } else if ($time > 60) {
138
            $time /= 60;
139
            $unit = 'm';
140
        }
141
        $time = number_format($time, $decimals);
142
        return "{$time} {$unit}";
143
    }
144
145
    /**
146
     * Static formatter for human-readable memory
147
     *
148
     * @param double  $size
149
     * @param integer $decimals
150
     */
151
    public static function getReadableMemory($size, $decimals = 2)
152
    {
153
        $unitOptions = array('b', 'k', 'M', 'G');
154
155
        $base = log($size, 1024);
156
157
        $memory = round(pow(1024, $base - floor($base)), $decimals);
158
        $unit = $unitOptions[floor($base)];
159
        return "{$memory} {$unit}";
160
    }
161
 
162
    public function __invoke()
163
    {
164
        $output = $this->output;
0 ignored issues
show
Unused Code introduced by
$output 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...
165
        require_once __DIR__ .'/../asset/display.tpl.php';
166
    }
167
}	
168