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 |
||
| 9 | class ODMLuceneQuery extends LuceneQuery |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var DocumentManager |
||
| 13 | */ |
||
| 14 | private $dm; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | private $onlyDocs = false; |
||
| 20 | |||
| 21 | private $documentName; |
||
| 22 | |||
| 23 | public function getDocumentName() |
||
| 27 | |||
| 28 | public function setDocumentName($documentName) |
||
| 33 | |||
| 34 | public function execute() |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * @param DocumentManager $dm |
||
| 64 | */ |
||
| 65 | public function setDocumentManager(DocumentManager $dm) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param bool $flag |
||
| 72 | * @return LuceneQuery |
||
| 73 | */ |
||
| 74 | public function onlyDocs($flag) |
||
| 80 | } |
||
| 81 |
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.