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 |
||
28 | class ObjectNormalizer extends SymfonyObjectNormalizer |
||
29 | { |
||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $serializeNull; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param ClassMetadataFactoryInterface $classMetadataFactory |
||
39 | * @param NameConverterInterface $nameConverter |
||
40 | * @param PropertyAccessorInterface $propertyAccessor |
||
41 | * @param PropertyTypeExtractorInterface $propertyTypeExtractor |
||
42 | * @param bool $serializeNull |
||
43 | */ |
||
44 | 11 | public function __construct( |
|
55 | |||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | * |
||
60 | * @throws \InvalidArgumentException |
||
61 | * @throws \LogicException |
||
62 | */ |
||
63 | 9 | public function normalize($object, $format = null, array $context = []): array |
|
64 | { |
||
65 | // Call to before normalization callback |
||
66 | 9 | if (array_key_exists('before_normalization', $context)) { |
|
67 | 2 | $beforeNormalization = $context['before_normalization']; |
|
68 | |||
69 | 2 | View Code Duplication | if (!is_callable($beforeNormalization)) { |
|
|||
70 | 1 | throw new \InvalidArgumentException(sprintf( |
|
71 | 1 | 'Invalid callable for before normalization. "%s" given.', |
|
72 | 1 | is_object($beforeNormalization) ? get_class($beforeNormalization) : gettype($beforeNormalization) |
|
73 | )); |
||
74 | } |
||
75 | |||
76 | 1 | $beforeNormalization($object, $format, $context); |
|
77 | } |
||
78 | |||
79 | // Normalize object |
||
80 | 8 | $data = parent::normalize($object, $format, $context); |
|
81 | |||
82 | 8 | if (!$this->serializeNull) { |
|
83 | 1 | $data = $this->removeNullableAttributes($data); |
|
84 | } |
||
85 | |||
86 | // Call to after normalization callback |
||
87 | 8 | if (array_key_exists('after_normalization', $context)) { |
|
88 | 3 | $afterNormalization = $context['after_normalization']; |
|
89 | |||
90 | 3 | View Code Duplication | if (!is_callable($afterNormalization)) { |
91 | 1 | throw new \InvalidArgumentException(sprintf( |
|
92 | 1 | 'Invalid callable for after normalization. "%s" given.', |
|
93 | 1 | is_object($afterNormalization) ? get_class($afterNormalization) : gettype($afterNormalization) |
|
94 | )); |
||
95 | } |
||
96 | |||
97 | 2 | $data = $afterNormalization($data, $object, $format, $context); |
|
98 | |||
99 | 2 | if (!is_array($data)) { |
|
100 | 1 | throw new \LogicException(sprintf( |
|
101 | 1 | 'The after normalization callback should return array, but "%s" given.', |
|
102 | 1 | is_object($data) ? get_class($data) : gettype($data) |
|
103 | )); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | 6 | return $data; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Remove nullable attributes |
||
112 | * |
||
113 | * @param array $data |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | private function removeNullableAttributes(array $data): array |
||
123 | } |
||
124 |
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.