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 FileInputStream extends InputStream |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var File ファイルオブジェクト |
||
| 17 | */ |
||
| 18 | protected $file; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * constructor |
||
| 22 | * @param mixed $file ファイルオブジェクトまたはファイルパス |
||
| 23 | * @throws InvalidArgumentException |
||
| 24 | * @throws IOException |
||
| 25 | */ |
||
| 26 | public function __construct($file) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * 入力ストリームを閉じる |
||
| 45 | */ |
||
| 46 | public function close() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function read($length = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 入力ストリームから行単位でデータを読み込む |
||
| 87 | * 末尾に改行コードは含まない |
||
| 88 | * @return string 読み込みデータ |
||
| 89 | */ |
||
| 90 | public function readLine() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function skip(int $pos) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function reset() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function eof() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function isMarkSupported() |
||
| 160 | } |
||
| 161 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.