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 namespace C4tech\RayEmitter\Domain; |
||
11 | abstract class Aggregate implements AggregateInterface |
||
12 | { |
||
13 | /** |
||
14 | * Aggregate root entity. |
||
15 | * @var AggregateRootInterface |
||
16 | */ |
||
17 | protected $root; |
||
18 | |||
19 | /** |
||
20 | * Event sequence counter. |
||
21 | * @var integer |
||
22 | */ |
||
23 | protected $sequence = -1; |
||
24 | |||
25 | /** |
||
26 | * @inheritDoc |
||
27 | */ |
||
28 | 2 | View Code Duplication | public function apply(EventInterface $event) |
45 | |||
46 | /** |
||
47 | * Create Method Name |
||
48 | * |
||
49 | * Generate a method name using a configurable prefix and an object's class basename. |
||
50 | * @param string $prefix_key The key from the configuration to lookup the appropriate prefix. |
||
51 | * @param object $object The object which shall be reduced to its class basename. |
||
52 | * @param string $prefix_default The default prefix. |
||
53 | * @return string The method name to use. |
||
54 | */ |
||
55 | 2 | protected function createMethodName($prefix_key, $object, $prefix_default = null) |
|
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | 1 | public function getEntity() |
|
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | 1 | public function getId() |
|
83 | |||
84 | /** |
||
85 | * @inheritDoc |
||
86 | */ |
||
87 | 3 | public function getSequence() |
|
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | 2 | View Code Duplication | public function handle(CommandInterface $command) |
112 | |||
113 | /** |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function hydrate(EventCollectionInterface $events) |
||
123 | |||
124 | /** |
||
125 | * Magic Getter |
||
126 | * |
||
127 | * Expose getter methods on the Aggregate and Aggregate Root as properties. |
||
128 | * @param string $property Requested "property" |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 2 | public function __get($property) |
|
141 | } |
||
142 |
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.