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 |
||
| 6 | View Code Duplication | class xKerman_Restricted_Source |
|
| 7 | { |
||
| 8 | /** @var string $str given string to deserialize */ |
||
| 9 | private $str; |
||
| 10 | /** @var int $length given string length */ |
||
| 11 | private $length; |
||
| 12 | /** @var int $current current position of parser */ |
||
| 13 | private $current; |
||
| 14 | /** |
||
| 15 | * constructor |
||
| 16 | * |
||
| 17 | * @param string $str parser input |
||
| 18 | * @throws InvalidArgumentException |
||
| 19 | */ |
||
| 20 | public function __construct($str) |
||
| 29 | /** |
||
| 30 | * throw error with currnt position |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | * @throws UnserializeFailedException |
||
| 34 | */ |
||
| 35 | public function triggerError() |
||
| 40 | /** |
||
| 41 | * consume given string if it is as expected |
||
| 42 | * |
||
| 43 | * @param string $expected expected string |
||
| 44 | * @param integer $length length of $expected |
||
| 45 | * @return void |
||
| 46 | * @throws UnserializeFailedException |
||
| 47 | */ |
||
| 48 | public function consume($expected, $length) |
||
| 55 | /** |
||
| 56 | * read givin length substring |
||
| 57 | * |
||
| 58 | * @param integer $length length to read |
||
| 59 | * @return string |
||
| 60 | * @throws UnserializeFailedException |
||
| 61 | */ |
||
| 62 | public function read($length) |
||
| 73 | /** |
||
| 74 | * return matching string for given regexp |
||
| 75 | * |
||
| 76 | * @param string $regexp Regular Expression for expected substring |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function match($regexp) |
||
| 88 | } |
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.