Completed
Push — master ( fdf5fc...3f8872 )
by Jacob
02:18
created

Display::setFileData()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 3
eloc 18
nc 3
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
                case 'log':
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
                default:
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
    /**
87
     * Sets file data
88
     *
89
     * @param array $data
90
     */
91
    public function setFileData(array $data)
92
    {
93
        $fileData = array(
94
            'fileList'   => array(),
95
            'fileTotals' => array(
96
                'count'   => count($data),
97
                'size'    => 0,
98
                'largest' => 0
99
            )
100
        );
101
102
        foreach ($data as $file) {
103
            array_push($fileData['fileList'], array(
104
                'name' => $file['name'],
105
                'size' => self::getReadableMemory($file['size'])
106
            ));
107
108
            $fileData['fileTotals']['size'] += $file['size'];
109
            if ($file['size'] > $fileData['fileTotals']['largest']) {
110
                $fileData['fileTotals']['largest'] = $file['size'];
111
            }
112
        }
113
114
        $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']);
115
        $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']);
116
117
        $this->output['files'] = $fileData['fileList'];
118
        $this->output['fileTotals'] = $fileData['fileTotals'];
119
    }
120
121
    /**
122
     * Sets memory data
123
     *
124
     * @param array $data
125
     */
126
    public function setMemoryData(array $data)
127
    {
128
        $this->output['memory'] = array(
129
            'used'    => self::getReadableMemory($data['used']),
130
            'allowed' => $data['allowed']
131
        );
132
    }
133
134
    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...
135
    {
136
        // the void
137
    }
138
139
    /**
140
     * Sets speed data
141
     *
142
     * @param array $data
143
     */
144
    public function setSpeedData(array $data)
145
    {
146
        $this->output['speed'] = array(
147
            'elapsed' => self::getReadableTime($data['elapsed']),
148
            'allowed' => self::getReadableTime($data['allowed'], 0)
149
        );
150
    }
151
152
    /**
153
     * Static formatter for human-readable time
154
     * Only handles time up to 60 minutes gracefully
155
     *
156
     * @param double  $time
157
     * @param integer $decimals
158
     * @return string
159
     */
160
    public static function getReadableTime($time, $decimals = 3)
161
    {
162
        $unit = 's';
163
        if ($time < 1) {
164
            $time *= 1000;
165
            $unit = 'ms';
166
        } else if ($time > 60) {
167
            $time /= 60;
168
            $unit = 'm';
169
        }
170
        $time = number_format($time, $decimals);
171
        return "{$time} {$unit}";
172
    }
173
174
    /**
175
     * Static formatter for human-readable memory
176
     *
177
     * @param double  $size
178
     * @param integer $decimals
179
     */
180
    public static function getReadableMemory($size, $decimals = 2)
181
    {
182
        $unitOptions = array('b', 'k', 'M', 'G');
183
184
        $base = log($size, 1024);
185
186
        $memory = round(pow(1024, $base - floor($base)), $decimals);
187
        $unit = $unitOptions[floor($base)];
188
        return "{$memory} {$unit}";
189
    }
190
 
191
    public function __invoke()
192
    {
193
        $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...
194
        require_once __DIR__ .'/../asset/display.tpl.php';
195
    }
196
}	
197