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 |
||
10 | class ConsoleOutputter implements IOutputter, ILazyWriter |
||
11 | { |
||
12 | /** |
||
13 | * https://github.com/php/php-src/tree/master/sapi |
||
14 | * PHP7以前のものは対応しない |
||
15 | * @var SAPIリスト |
||
16 | */ |
||
17 | private $sapis = [ |
||
18 | 'apache2handler' => 'http', |
||
19 | 'cgi' => 'http', |
||
20 | 'cli' => 'console', |
||
21 | 'fpm' => 'http', |
||
22 | 'embed' => 'unsupported', |
||
23 | 'litespeed' => 'unsupported', |
||
24 | 'phpdbg' => 'unsupported', |
||
25 | 'tests' => 'unsupported' |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var array<string> ログメッセージリスト |
||
30 | */ |
||
31 | private $logMessages; |
||
32 | |||
33 | /** |
||
34 | * @var int バッファリングサイズ |
||
35 | */ |
||
36 | private $bufferSize; |
||
37 | |||
38 | /** |
||
39 | * @var bool 遅延書き出しフラグ |
||
40 | */ |
||
41 | private $isLazyWrite; |
||
42 | |||
43 | /** |
||
44 | * constructor |
||
45 | */ |
||
46 | public function __construct($bufferSize = 1000) |
||
52 | |||
53 | /** |
||
54 | * destructor |
||
55 | */ |
||
56 | public function __destruct() |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function enableLazyWrite() |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function enableDirectWrite() |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | View Code Duplication | public function write($message) |
|
93 | |||
94 | /** |
||
95 | * バッファをクリアする |
||
96 | */ |
||
97 | private function clear() |
||
101 | |||
102 | /** |
||
103 | * バッファをログ出力する |
||
104 | */ |
||
105 | View Code Duplication | private function flush() |
|
112 | |||
113 | /** |
||
114 | * ログファイルに書き出す |
||
115 | * @param string $message ログメッセージ |
||
116 | */ |
||
117 | private function writeLog($message) |
||
124 | } |
||
125 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.