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 $logLevel; |
||
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 $logger |
||
70 | * @param boolean $debug |
||
71 | * @param Config $config |
||
72 | * @throws \Exception |
||
73 | */ |
||
74 | 2 | private function addPushLogger($logger, $debug, Config $config) |
|
75 | { |
||
76 | 2 | $this->logger = new Monolog(strtoupper($logger)); |
|
77 | 2 | $this->logger->pushHandler($this->addDefaultStreamHandler($debug)); |
|
78 | 2 | if ($debug) { |
|
79 | 2 | $phpFireLog = $config->get('logger.phpFire'); |
|
80 | 2 | if (!empty($phpFireLog)) { |
|
81 | 1 | $this->logger->pushHandler(new FirePHPHandler()); |
|
82 | } |
||
83 | 2 | $memoryLog = $config->get('logger.memory'); |
|
84 | 2 | if (!empty($memoryLog)) { |
|
85 | 1 | $this->logger->pushProcessor(new MemoryUsageProcessor()); |
|
86 | } |
||
87 | } |
||
88 | 2 | $this->uuid = new UidProcessor(); |
|
89 | 2 | $this->logger->pushProcessor($this->uuid); |
|
90 | 2 | } |
|
91 | |||
92 | /** |
||
93 | * @param Config $config |
||
94 | * @param array $args |
||
95 | * @return array |
||
96 | * @throws exception\GeneratorException |
||
97 | */ |
||
98 | 2 | private function setup(Config $config, array $args = []) |
|
110 | |||
111 | /** |
||
112 | * @param Config $config |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 2 | private function setLoggerName(Config $config) |
|
123 | |||
124 | /** |
||
125 | * @param $logger |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | 2 | private function cleanLoggerName($logger) |
|
136 | |||
137 | /** |
||
138 | * @param Config $config |
||
139 | * @return string |
||
140 | * @throws exception\GeneratorException |
||
141 | */ |
||
142 | 2 | private function createLoggerPath(Config $config) |
|
150 | |||
151 | /** |
||
152 | * @param string $msg |
||
153 | * @param int $type |
||
154 | * @param array $context |
||
155 | * @return bool |
||
156 | */ |
||
157 | 22 | public function addLog($msg, $type = \Monolog\Logger::NOTICE, array $context = []) { |
|
160 | |||
161 | /** |
||
162 | * @param int $level |
||
163 | * @return bool |
||
164 | */ |
||
165 | 22 | private function checkLogLevel($level = \Monolog\Logger::NOTICE) { |
|
177 | |||
178 | /** |
||
179 | * @param string $msg |
||
180 | * @param int $type |
||
181 | * @param array $context |
||
|
|||
182 | */ |
||
183 | 22 | public static function log($msg, $type = LOG_DEBUG, array $context = null) |
|
215 | |||
216 | /** |
||
217 | * @param bool $debug |
||
218 | * @return StreamHandler |
||
219 | * @throws \Exception |
||
220 | */ |
||
221 | 2 | private function addDefaultStreamHandler($debug = false) |
|
233 | |||
234 | /** |
||
235 | * @param array $context |
||
236 | * @return array |
||
237 | */ |
||
238 | 5 | private function addMinimalContext(array $context = []) |
|
247 | |||
248 | /** |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getLogUid() { |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | public static function getUid() { |
||
261 | } |
||
262 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.