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 |
||
| 15 | class Logger |
||
| 16 | { |
||
| 17 | use LoggerUtils; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Logger ロガー |
||
| 21 | */ |
||
| 22 | private static $logger; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var LoggerFormatter ロガーフォーマッタ |
||
| 26 | */ |
||
| 27 | private static $formatter; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Container ログ設定コンテナ |
||
| 31 | */ |
||
| 32 | private static $config; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Container ログ設定コンテナ |
||
| 36 | */ |
||
| 37 | private $logConfig; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array<IOoutputter> Outputterリスト |
||
| 41 | */ |
||
| 42 | private $outputters; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * コンストラクタ |
||
| 46 | * @param Container ログ設定コンテナ |
||
| 47 | */ |
||
| 48 | private function __construct(Container $logConfig) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * ログ設定を返却する |
||
| 56 | * @return Container ログ設定 |
||
| 57 | */ |
||
| 58 | public function getConfig() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * デストラクタ |
||
| 65 | */ |
||
| 66 | public function __destruct() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * 遅延書き出しを有効にする |
||
| 73 | */ |
||
| 74 | public static function enableLazyWrite() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * 即時書き出しを有効にする |
||
| 81 | */ |
||
| 82 | public static function enableDirectWrite() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * インスタンスを返却する |
||
| 89 | * @return WebStream\Module\Logger ロガーインスタンス |
||
| 90 | */ |
||
| 91 | public static function getInstance() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Loggerを初期化する |
||
| 98 | * @param Container ログ設定コンテナ |
||
| 99 | */ |
||
| 100 | public static function init(Container $config) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Loggerを終了する |
||
| 109 | */ |
||
| 110 | public static function finalize() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Loggerが初期化済みかどうかチェックする |
||
| 119 | * @param bool 初期化済みならtrue |
||
| 120 | */ |
||
| 121 | public static function isInitialized() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Loggerメソッドの呼び出しを受ける |
||
| 128 | * @param string メソッド名(ログレベル文字列) |
||
| 129 | * @param array 引数 |
||
| 130 | */ |
||
| 131 | public static function __callStatic($level, $arguments) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Outputterを設定する |
||
| 146 | * @param array<IOutputter> $outputters Outputterリスト |
||
| 147 | */ |
||
| 148 | public function setOutputter(array $outputters) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * タイムスタンプを取得する |
||
| 160 | * @return string タイムスタンプ |
||
| 161 | */ |
||
| 162 | private function getTimeStamp() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * ログメッセージにスタックトレースの内容を追加する |
||
| 172 | * @param string ログメッセージ |
||
| 173 | * @param string スタックトレース文字列 |
||
| 174 | * @return string 加工済みログメッセージ |
||
| 175 | */ |
||
| 176 | private function message($msg, $stacktrace = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * ログを書き出す |
||
| 193 | * @param string ログレベル文字列 |
||
| 194 | * @param string 出力文字列 |
||
| 195 | * @param array<mixed> 埋め込み値リスト |
||
| 196 | */ |
||
| 197 | public function write($level, $msg, $context = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * ログステータスファイルに書きこむ |
||
| 235 | */ |
||
| 236 | private function writeStatus() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * ログステータスファイルを読み込む |
||
| 243 | */ |
||
| 244 | private function readStatus() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * ステータスファイルを作成する |
||
| 259 | */ |
||
| 260 | private function createstatusPath() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * ログファイルをアーカイブする |
||
| 270 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 271 | * @param string ログファイルパス |
||
| 272 | */ |
||
| 273 | private function rotate() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * ローテートを実行する |
||
| 289 | * @param integer 作成日時のUnixTime |
||
| 290 | * @param integer 現在日時のUnixTime |
||
| 291 | */ |
||
| 292 | private function runRotate($from, $to) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * 時間単位でローテートする |
||
| 308 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 309 | */ |
||
| 310 | View Code Duplication | private function rotateByCycle() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * サイズ単位でローテートする |
||
| 325 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 326 | */ |
||
| 327 | View Code Duplication | private function rotateBySize() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * ログ出力パスを返却する |
||
| 342 | * @return string ログ出力パス |
||
| 343 | */ |
||
| 344 | public function getLogPath() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * ログローテートサイクルを返却する |
||
| 351 | * @return string ログ出力パス |
||
| 352 | */ |
||
| 353 | public function getLogRotateCycle() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * ログローテートサイズを返却する |
||
| 360 | * @return string ログ出力パス |
||
| 361 | */ |
||
| 362 | public function getLogRotateSize() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * 遅延書き出しを有効にする |
||
| 369 | */ |
||
| 370 | public function lazyWrite() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * 即時書き出しを有効にする |
||
| 381 | */ |
||
| 382 | public function directWrite() |
||
| 390 | } |
||
| 391 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..