Completed
Push — master ( 02d665...0e65c4 )
by Asmir
12s
created

XmlSchemaDateHandler   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 139
Duplicated Lines 15.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 21
loc 139
ccs 20
cts 75
cp 0.2667
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 0 47 1
A __construct() 0 5 1
A deserializeDateIntervalXml() 0 7 3
A serializeDate() 0 7 1
A deserializeDate() 0 12 4
A serializeDateTime() 0 7 1
A deserializeDateTime() 10 10 3
A serializeTime() 0 8 2
A deserializeTime() 11 11 3
A parseDateTime() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler;
3
4
use JMS\Serializer\Context;
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\SubscribingHandlerInterface;
7
use JMS\Serializer\XmlDeserializationVisitor;
8
use JMS\Serializer\XmlSerializationVisitor;
9
use RuntimeException;
10
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 17
    public function __construct($defaultTimezone = 'UTC')
65
    {
66 17
        $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
67
68 17
    }
69
70
    public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $data, array $type){
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
        $attributes = $data->attributes('xsi', true);
72
        if (isset($attributes['nil'][0]) && (string) $attributes['nil'][0] === 'true') {
73
            return null;
74
        }
75
        return new \DateInterval((string)$data);
76
    }
77
78 5
    public function serializeDate(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
79
    {
80
81 5
        $v = $date->format('Y-m-d');
82
83 5
        return $visitor->visitSimpleString($v, $type, $context);
84
    }
85
86 5
    public function deserializeDate(XmlDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88 5
        $attributes = $data->attributes('xsi', true);
89 5
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
90
            return null;
91
        }
92 5
        if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})(Z|([+-]\d{2}:\d{2}))?$/', $data)) {
93 1
            throw new RuntimeException(sprintf('Invalid date "%s", expected valid XML Schema date string.', $data));
94
        }
95
96 4
        return $this->parseDateTime($data, $type);
97
    }
98
99 7
    public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
100
    {
101
102 7
        $v = $date->format(\DateTime::W3C);
103
104 7
        return $visitor->visitSimpleString($v, $type, $context);
105
    }
106
107 View Code Duplication
    public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
108
    {
109
        $attributes = $data->attributes('xsi', true);
110
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
111
            return null;
112
        }
113
114
        return $this->parseDateTime($data, $type);
115
116
    }
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)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
    {
129
        $attributes = $data->attributes('xsi', true);
130
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
131
            return null;
132
        }
133
134
        $data = (string)$data;
135
136
        return new \DateTime($data, $this->defaultTimezone);
137
    }
138
139 4
    private function parseDateTime($data, array $type)
140
    {
141 4
        $timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
142 4
        $datetime = new \DateTime((string)$data, $timezone);
143 4
        if (false === $datetime) {
144
            throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data));
145
        }
146
147 4
        return $datetime;
148
    }
149
}
150
151