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:
1 | <?php |
||
14 | class LoggerFormatter |
||
15 | { |
||
16 | use LoggerUtils; |
||
17 | |||
18 | /** |
||
19 | * @var Container ログ設定コンテナ |
||
20 | */ |
||
21 | private $logConfig; |
||
22 | |||
23 | /** |
||
24 | * コンストラクタ |
||
25 | * @param string 設定ファイルパス |
||
26 | */ |
||
27 | public function __construct(Container $logConfig) |
||
33 | |||
34 | /** |
||
35 | * フォーマット済みメッセージを返却する |
||
36 | * @param string メッセージ |
||
37 | * @param string ログレベル |
||
38 | * @return フォーマット済みメッセージ |
||
39 | */ |
||
40 | public function getFormattedMessage($message, $logLevel) |
||
55 | |||
56 | /** |
||
57 | * 固定の項目を埋め込む |
||
58 | */ |
||
59 | private function compile() |
||
63 | |||
64 | /** |
||
65 | * アプリケーション名項目を埋め込む |
||
66 | * @param string メッセージ |
||
67 | * @param string アプリケーション名 |
||
68 | * @return 埋め込み済みメッセージ |
||
69 | */ |
||
70 | private function compileApplicationName($message, $applicationName) |
||
80 | |||
81 | /** |
||
82 | * 日付項目を埋め込む |
||
83 | * @param string メッセージ |
||
84 | * @return 埋め込み済みメッセージ |
||
85 | */ |
||
86 | private function compileDateTime($message) |
||
108 | |||
109 | /** |
||
110 | * ログレベル項目を埋め込む |
||
111 | * @param string メッセージ |
||
112 | * @param string ログレベル |
||
113 | * @return 埋め込み済みメッセージ |
||
114 | */ |
||
115 | private function compileLogLevel($message, $logLevel) |
||
129 | } |
||
130 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.