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 |
||
27 | final class ObjectNormalizer extends Normalizer |
||
28 | { |
||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | public function normalize($object, $format = null, array $context = array()) |
||
33 | { |
||
34 | if ($object instanceof Activity) { |
||
35 | $activityData = array( |
||
36 | 'objectType' => 'Activity', |
||
37 | 'id' => $object->getId()->getValue(), |
||
38 | ); |
||
39 | |||
40 | if (null !== $definition = $object->getDefinition()) { |
||
41 | $activityData['definition'] = $this->normalizeAttribute($definition, $format, $context); |
||
42 | } |
||
43 | |||
44 | return $activityData; |
||
45 | } |
||
46 | |||
47 | if ($object instanceof StatementReference) { |
||
48 | return array( |
||
49 | 'objectType' => 'StatementRef', |
||
50 | 'id' => $object->getStatementId()->getValue(), |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | if ($object instanceof SubStatement) { |
||
55 | $data = array( |
||
56 | 'objectType' => 'SubStatement', |
||
57 | 'actor' => $this->normalizeAttribute($object->getActor(), $format, $context), |
||
58 | 'verb' => $this->normalizeAttribute($object->getVerb(), $format, $context), |
||
59 | 'object' => $this->normalizeAttribute($object->getObject(), $format, $context), |
||
60 | ); |
||
61 | |||
62 | if (null !== $result = $object->getResult()) { |
||
63 | $data['result'] = $this->normalizeAttribute($result, $format, $context); |
||
64 | } |
||
65 | |||
66 | if (null !== $statementContext = $object->getContext()) { |
||
67 | $data['context'] = $this->normalizeAttribute($statementContext, $format, $context); |
||
68 | } |
||
69 | |||
70 | return $data; |
||
71 | } |
||
72 | |||
73 | return null; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function supportsNormalization($data, $format = null) |
||
80 | { |
||
81 | return $data instanceof Object || $data instanceof StatementObject; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function denormalize($data, $class, $format = null, array $context = array()) |
||
88 | { |
||
89 | if (!isset($data['objectType']) || 'Activity' === $data['objectType']) { |
||
90 | return $this->denormalizeActivity($data, $format, $context); |
||
91 | } |
||
92 | |||
93 | if (isset($data['objectType']) && ('Agent' === $data['objectType'] || 'Group' === $data['objectType'])) { |
||
94 | return $this->denormalizeData($data, 'Xabbuh\XApi\Model\Actor', $format, $context); |
||
95 | } |
||
96 | |||
97 | if (isset($data['objectType']) && 'SubStatement' === $data['objectType']) { |
||
98 | return $this->denormalizeSubStatement($data, $format, $context); |
||
99 | } |
||
100 | |||
101 | if (isset($data['objectType']) && 'StatementRef' === $data['objectType']) { |
||
102 | return new StatementReference(StatementId::fromString($data['id'])); |
||
103 | } |
||
104 | |||
105 | return null; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function supportsDenormalization($data, $type, $format = null) |
||
112 | { |
||
113 | return 'Xabbuh\XApi\Model\Activity' === $type || 'Xabbuh\XApi\Model\StatementObject' === $type || 'Xabbuh\XApi\Model\Object' === $type || 'Xabbuh\XApi\Model\StatementReference' === $type || 'Xabbuh\XApi\Model\SubStatement' === $type; |
||
114 | } |
||
115 | |||
116 | private function denormalizeActivity(array $data, $format = null, array $context = array()) |
||
117 | { |
||
118 | $definition = null; |
||
119 | |||
120 | if (isset($data['definition'])) { |
||
121 | $definition = $this->denormalizeData($data['definition'], 'Xabbuh\XApi\Model\Definition', $format, $context); |
||
122 | } |
||
123 | |||
124 | return new Activity(IRI::fromString($data['id']), $definition); |
||
125 | } |
||
126 | |||
127 | private function denormalizeSubStatement(array $data, $format = null, array $context = array()) |
||
128 | { |
||
129 | $actor = $this->denormalizeData($data['actor'], 'Xabbuh\XApi\Model\Actor', $format, $context); |
||
130 | $verb = $this->denormalizeData($data['verb'], 'Xabbuh\XApi\Model\Verb', $format, $context); |
||
131 | if (class_exists('Xabbuh\XApi\Model\Object')) { |
||
132 | $object = (class_exists('Xabbuh\XApi\Model\Object'))?$this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\Object', $format, $context):$this->denormalizeData($data['object'], 'Xabbuh\XApi\Model\StatementObject', $format, $context); |
||
133 | $result = null; |
||
134 | $statementContext = null; |
||
135 | |||
136 | if (isset($data['result'])) { |
||
137 | $result = $this->denormalizeData($data['result'], 'Xabbuh\XApi\Model\Result', $format, $context); |
||
138 | } |
||
139 | |||
140 | if (isset($data['context'])) { |
||
141 | $statementContext = $this->denormalizeData($data['context'], 'Xabbuh\XApi\Model\Context', $format, $context); |
||
142 | } |
||
143 | |||
144 | return new SubStatement($actor, $verb, $object, $result, $statementContext); |
||
145 | } |
||
146 | } |
||
147 | |||
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.