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 |
||
6 | class DataTransformer implements DataTransformerInterface |
||
7 | { |
||
8 | /** @var ModelClosure */ |
||
9 | protected $closuresFn; |
||
10 | /** @var TypeConverter */ |
||
11 | protected $typeConverter; |
||
12 | /** @var MapsManager */ |
||
13 | protected $mapsManager; |
||
14 | |||
15 | /** |
||
16 | * @param TypeConverter $converter |
||
17 | * @param MapsManager $mapsManager |
||
18 | * @param ModelClosure $closuresFn |
||
19 | */ |
||
20 | 4 | public function __construct(TypeConverter $converter, MapsManager $mapsManager, ModelClosure $closuresFn) |
|
26 | |||
27 | /** |
||
28 | * @return MapsManager |
||
29 | */ |
||
30 | 4 | public function getMapsManager() |
|
34 | |||
35 | /** |
||
36 | * @param ModelInterface $model |
||
37 | * @param string $mapType |
||
38 | * @return array |
||
39 | * |
||
40 | * @throws ParseException |
||
41 | */ |
||
42 | 2 | View Code Duplication | public function getData(ModelInterface $model, $mapType = 'dto') |
49 | |||
50 | /** |
||
51 | * @param array $data |
||
52 | * @param ModelInterface $model |
||
53 | * @param string $mapType |
||
54 | * |
||
55 | * @throws ParseException |
||
56 | */ |
||
57 | 1 | View Code Duplication | public function fillModel(array $data, ModelInterface $model, $mapType = 'dto') |
64 | |||
65 | /** |
||
66 | * @param string $class |
||
67 | * @return ModelInterface |
||
68 | */ |
||
69 | 1 | public function createModel($class) |
|
74 | } |
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.