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 |
||
| 29 | class UsrAuthFactory |
||
| 30 | { |
||
| 31 | protected $cookie_definitions = [ |
||
| 32 | 'id' => [ |
||
| 33 | 'filter' => FILTER_VALIDATE_INT, |
||
| 34 | 'options' => [ |
||
| 35 | 'min_range' => 1, |
||
| 36 | 'max_range' => PHP_INT_MAX |
||
| 37 | ] |
||
| 38 | ], |
||
| 39 | 'pw' => [ |
||
| 40 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 41 | 'options' => [ |
||
| 42 | 'regexp' => '/^.{31}$/' |
||
| 43 | ] |
||
| 44 | ] |
||
| 45 | ]; |
||
| 46 | protected $post_definitions = [ |
||
| 47 | 'ln' => [ |
||
| 48 | 'filter' => FILTER_VALIDATE_EMAIL, |
||
| 49 | ], |
||
| 50 | 'pw' => [ |
||
| 51 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 52 | 'options' => [ |
||
| 53 | 'regexp' => '/^[a-z0-9_-]{1,20}$/i' |
||
| 54 | ] |
||
| 55 | ] |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Algorithm for choosing auth strategy |
||
| 60 | * |
||
| 61 | * @todo Move validation and error handling into separate class |
||
| 62 | * @return AbstractAuthStrategy |
||
| 63 | */ |
||
| 64 | 1 | public function create() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @codeCoverageIgnore |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | protected function getCookies() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @codeCoverageIgnore |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | protected function getPost() |
||
| 113 | } |
||
| 114 |
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.