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 |
||
20 | class Logger |
||
21 | { |
||
22 | use Injector, LoggerUtils; |
||
23 | |||
24 | /** |
||
25 | * @var Logger ロガー |
||
26 | */ |
||
27 | private static $logger; |
||
28 | |||
29 | /** |
||
30 | * @var LoggerFormatter ロガーフォーマッタ |
||
31 | */ |
||
32 | private static $formatter; |
||
33 | |||
34 | /** |
||
35 | * @var Container ログ設定コンテナ |
||
36 | */ |
||
37 | private static $config; |
||
38 | |||
39 | /** |
||
40 | * @var Container ログ設定コンテナ |
||
41 | */ |
||
42 | private $logConfig; |
||
43 | |||
44 | /** |
||
45 | * @var array<IOutputter> Outputterリスト |
||
46 | */ |
||
47 | private $outputters; |
||
48 | |||
49 | /** |
||
50 | * @var Container IOコンテナ |
||
51 | */ |
||
52 | private $ioContainer; |
||
53 | |||
54 | /** |
||
55 | * @var File ログファイル |
||
56 | */ |
||
57 | private $logFile; |
||
58 | |||
59 | /** |
||
60 | * @var File ステータスファイル |
||
61 | */ |
||
62 | private $statusFile; |
||
63 | |||
64 | /** |
||
65 | * コンストラクタ |
||
66 | * @param Container ログ設定コンテナ |
||
67 | */ |
||
68 | private function __construct(Container $logConfig) |
||
91 | |||
92 | /** |
||
93 | * デストラクタ |
||
94 | */ |
||
95 | public function __destruct() |
||
99 | |||
100 | /** |
||
101 | * ログ設定を返却する |
||
102 | * @return Container ログ設定 |
||
103 | */ |
||
104 | public function getConfig() |
||
108 | |||
109 | /** |
||
110 | * 遅延書き出しを有効にする |
||
111 | */ |
||
112 | public static function enableLazyWrite() |
||
116 | |||
117 | /** |
||
118 | * 即時書き出しを有効にする |
||
119 | */ |
||
120 | public static function enableDirectWrite() |
||
124 | |||
125 | /** |
||
126 | * インスタンスを返却する |
||
127 | * @return WebStream\Module\Logger ロガーインスタンス |
||
128 | */ |
||
129 | public static function getInstance() |
||
133 | |||
134 | /** |
||
135 | * Loggerを初期化する |
||
136 | * @param Container ログ設定コンテナ |
||
137 | */ |
||
138 | public static function init(Container $config) |
||
144 | |||
145 | /** |
||
146 | * Loggerを終了する |
||
147 | */ |
||
148 | public static function finalize() |
||
154 | |||
155 | /** |
||
156 | * Loggerが初期化済みかどうかチェックする |
||
157 | * @param bool 初期化済みならtrue |
||
158 | */ |
||
159 | public static function isInitialized() |
||
163 | |||
164 | /** |
||
165 | * Loggerメソッドの呼び出しを受ける |
||
166 | * @param string メソッド名(ログレベル文字列) |
||
167 | * @param array 引数 |
||
168 | */ |
||
169 | public static function __callStatic($level, $arguments) |
||
181 | |||
182 | /** |
||
183 | * Outputterを設定する |
||
184 | * @param array<IOutputter> $outputters Outputterリスト |
||
185 | */ |
||
186 | public function setOutputter(array $outputters) |
||
195 | |||
196 | /** |
||
197 | * タイムスタンプを取得する |
||
198 | * @return string タイムスタンプ |
||
199 | */ |
||
200 | private function getTimeStamp() |
||
207 | |||
208 | /** |
||
209 | * ログを書き出す |
||
210 | * @param string ログレベル文字列 |
||
211 | * @param string 出力文字列 |
||
212 | * @param array<mixed> 埋め込み値リスト |
||
213 | */ |
||
214 | public function write($level, $msg, $context = null) |
||
251 | |||
252 | /** |
||
253 | * ログファイルをアーカイブする |
||
254 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
255 | * @param string ログファイルパス |
||
256 | */ |
||
257 | private function rotate() |
||
271 | |||
272 | /** |
||
273 | * ログステータスファイルに書きこむ |
||
274 | * @throws IOException |
||
275 | */ |
||
276 | private function writeStatus() |
||
280 | |||
281 | /** |
||
282 | * ログステータスファイルを読み込む |
||
283 | * @return int UnixTime |
||
284 | * @throws LoggerException |
||
285 | */ |
||
286 | private function readStatus() |
||
295 | |||
296 | /** |
||
297 | * ステータスファイルを作成する |
||
298 | */ |
||
299 | private function createStatusFile() |
||
306 | |||
307 | /** |
||
308 | * ローテートを実行する |
||
309 | * @param integer 作成日時のUnixTime |
||
310 | * @param integer 現在日時のUnixTime |
||
311 | */ |
||
312 | private function runRotate($from, $to) |
||
325 | |||
326 | /** |
||
327 | * 時間単位でローテートする |
||
328 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
329 | */ |
||
330 | private function rotateByCycle() |
||
341 | |||
342 | /** |
||
343 | * サイズ単位でローテートする |
||
344 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
345 | */ |
||
346 | private function rotateBySize() |
||
358 | |||
359 | /** |
||
360 | * ログ出力パスを返却する |
||
361 | * @return string ログ出力パス |
||
362 | */ |
||
363 | public function getLogPath() |
||
367 | |||
368 | /** |
||
369 | * ログローテートサイクルを返却する |
||
370 | * @return string ログ出力パス |
||
371 | */ |
||
372 | public function getLogRotateCycle() |
||
376 | |||
377 | /** |
||
378 | * ログローテートサイズを返却する |
||
379 | * @return string ログ出力パス |
||
380 | */ |
||
381 | public function getLogRotateSize() |
||
385 | |||
386 | /** |
||
387 | * 遅延書き出しを有効にする |
||
388 | */ |
||
389 | public function lazyWrite() |
||
397 | |||
398 | /** |
||
399 | * 即時書き出しを有効にする |
||
400 | */ |
||
401 | public function directWrite() |
||
409 | } |
||
410 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.