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 |
||
16 | class FileOutputter implements IOutputter, ILazyWriter |
||
17 | { |
||
18 | use LoggerUtils, CacheUtils; |
||
19 | |||
20 | /** |
||
21 | * @var string ログファイルパス |
||
22 | */ |
||
23 | private $logPath; |
||
24 | |||
25 | /** |
||
26 | * @var ICache キャッシュドライバ |
||
27 | */ |
||
28 | private $driver; |
||
29 | |||
30 | /** |
||
31 | * @var int バッファリングサイズ |
||
32 | */ |
||
33 | private $bufferSize; |
||
34 | |||
35 | /** |
||
36 | * @var bool 遅延書き出しフラグ |
||
37 | */ |
||
38 | private $isLazyWrite; |
||
39 | |||
40 | /** |
||
41 | * @var SimpleFileWriter Writerオブジェクト |
||
42 | */ |
||
43 | private $writer; |
||
44 | |||
45 | /** |
||
46 | * @var LoggerCache ログキャッシュ |
||
47 | */ |
||
48 | private $cache; |
||
49 | |||
50 | /** |
||
51 | * constructor |
||
52 | * @param string $logPath ログファイルパス |
||
53 | * @param int $bufferSize バッファリングサイズ |
||
54 | */ |
||
55 | public function __construct(string $logPath, int $bufferSize = 1000) |
||
56 | { |
||
57 | $this->logPath = $logPath; |
||
58 | $driver = $this->getCacheDriver("apcu"); |
||
59 | // LoggerCacheの中ではログは取らない |
||
60 | $driver->inject('logger', new class() { function __call($name, $args) {} }); |
||
66 | |||
67 | /** |
||
68 | * destructor |
||
69 | */ |
||
70 | public function __destruct() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function enableLazyWrite() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function enableDirectWrite() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | View Code Duplication | public function write($message) |
|
120 | |||
121 | /** |
||
122 | * バッファをログ出力する |
||
123 | */ |
||
124 | private function flush() |
||
130 | |||
131 | /** |
||
132 | * ログファイルに書き出す |
||
133 | * @param string $message ログメッセージ |
||
134 | */ |
||
135 | private function writeLog($message) |
||
139 | } |
||
140 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.