Completed
Push — master ( 582431...151715 )
by Karsten
02:40
created

LocalDateMapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 3
dl 14
loc 70
ccs 10
cts 20
cp 0.5
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOptions() 0 4 1
A slumber() 11 11 2
C awake() 3 22 7

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
/**
3
 * File was created 30.09.2015 07:32
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data\MongoDb\Types;
7
8
use MongoDB\BSON\UTCDateTime;
9
use PeekAndPoke\Component\Psi\Functions\Unary\Matcher\IsDateString;
10
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsLocalDate;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
12
use PeekAndPoke\Component\Slumber\Core\Codec\Property\AbstractPropertyMapper;
13
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
14
use PeekAndPoke\Types\LocalDate;
15
16
/**
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class LocalDateMapper extends AbstractPropertyMapper
20
{
21
    /** @var AsLocalDate */
22
    private $options;
23
24
    /**
25
     * C'tor.
26
     *
27
     * @param AsLocalDate $options
28
     */
29
    public function __construct(AsLocalDate $options)
30
    {
31
        $this->options = $options;
32
    }
33
34
    /**
35
     * @return AsLocalDate
36
     */
37
    public function getOptions()
38
    {
39
        return $this->options;
40
    }
41
42
    /**
43
     * @param Slumberer $slumberer
44
     * @param mixed     $value
45
     *
46
     * @return array
47
     */
48 21 View Code Duplication
    public function slumber(Slumberer $slumberer, $value)
0 ignored issues
show
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...
49
    {
50 21
        if (! $value instanceof LocalDate) {
51 4
            return null;
52
        }
53
54
        return [
55 17
            'date' => new UTCDateTime($value->getTimestamp() * 1000),
56 17
            'tz'   => $value->getTimezone()->getName(),
57
        ];
58
    }
59
60
    /**
61
     * @param Awaker $awaker
62
     * @param mixed  $value
63
     *
64
     * @return LocalDate
65
     */
66 17
    public function awake(Awaker $awaker, $value)
67
    {
68
        // default cases first
69 17
        $canAccess     = $value instanceof \ArrayAccess || is_array($value);
70 17
        $hasComponents = isset($value['date'], $value['tz']);
71
72 17 View Code Duplication
        if ($canAccess && $hasComponents && $value['date'] instanceof \DateTime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73 17
            return new LocalDate($value['date'], $value['tz']);
74
        }
75
76
        // compatibility in case a SimpleDate was changed to a LocalDate
77
        if ($value instanceof \DateTime) {
78
            return LocalDate::raw($value);
79
        }
80
81
        // compatibility in case a string was change to a LocalDate
82
        if (IsDateString::isValidDateString($value)) {
83
            return LocalDate::raw(new \DateTime($value));
84
        }
85
86
        return null;
87
    }
88
}
89