Passed
Push — master ( b0f5d7...c171ec )
by Sergey
02:30
created

Helper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
namespace PhpConsole {
4
5
	/**
6
	 * Makes more easy access to debug dispatcher method.
7
	 *
8
	 * Usage:
9
	 * 1. Call \PhpConsole\Helper::register();
10
	 * 2. Call PC::debug($sql, 'db') or PC::db($sql)
11
	 *
12
	 * It will be the same as calling Handler::getInstance()->debug($var, 'db')
13
	 *
14
	 * @package PhpConsole
15
	 * @version 3.1
16
	 * @link http://consle.com
17
	 * @author Sergey Barbushin http://linkedin.com/in/barbushin
18
	 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
19
	 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
20
	 */
21
	class Helper {
22
23
		/** @var Connector|null */
24
		private static $connector;
25
		/** @var Handler|null */
26
		private static $handler;
27
		/** @var  bool */
28
		protected static $isActive;
29
30
		private function __construct() {
31
		}
32
33
		/**
34
		 * This method must be called to make class "PC" available
35
		 * @param Connector|null $connector
36
		 * @param Handler|null $handler
37
		 * @throws \Exception
38
		 * @return Connector
39
		 */
40 9
		public static function register(Connector $connector = null, Handler $handler = null) {
41 9
			if(static::$connector) {
0 ignored issues
show
Bug introduced by
Since $connector is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $connector to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
42 1
				throw new \Exception('Helper already registered');
43
			}
44 9
			self::$handler = $handler;
45 9
			self::$connector = $connector ? : Connector::getInstance();
46 9
			self::$isActive = self::$connector->isActiveClient();
47 9
			return self::$connector;
48
		}
49
50
		/**
51
		 * Check if method Helper::register() was called before
52
		 * @return bool
53
		 */
54 2
		public static function isRegistered() {
55 2
			return isset(self::$connector);
56
		}
57
58
		/**
59
		 * Get actual helper connector instance
60
		 * @return Connector
61
		 * @throws \Exception
62
		 */
63 3
		public static function getConnector() {
64 3
			if(!self::$connector) {
65 1
				throw new \Exception('Helper is not registered. Call ' . get_called_class() . '::register()');
66
			}
67 2
			return self::$connector;
68
		}
69
70
		/**
71
		 * Get actual handler instance
72
		 * @return Handler
73
		 * @throws \Exception
74
		 */
75 3
		public static function getHandler() {
76 3
			if(!self::$connector) {
77 1
				throw new \Exception('Helper is not registered. Call ' . get_called_class() . '::register()');
78
			}
79 2
			if(!self::$handler) {
80 1
				self::$handler = Handler::getInstance();
81
			}
82 2
			return self::$handler;
83
		}
84
85
		/**
86
		 * Analog of Handler::getInstance()->debug(...) method
87
		 * @param mixed $data
88
		 * @param string|null $tags Tags separated by dot, e.g. "low.db.billing"
89
		 * @param int|array $ignoreTraceCalls Ignore tracing classes by name prefix `array('PhpConsole')` or fixed number of calls to ignore
90
		 */
91 3
		public static function debug($data, $tags = null, $ignoreTraceCalls = 0) {
92 3
			if(self::$isActive) {
93 2
				self::$connector->getDebugDispatcher()->dispatchDebug($data, $tags, is_numeric($ignoreTraceCalls) ? $ignoreTraceCalls + 1 : $ignoreTraceCalls);
94
			}
95 3
		}
96
97
		/**
98
		 * Short access to analog of Handler::getInstance()->debug(...) method
99
		 * You can access it like PC::tagName($debugData, $additionalTags = null)
100
		 * @param string $tags
101
		 * @param $args
102
		 */
103 1
		public static function __callStatic($tags, $args) {
104 1
			if(isset($args[1])) {
105
				$tags .= '.' . $args[1];
106
			}
107 1
			static::debug(isset($args[0]) ? $args[0] : null, $tags, 1);
108 1
		}
109
	}
110
}
111
112
namespace {
113
114
	use PhpConsole\Helper;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
115
116 1
	if(!class_exists('PC', false)) {
117
		/**
118
		 * Helper short class name in global namespace
119
		 */
120
		class PC extends Helper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
121
122
		}
123
	}
124
}
125