Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
14 | class Logger |
||
15 | { |
||
16 | use LoggerUtils; |
||
17 | |||
18 | /** |
||
19 | * @var Logger ロガー |
||
20 | */ |
||
21 | private static $logger; |
||
22 | |||
23 | /** |
||
24 | * @var LoggerFormatter ロガーフォーマッタ |
||
25 | */ |
||
26 | private static $formatter; |
||
27 | |||
28 | /** |
||
29 | * @var Container ログ設定コンテナ |
||
30 | */ |
||
31 | private static $config; |
||
32 | |||
33 | /** |
||
34 | * @var Container ログ設定コンテナ |
||
35 | */ |
||
36 | private $logConfig; |
||
37 | |||
38 | /** |
||
39 | * @var array<IOutputter> Outputterリスト |
||
40 | */ |
||
41 | private $outputters; |
||
42 | |||
43 | /** |
||
44 | * コンストラクタ |
||
45 | * @param Container ログ設定コンテナ |
||
46 | */ |
||
47 | private function __construct(Container $logConfig) |
||
52 | |||
53 | /** |
||
54 | * ログ設定を返却する |
||
55 | * @return Container ログ設定 |
||
56 | */ |
||
57 | public function getConfig() |
||
61 | |||
62 | /** |
||
63 | * デストラクタ |
||
64 | */ |
||
65 | public function __destruct() |
||
69 | |||
70 | /** |
||
71 | * 遅延書き出しを有効にする |
||
72 | */ |
||
73 | public static function enableLazyWrite() |
||
77 | |||
78 | /** |
||
79 | * 即時書き出しを有効にする |
||
80 | */ |
||
81 | public static function enableDirectWrite() |
||
85 | |||
86 | /** |
||
87 | * インスタンスを返却する |
||
88 | * @return WebStream\Module\Logger ロガーインスタンス |
||
89 | */ |
||
90 | public static function getInstance() |
||
94 | |||
95 | /** |
||
96 | * Loggerを初期化する |
||
97 | * @param Container ログ設定コンテナ |
||
98 | */ |
||
99 | public static function init(Container $config) |
||
105 | |||
106 | /** |
||
107 | * Loggerを終了する |
||
108 | */ |
||
109 | public static function finalize() |
||
115 | |||
116 | /** |
||
117 | * Loggerが初期化済みかどうかチェックする |
||
118 | * @param bool 初期化済みならtrue |
||
119 | */ |
||
120 | public static function isInitialized() |
||
124 | |||
125 | /** |
||
126 | * Loggerメソッドの呼び出しを受ける |
||
127 | * @param string メソッド名(ログレベル文字列) |
||
128 | * @param array 引数 |
||
129 | */ |
||
130 | public static function __callStatic($level, $arguments) |
||
142 | |||
143 | /** |
||
144 | * Outputterを設定する |
||
145 | * @param array<IOutputter> $outputters Outputterリスト |
||
146 | */ |
||
147 | public function setOutputter(array $outputters) |
||
156 | |||
157 | /** |
||
158 | * タイムスタンプを取得する |
||
159 | * @return string タイムスタンプ |
||
160 | */ |
||
161 | private function getTimeStamp() |
||
168 | |||
169 | /** |
||
170 | * ログを書き出す |
||
171 | * @param string ログレベル文字列 |
||
172 | * @param string 出力文字列 |
||
173 | * @param array<mixed> 埋め込み値リスト |
||
174 | */ |
||
175 | public function write($level, $msg, $context = null) |
||
210 | |||
211 | /** |
||
212 | * ログステータスファイルに書きこむ |
||
213 | */ |
||
214 | private function writeStatus() |
||
218 | |||
219 | /** |
||
220 | * ログステータスファイルを読み込む |
||
221 | * @return int UnixTime |
||
222 | */ |
||
223 | private function readStatus() |
||
235 | |||
236 | /** |
||
237 | * ステータスファイルを作成する |
||
238 | */ |
||
239 | private function createstatusPath() |
||
246 | |||
247 | /** |
||
248 | * ログファイルをアーカイブする |
||
249 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
250 | * @param string ログファイルパス |
||
251 | */ |
||
252 | private function rotate() |
||
265 | |||
266 | /** |
||
267 | * ローテートを実行する |
||
268 | * @param integer 作成日時のUnixTime |
||
269 | * @param integer 現在日時のUnixTime |
||
270 | */ |
||
271 | private function runRotate($from, $to) |
||
284 | |||
285 | /** |
||
286 | * 時間単位でローテートする |
||
287 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
288 | */ |
||
289 | View Code Duplication | private function rotateByCycle() |
|
301 | |||
302 | /** |
||
303 | * サイズ単位でローテートする |
||
304 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
305 | */ |
||
306 | View Code Duplication | private function rotateBySize() |
|
318 | |||
319 | /** |
||
320 | * ログ出力パスを返却する |
||
321 | * @return string ログ出力パス |
||
322 | */ |
||
323 | public function getLogPath() |
||
327 | |||
328 | /** |
||
329 | * ログローテートサイクルを返却する |
||
330 | * @return string ログ出力パス |
||
331 | */ |
||
332 | public function getLogRotateCycle() |
||
336 | |||
337 | /** |
||
338 | * ログローテートサイズを返却する |
||
339 | * @return string ログ出力パス |
||
340 | */ |
||
341 | public function getLogRotateSize() |
||
345 | |||
346 | /** |
||
347 | * 遅延書き出しを有効にする |
||
348 | */ |
||
349 | public function lazyWrite() |
||
357 | |||
358 | /** |
||
359 | * 即時書き出しを有効にする |
||
360 | */ |
||
361 | public function directWrite() |
||
369 | } |
||
370 |