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 |
||
18 | class Callback |
||
19 | { |
||
20 | protected $serializer; |
||
21 | protected $groups = []; |
||
22 | protected $version; |
||
23 | protected $serializeNull = false; |
||
24 | |||
25 | /** |
||
26 | * @param $serializer |
||
27 | */ |
||
28 | 5 | public function setSerializer($serializer) |
|
35 | |||
36 | /** |
||
37 | * @param array $groups |
||
38 | */ |
||
39 | 4 | View Code Duplication | public function setGroups(array $groups): void |
40 | { |
||
41 | 4 | $this->groups = $groups; |
|
42 | |||
43 | 4 | if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) { |
|
44 | 1 | throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer".'); |
|
45 | } |
||
46 | 3 | } |
|
47 | |||
48 | /** |
||
49 | * @param $version |
||
50 | */ |
||
51 | 1 | public function setVersion($version): void |
|
59 | |||
60 | 2 | View Code Duplication | public function setSerializeNull(bool $serializeNull): void |
68 | |||
69 | /** |
||
70 | * @param $object |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | 2 | public function serialize($object) |
|
98 | } |
||
99 |