Completed
Push — master ( c58c9b...07c22d )
by Jacob
02:25
created

Console::logError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 10
rs 9.4286
c 1
b 1
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
3
/* - - - - - - - - - - - - - - - - - - - - -
4
5
 Title : PHP Quick Profiler Console Class
6
 Author : Created by Ryan Campbell
7
 URL : http://particletree.com/features/php-quick-profiler/
8
9
 Last Updated : April 22, 2009
10
11
 Description : This class serves as a wrapper around a global
12
 php variable, debugger_logs, that we have created.
13
14
- - - - - - - - - - - - - - - - - - - - - */
15
16
namespace Particletree\Pqp;
17
18
class Console {
19
	
20
	/*-----------------------------------
21
	     LOG A VARIABLE TO CONSOLE
22
	------------------------------------*/
23
	
24
	public static function log($data) {
0 ignored issues
show
Coding Style introduced by
log uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
		$logItem = array(
26
			"data" => $data,
27
			"type" => 'log'
28
		);
29
		$GLOBALS['debugger_logs']['console'][] = $logItem;
30
		$GLOBALS['debugger_logs']['logCount'] += 1;
31
	}
32
	
33
	/*---------------------------------------------------
34
	     LOG MEMORY USAGE OF VARIABLE OR ENTIRE SCRIPT
35
	-----------------------------------------------------*/
36
	
37
	public function logMemory($object = false, $name = 'PHP') {
0 ignored issues
show
Coding Style introduced by
logMemory uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
38
		$memory = memory_get_usage();
39
		if($object) $memory = strlen(serialize($object));
40
		$logItem = array(
41
			"data" => $memory,
42
			"type" => 'memory',
43
			"name" => $name,
44
			"dataType" => gettype($object)
45
		);
46
		$GLOBALS['debugger_logs']['console'][] = $logItem;
47
		$GLOBALS['debugger_logs']['memoryCount'] += 1;
48
	}
49
	
50
	/*-----------------------------------
51
	     LOG A PHP EXCEPTION OBJECT
52
	------------------------------------*/
53
	
54
	public function logError($exception, $message) {
0 ignored issues
show
Coding Style introduced by
logError uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
		$logItem = array(
56
			"data" => $message,
57
			"type" => 'error',
58
			"file" => $exception->getFile(),
59
			"line" => $exception->getLine()
60
		);
61
		$GLOBALS['debugger_logs']['console'][] = $logItem;
62
		@$GLOBALS['debugger_logs']['errorCount'] += 1;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
	}
64
	
65
	/*------------------------------------
66
	     POINT IN TIME SPEED SNAPSHOT
67
	-------------------------------------*/
68
	
69
	public function logSpeed($name = 'Point in Time') {
0 ignored issues
show
Coding Style introduced by
logSpeed uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
70
		$logItem = array(
71
			"data" => PhpQuickProfiler::getMicroTime(),
72
			"type" => 'speed',
73
			"name" => $name
74
		);
75
		$GLOBALS['debugger_logs']['console'][] = $logItem;
76
		$GLOBALS['debugger_logs']['speedCount'] += 1;
77
	}
78
	
79
	/*-----------------------------------
80
	     SET DEFAULTS & RETURN LOGS
81
	------------------------------------*/
82
	
83
	public function getLogs() {
0 ignored issues
show
Coding Style introduced by
getLogs uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
		if(!$GLOBALS['debugger_logs']['memoryCount']) $GLOBALS['debugger_logs']['memoryCount'] = 0;
85
		if(!$GLOBALS['debugger_logs']['logCount']) $GLOBALS['debugger_logs']['logCount'] = 0;
86
		if(!$GLOBALS['debugger_logs']['speedCount']) $GLOBALS['debugger_logs']['speedCount'] = 0;
87
		if(!$GLOBALS['debugger_logs']['errorCount']) $GLOBALS['debugger_logs']['errorCount'] = 0;
88
		return $GLOBALS['debugger_logs'];
89
	}
90
}
91
92
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
93