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 |
||
| 23 | View Code Duplication | class AnswersOfIdsHandler |
|
|
|
|||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The data transformer. |
||
| 27 | * |
||
| 28 | * @var ResponseAnswerDataTransformer |
||
| 29 | */ |
||
| 30 | private $dataTransformer; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The HTTP domain class. |
||
| 34 | * |
||
| 35 | * @var Http |
||
| 36 | */ |
||
| 37 | private $http; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The authentication, it is not mandatory. |
||
| 41 | * |
||
| 42 | * @var Authentication|null |
||
| 43 | */ |
||
| 44 | private $authentication; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor. |
||
| 48 | * |
||
| 49 | * @param Http $http The HTTP domain class |
||
| 50 | * @param ResponseAnswerDataTransformer $dataTransformer The answer data transformer |
||
| 51 | * @param Authentication|null $anAuthentication The authentication, it is not mandatory |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get answers identified by a set of ids. |
||
| 65 | * |
||
| 66 | * More info: http://api.stackexchange.com/docs/answers-by-ids |
||
| 67 | * |
||
| 68 | * @param AnswersOfIdsCommand $command The command |
||
| 69 | * |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function handle(AnswersOfIdsCommand $command) |
||
| 84 | } |
||
| 85 |
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.