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 |
||
8 | class EventAdapter |
||
9 | { |
||
10 | /** |
||
11 | * @var JsonTransformer |
||
12 | */ |
||
13 | private $jsonTransformer; |
||
14 | |||
15 | /** |
||
16 | * @param JsonTransformer $jsonTransformer |
||
17 | */ |
||
18 | 85 | public function __construct(JsonTransformer $jsonTransformer) |
|
22 | |||
23 | /** |
||
24 | * @param StoredEvent $storedEvent |
||
25 | * @param string $pathExpression |
||
26 | * @param string $newName |
||
27 | */ |
||
28 | 14 | public function renameField($storedEvent, $pathExpression, $newName) |
|
37 | |||
38 | /** |
||
39 | * @param StoredEvent $storedEvent |
||
40 | * @param string $newName |
||
41 | */ |
||
42 | 1 | public function rename($storedEvent, $newName) |
|
46 | |||
47 | /** |
||
48 | * @param StoredEvent $storedEvent |
||
49 | * @param string $pathExpression |
||
50 | * @param \Closure $closure |
||
51 | */ |
||
52 | 1 | View Code Duplication | public function enrich($storedEvent, $pathExpression, \Closure $closure) |
62 | |||
63 | /** |
||
64 | * @param StoredEvent $storedEvent |
||
65 | * @param string $pathExpression |
||
66 | */ |
||
67 | 1 | public function removeField($storedEvent, $pathExpression) |
|
75 | |||
76 | /** |
||
77 | * @param StoredEvent $storedEvent |
||
78 | * @param string $pathExpression |
||
79 | * @param \Closure $closure |
||
80 | */ |
||
81 | 3 | View Code Duplication | public function changeValue($storedEvent, $pathExpression, \Closure $closure) |
91 | } |
||
92 |
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.