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 |
||
27 | class DeleteQuery extends AbstractEntityQuery implements DeleteQueryInterface |
||
28 | { |
||
29 | const EVENT_BEFORE = 'delete.before'; |
||
30 | const EVENT_AFTER = 'delete.after'; |
||
31 | |||
32 | use GetTypeTrait; |
||
33 | |||
34 | protected $instance; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param Connection $connection |
||
40 | * @param mixed $entity |
||
41 | * @param ModelInterface $model |
||
42 | * @param RelationFactoryInterface $factory |
||
43 | * @param AccessorInterface $accessor |
||
44 | * @param EventDispatcherInterface $dispatcher |
||
45 | */ |
||
46 | View Code Duplication | public function __construct(Connection $connection, $entity, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher) |
|
53 | |||
54 | /** |
||
55 | * Sets query instance with delete operation and table |
||
56 | */ |
||
57 | protected function setQuery() |
||
62 | |||
63 | /** |
||
64 | * Executes query |
||
65 | * After execution query is reset |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function execute() |
||
84 | |||
85 | /** |
||
86 | * Resets adapter |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function reset() |
||
101 | } |
||
102 |
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.