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 |
||
15 | class SimpleFileWriter extends OutputStreamWriter |
||
16 | { |
||
17 | /** |
||
18 | * @var string ファイルパス |
||
19 | */ |
||
20 | private $filepath; |
||
21 | |||
22 | /** |
||
23 | * @var int バッファリングサイズ |
||
24 | */ |
||
25 | private $bufferSize; |
||
26 | |||
27 | /** |
||
28 | * @var string 書き込みモード |
||
29 | */ |
||
30 | private $mode; |
||
31 | |||
32 | /** |
||
33 | * constructor |
||
34 | * @param string $filepath ファイルパス |
||
35 | * @param int $bufferSize バッファリングサイズ |
||
36 | */ |
||
37 | public function __construct($filepath, $bufferSize = null) |
||
49 | |||
50 | /** |
||
51 | * ファイルに書き込む |
||
52 | * ファイルが存在する場合、常に追記モード |
||
53 | * @param mixed $data 書き込みデータ |
||
54 | */ |
||
55 | public function write($data) |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function close() |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function flush() |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function newLine() |
||
105 | } |
||
106 |
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.