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 |
||
11 | class XmlSchemaDateHandler implements SubscribingHandlerInterface |
||
12 | { |
||
13 | |||
14 | protected $defaultTimezone; |
||
15 | |||
16 | public static function getSubscribingMethods() |
||
17 | { |
||
18 | return array( |
||
19 | array( |
||
20 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
||
21 | 'format' => 'xml', |
||
22 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
||
23 | 'method' => 'deserializeDate' |
||
24 | ), |
||
25 | array( |
||
26 | 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
||
27 | 'format' => 'xml', |
||
28 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Date', |
||
29 | 'method' => 'serializeDate' |
||
30 | ), |
||
31 | array( |
||
32 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
||
33 | 'format' => 'xml', |
||
34 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
||
35 | 'method' => 'deserializeDateTime' |
||
36 | ), |
||
37 | array( |
||
38 | 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
||
39 | 'format' => 'xml', |
||
40 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime', |
||
41 | 'method' => 'serializeDateTime' |
||
42 | ), |
||
43 | array( |
||
44 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
||
45 | 'format' => 'xml', |
||
46 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
||
47 | 'method' => 'deserializeTime' |
||
48 | ), |
||
49 | array( |
||
50 | 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
||
51 | 'format' => 'xml', |
||
52 | 'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time', |
||
53 | 'method' => 'serializeTime' |
||
54 | ), |
||
55 | array( |
||
56 | 'type' => 'DateInterval', |
||
57 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
||
58 | 'format' => 'xml', |
||
59 | 'method' => 'deserializeDateIntervalXml', |
||
60 | ), |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | 19 | public function __construct($defaultTimezone = 'UTC') |
|
69 | |||
70 | 2 | View Code Duplication | public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $data, array $type){ |
71 | 2 | $attributes = $data->attributes('xsi', true); |
|
72 | 2 | if (isset($attributes['nil'][0]) && (string) $attributes['nil'][0] === 'true') { |
|
73 | return null; |
||
74 | } |
||
75 | 2 | return $this->createDateInterval((string)$data); |
|
76 | } |
||
77 | |||
78 | 5 | public function serializeDate(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
|
85 | |||
86 | 5 | public function deserializeDate(XmlDeserializationVisitor $visitor, $data, array $type) |
|
98 | |||
99 | 7 | public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
|
106 | |||
107 | View Code Duplication | public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
117 | |||
118 | public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context) |
||
119 | { |
||
120 | $v = $date->format('H:i:s'); |
||
121 | if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) { |
||
122 | $v .= $date->format('P'); |
||
123 | } |
||
124 | return $visitor->visitSimpleString($v, $type, $context); |
||
125 | } |
||
126 | |||
127 | View Code Duplication | public function deserializeTime(XmlDeserializationVisitor $visitor, $data, array $type) |
|
138 | |||
139 | 4 | private function parseDateTime($data, array $type) |
|
149 | |||
150 | 2 | private function createDateInterval($interval){ |
|
151 | 2 | $f = 0.0; |
|
152 | 2 | if (preg_match('~\.\d+~',$interval,$match)) { |
|
164 | } |
||
165 | |||
166 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.