Completed
Push — master ( 91f892...09365e )
by Asmir
02:45
created

XmlSchemaDateHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 22.34 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 18%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 15
c 2
b 1
f 1
lcom 1
cbo 1
dl 21
loc 94
ccs 9
cts 50
cp 0.18
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getSubscribingMethods() 0 29 1
A __construct() 0 5 1
A serializeDateTime() 0 9 2
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\DateTime',
23
                'method' => 'deserializeDateTime'
24
            ),
25
            array(
26
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27
                'format' => 'xml',
28
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime',
29
                'method' => 'serializeDateTime'
30
            ),
31
            array(
32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33
                'format' => 'xml',
34
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
35
                'method' => 'deserializeTime'
36
            ),
37
            array(
38
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
39
                'format' => 'xml',
40
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
41
                'method' => 'serializeTime'
42
            )
43
        );
44
    }
45
46 5
    public function __construct($defaultTimezone = 'UTC')
47
    {
48 5
        $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
49
50 5
    }
51
52 5
    public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
53
    {
54
55 5
        $v = $date->format(\DateTime::W3C);
56 5
        if (substr($v, -5) == "00:00") {
57 4
            $v = substr($v, 0, -6);
58 4
        }
59 5
        return $visitor->visitSimpleString($v, $type, $context);
60
    }
61
62 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...
63
    {
64
        $attributes = $data->attributes('xsi', true);
65
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
66
            return null;
67
        }
68
69
        return $this->parseDateTime($data, $type);
70
71
    }
72
73
    public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
74
    {
75
        $v = $date->format('H:i:s');
76
        if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) {
77
            $v .= $date->format('P');
78
        }
79
        return $visitor->visitSimpleString($v, $type, $context);
80
    }
81
82 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...
83
    {
84
        $attributes = $data->attributes('xsi', true);
85
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
86
            return null;
87
        }
88
89
        $data = (string)$data;
90
91
        return new \DateTime($data, $this->defaultTimezone);
92
    }
93
94
    private function parseDateTime($data, array $type)
95
    {
96
        $timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
97
        $datetime = new \DateTime((string)$data, $timezone);
98
        if (false === $datetime) {
99
            throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data));
100
        }
101
102
        return $datetime;
103
    }
104
}
105
106