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 StringInputStream extends InputStream |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var int 文字列長 |
||
| 14 | */ |
||
| 15 | private $length; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * construct |
||
| 19 | * @param string $str 文字列 |
||
| 20 | */ |
||
| 21 | public function __construct($str) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * 入力ストリームを閉じる |
||
| 29 | */ |
||
| 30 | public function close() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritdoc} |
||
| 37 | */ |
||
| 38 | public function read($length = null) |
||
| 61 | |||
| 62 | public function readLine() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | public function skip(int $pos) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function eof() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function isMarkSupported() |
||
| 124 | } |
||
| 125 |
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.