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 |
||
13 | class InputStream |
||
14 | { |
||
15 | /** |
||
16 | * @var resource 入力ストリーム |
||
17 | */ |
||
18 | protected $stream; |
||
19 | |||
20 | /** |
||
21 | * @var int 現在のポインタ位置 |
||
22 | */ |
||
23 | protected $cursorPosition; |
||
24 | |||
25 | /** |
||
26 | * @var int markしたポインタ位置 |
||
27 | */ |
||
28 | protected $markedPosition; |
||
29 | |||
30 | /** |
||
31 | * constructor |
||
32 | * @param resource $stream 入力ストリーム |
||
33 | */ |
||
34 | public function __construct($stream) |
||
44 | |||
45 | /** |
||
46 | * destructor |
||
47 | */ |
||
48 | public function __destruct() |
||
52 | |||
53 | /** |
||
54 | * 入力ストリームを閉じる |
||
55 | */ |
||
56 | public function close() |
||
68 | |||
69 | /** |
||
70 | * 入力ストリームからデータを読み込む |
||
71 | * 引数に数値を指定した場合、指定数値バイトだけ読み込む |
||
72 | * @param int length 読み込みバイト数 |
||
73 | * @return string 読み込みデータ |
||
74 | * @throws InvalidArgumentException |
||
75 | */ |
||
76 | public function read($length = null) |
||
101 | |||
102 | /** |
||
103 | * 入力ストリームから指定バイト数後方へポインタを移動する |
||
104 | * @param int $pos 後方への移動バイト数(負数の場合は前方へ移動) |
||
105 | * @return int $skipNum 移動したバイト数、移動に失敗した場合-1 |
||
106 | */ |
||
107 | public function skip(int $pos) |
||
129 | |||
130 | /** |
||
131 | * 入力ストリームの現在位置にmarkを設定する |
||
132 | * @throws IOException |
||
133 | */ |
||
134 | public function mark() |
||
142 | |||
143 | /** |
||
144 | * 最後にmarkされた位置に再配置する |
||
145 | * @throws IOException |
||
146 | */ |
||
147 | public function reset() |
||
158 | |||
159 | /** |
||
160 | * EOFかどうか返却する |
||
161 | * @return bool EOFならtrue |
||
162 | */ |
||
163 | public function eof() |
||
167 | |||
168 | /** |
||
169 | * mark機能をサポートしているかどうか |
||
170 | * @return boolean マークをサポートしていればtrue |
||
171 | */ |
||
172 | public function isMarkSupported() |
||
176 | } |
||
177 |
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.