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 |
||
15 | class AttributeHandler implements HandlerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var DataTypeManager |
||
19 | */ |
||
20 | protected $typeManager; |
||
21 | |||
22 | /** |
||
23 | * AttributeHandler constructor. |
||
24 | * |
||
25 | * @param DataTypeManager $typeManager |
||
26 | */ |
||
27 | 6 | public function __construct(DataTypeManager $typeManager) |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 3 | public function toResource($object, ResourceObject $resource, MappingContext $context) |
|
44 | |||
45 | /** |
||
46 | * Process attribute to resource mapping |
||
47 | * |
||
48 | * @param mixed $object |
||
49 | * @param ResourceObject $resource |
||
50 | * @param Attribute $definition |
||
51 | */ |
||
52 | 3 | View Code Duplication | protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition) |
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 3 | public function fromResource($object, ResourceObject $resource, MappingContext $context) |
|
84 | |||
85 | /** |
||
86 | * Process resource to attribute mapping |
||
87 | * |
||
88 | * @param mixed $object |
||
89 | * @param ResourceObject $resource |
||
90 | * @param Attribute $definition |
||
91 | */ |
||
92 | 3 | View Code Duplication | protected function processResourceToAttribute($object, ResourceObject $resource, Attribute $definition) |
111 | } |
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.