Completed
Push — master ( 07c22d...01f91a )
by Jacob
02:50
created

Console::getLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
rs 9.6667
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/*****************************************
4
 * Title : PHP Quick Profiler Console Class
5
 * Author : Created by Ryan Campbell
6
 * URL : http://particletree.com/features/php-quick-profiler/
7
 * Description : This class serves as a wrapper to hold onto
8
 *  various messages and profiling data
9
 *****************************************/
10
11
namespace Particletree\Pqp;
12
use Exception;
13
14
class Console
15
{
16
17
    /** @var  array */
18
    protected $log = array();
19
20
    /** @var  array */
21
    protected $memory = array();
22
23
    /** @var  array */
24
    protected $error = array();
25
26
    /** @var  array */
27
    protected $speed = array();
28
29
    /**
30
     * Logs data to the console
31
     * Accepts any data type
32
     *
33
     * @param mixed $data
34
     */
35
    public function log($data)
36
    {
37
        array_push($this->logs, $data);
0 ignored issues
show
Bug introduced by
The property logs 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...
38
    }
39
40
    /**
41
     * Logs memory usage of a variable
42
     * If no parameter is passed in, logs current memory usage
43
     *
44
     * @param mixed $object
45
     * @param string $name
46
     */
47
    public function logMemory($object = null, $name = '')
48
    {
49
        if (!is_null($object)) {
50
            $memory = strlen(serialize($object));
51
        } else {
52
            $memory = memory_get_usage();
53
            $name = 'PHP';
54
        }
55
56
        array_push($this->memory, array(
57
            'data' => $memory,
58
            'name' => $name,
59
            'type' => gettype($object)
60
        ));
61
    }
62
63
    /**
64
     * Logs exception with optional message override
65
     *
66
     * @param Exception $exception
67
     * @param string    $message
68
     */
69
    public function logError(Exception $exception, $message = '')
70
    {
71
        if (empty($message)) {
72
            $message = $exception->getMessage();
73
        }
74
75
        array_push($this->error, array(
76
            'data' => $message,
77
            'file' => $exception->getFile(),
78
            'line' => $exception->getLine(),
79
        ));
80
    }
81
82
    /**
83
     * Logs current time with optional message
84
     *
85
     * @param string $name
86
     */
87
    public function logSpeed($name = 'Point in Time')
88
    {
89
        array_push($this->speed, array(
90
            'data' => microtime(true),
91
            'name' => $name,
92
        ));
93
    }
94
95
    /**
96
     * Returns the collected logs
97
     *
98
     * @returns array
99
     */
100
    public function getLogs()
101
    {
102
        return array(
103
            'log'    => $this->log,
104
            'memory' => $this->memory,
105
            'error'  => $this->error,
106
            'speed'  => $this->speed
107
        );
108
    }
109
}
110