Complex classes like Logger often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Logger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Logger |
||
29 | { |
||
30 | const DEFAULT_NAMESPACE = 'PSFS'; |
||
31 | use SingletonTrait; |
||
32 | /** |
||
33 | * @var \Monolog\Logger |
||
34 | */ |
||
35 | protected $logger; |
||
36 | /** |
||
37 | * @var resource |
||
38 | */ |
||
39 | private $stream; |
||
40 | /** |
||
41 | * @var UidProcessor |
||
42 | */ |
||
43 | private $uuid; |
||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $log_level; |
||
48 | |||
49 | /** |
||
50 | * Logger constructor. |
||
51 | * @throws exception\GeneratorException |
||
52 | */ |
||
53 | 2 | public function __construct() |
|
62 | |||
63 | 1 | public function __destruct() |
|
67 | |||
68 | /** |
||
69 | * @param string $msg |
||
70 | * @param array $context |
||
71 | * @return bool |
||
72 | */ |
||
73 | 1 | public function defaultLog($msg = '', array $context = []) |
|
77 | |||
78 | /** |
||
79 | * @param string $msg |
||
80 | * @param array $context |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | 4 | public function infoLog($msg = '', array $context = []) |
|
88 | |||
89 | /** |
||
90 | * @param string $msg |
||
91 | * @param array $context |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | 21 | public function debugLog($msg = '', array $context = []) |
|
99 | |||
100 | /** |
||
101 | * @param $msg |
||
102 | * @param array $context |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | 4 | public function errorLog($msg, array $context = []) |
|
110 | |||
111 | /** |
||
112 | * @param $msg |
||
113 | * @param array $context |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 1 | public function criticalLog($msg, array $context = []) |
|
124 | |||
125 | /** |
||
126 | * @param $msg |
||
127 | * @param array $context |
||
128 | * @return bool |
||
129 | */ |
||
130 | 2 | public function warningLog($msg, array $context = []) |
|
134 | |||
135 | /** |
||
136 | * @param string $logger |
||
137 | * @param boolean $debug |
||
138 | * @param Config $config |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | 2 | private function addPushLogger($logger, $debug, Config $config) |
|
158 | |||
159 | /** |
||
160 | * @param Config $config |
||
161 | * @param array $args |
||
162 | * @return array |
||
163 | * @throws exception\GeneratorException |
||
164 | */ |
||
165 | 2 | private function setup(Config $config, array $args = array()) |
|
180 | |||
181 | /** |
||
182 | * @param Config $config |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 2 | private function setLoggerName(Config $config) |
|
193 | |||
194 | /** |
||
195 | * @param $logger |
||
196 | * |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 2 | private function cleanLoggerName($logger) |
|
206 | |||
207 | /** |
||
208 | * @param Config $config |
||
209 | * @return string |
||
210 | * @throws exception\GeneratorException |
||
211 | */ |
||
212 | 2 | private function createLoggerPath(Config $config) |
|
220 | |||
221 | /** |
||
222 | * @param string $msg |
||
223 | * @param int $type |
||
224 | * @param array $context |
||
225 | */ |
||
226 | 22 | public static function log($msg, $type = LOG_DEBUG, array $context = null) |
|
255 | |||
256 | /** |
||
257 | * @param bool $debug |
||
258 | * @return StreamHandler |
||
259 | * @throws \Exception |
||
260 | */ |
||
261 | 2 | private function addDefaultStreamHandler($debug = false) |
|
273 | |||
274 | /** |
||
275 | * @param array $context |
||
276 | * @return array |
||
277 | */ |
||
278 | 7 | private function addMinimalContext(array $context = []) |
|
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getLogUid() { |
||
294 | |||
295 | /** |
||
296 | * @return string |
||
297 | */ |
||
298 | public static function getUid() { |
||
301 | } |
||
302 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.