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 |
||
| 18 | View Code Duplication | class OrX extends MongoCriteria { |
|
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * @var MongoCriteria |
||
| 22 | */ |
||
| 23 | private $criteria1; |
||
| 24 | /** |
||
| 25 | * @var MongoCriteria |
||
| 26 | */ |
||
| 27 | private $criteria2; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Initializes an OR operator where one of the given criteria must evaluate to <code>true</code>. |
||
| 31 | * |
||
| 32 | * @param MongoCriteria $criteria1 One of the criteria that must evaluate to true |
||
| 33 | * @param MongoCriteria $criteria2 One of the criteria that must evaluate to true |
||
| 34 | */ |
||
| 35 | 1 | public function __construct(MongoCriteria $criteria1, MongoCriteria $criteria2) { |
|
| 39 | |||
| 40 | |||
| 41 | 1 | public function asMongoObject() { |
|
| 49 | } |
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.