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 |
||
25 | final class StatementNormalizer extends Normalizer |
||
26 | { |
||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function normalize($object, $format = null, array $context = array()) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function supportsNormalization($data, $format = null) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function denormalize($data, $class, $format = null, array $context = array()) |
||
89 | { |
||
90 | $version = null; |
||
91 | |||
92 | if (isset($data['version'])) { |
||
93 | $version = $data['version']; |
||
94 | |||
95 | if (!preg_match('/^1\.0(?:\.\d+)?$/', $version)) { |
||
96 | throw new UnsupportedStatementVersionException(sprintf('Statements at version "%s" are not supported.', $version)); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | $id = isset($data['id']) ? StatementId::fromString($data['id']) : null; |
||
101 | $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context); |
||
102 | $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context); |
||
103 | |||
104 | View Code Duplication | if (class_exists(StatementObject::class)) { |
|
105 | $object = $this->denormalizeData($data['object'], StatementObject::class, $format, $context); |
||
106 | } else { |
||
107 | $object = $this->denormalizeData($data['object'], LegacyStatementObject::class, $format, $context); |
||
108 | } |
||
109 | |||
110 | $result = null; |
||
111 | $authority = null; |
||
112 | $created = null; |
||
113 | $stored = null; |
||
114 | $statementContext = null; |
||
115 | $attachments = null; |
||
116 | |||
117 | if (isset($data['result'])) { |
||
118 | $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context); |
||
119 | } |
||
120 | |||
121 | if (isset($data['authority'])) { |
||
122 | $authority = $this->denormalizeData($data['authority'], 'Xabbuh\XApi\Model\Actor', $format, $context); |
||
123 | } |
||
124 | |||
125 | if (isset($data['timestamp'])) { |
||
126 | $created = $this->denormalizeData($data['timestamp'], 'DateTime', $format, $context); |
||
127 | } |
||
128 | |||
129 | if (isset($data['stored'])) { |
||
130 | $stored = $this->denormalizeData($data['stored'], 'DateTime', $format, $context); |
||
131 | } |
||
132 | |||
133 | if (isset($data['context'])) { |
||
134 | $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context); |
||
135 | } |
||
136 | |||
137 | if (isset($data['attachments'])) { |
||
138 | $attachments = $this->denormalizeData($data['attachments'], 'Xabbuh\XApi\Model\Attachment[]', $format, $context); |
||
139 | } |
||
140 | |||
141 | return new Statement($id, $actor, $verb, $object, $result, $authority, $created, $stored, $statementContext, $attachments, $version); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function supportsDenormalization($data, $type, $format = null) |
||
151 | } |
||
152 |
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.