DateTimeHandler::deserializeWonnovaDateTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
crap 1
1
<?php
2
namespace Wonnova\SDK\Serializer;
3
4
use JMS\Serializer\Context;
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\SubscribingHandlerInterface;
7
use Wonnova\SDK\Common\WonnovaDateTimeParserTrait;
8
9
/**
10
 * This fixes inconsistences in date fields returned from the API, so that all of them are cast into DateTime objects
11
 * @author Wonnova
12
 * @link http://www.wonnova.com
13
 */
14
class DateTimeHandler implements SubscribingHandlerInterface
15
{
16
    use WonnovaDateTimeParserTrait;
17
18
    /**
19
     * Return format:
20
     *
21
     *      array(
22
     *          array(
23
     *              'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
24
     *              'format' => 'json',
25
     *              'type' => 'DateTime',
26
     *              'method' => 'serializeDateTimeToJson',
27
     *          ),
28
     *      )
29
     *
30
     * The direction and method keys can be omitted.
31
     *
32
     * @return array
33
     */
34 43
    public static function getSubscribingMethods()
35
    {
36
        $wonnovaDateTimeDeserializationStrategyConfig = [
37 43
            'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
38 43
            'type' => 'WonnovaDateTime',
39 43
            'method' => 'deserializeWonnovaDateTime',
40 43
        ];
41
42
        return [
43 43
            static::getArrayWithFormat($wonnovaDateTimeDeserializationStrategyConfig),
0 ignored issues
show
Bug introduced by
Since getArrayWithFormat() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getArrayWithFormat() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
44 43
            static::getArrayWithFormat($wonnovaDateTimeDeserializationStrategyConfig, 'array'),
0 ignored issues
show
Bug introduced by
Since getArrayWithFormat() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getArrayWithFormat() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
45 43
        ];
46
    }
47
48 43
    private static function getArrayWithFormat($array, $format = 'json')
49
    {
50 43
        $array['format'] = $format;
51 43
        return $array;
52
    }
53
54 13
    public function deserializeWonnovaDateTime(
55
        $visitor,
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...
56
        $data,
57
        array $type,
0 ignored issues
show
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...
58
        Context $context
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
59
    ) {
60 13
        return $this->parseWonnovaDateTime($data);
61
    }
62
}
63