DateTimeType   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 65
Duplicated Lines 55.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 36
loc 65
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A createFromFormat() 0 4 1
A getDescription() 0 4 1
A serialize() 12 12 4
A parseValue() 12 12 4
A isValidValue() 12 12 6

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
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/27/15 1:22 AM
7
*/
8
9
namespace Youshido\GraphQL\Type\Scalar;
10
11
class DateTimeType extends AbstractScalarType
12
{
13
14
    private $format;
15
16 9
    public function __construct($format = 'Y-m-d H:i:s')
17
    {
18 9
        $this->format = $format;
19 9
    }
20
21 1
    public function getName()
22
    {
23 1
        return 'DateTime';
24
    }
25
26 7 View Code Duplication
    public function isValidValue($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...
27
    {
28 7
        if ((is_object($value) && $value instanceof \DateTimeInterface) || is_null($value)) {
29 6
            return true;
30 3
        } else if (is_string($value)) {
31 3
            $date = $this->createFromFormat($value);
32 3
        } else {
33
            $date = null;
34
        }
35
36 3
        return $date ? true : false;
37
    }
38
39 7 View Code Duplication
    public function serialize($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...
40
    {
41 7
        $date = null;
42
43 7
        if (is_string($value)) {
44
            $date = $this->createFromFormat($value);
45 7
        } elseif ($value instanceof \DateTimeInterface) {
46 4
            $date = $value;
47 4
        }
48
49 7
        return $date ? $date->format($this->format) : null;
50
    }
51
52 6 View Code Duplication
    public function parseValue($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...
53
    {
54 6
        if (is_string($value)) {
55 3
            $date = $this->createFromFormat($value);
56 6
        } elseif ($value instanceof \DateTimeInterface) {
57 1
            $date = $value;
58 1
        } else {
59 3
            $date = false;
60
        }
61
62 6
        return $date ?: null;
63
    }
64
65 3
    private function createFromFormat($value)
66
    {
67 3
        return \DateTime::createFromFormat($this->format, $value);
68
    }
69
70 1
    public function getDescription()
71
    {
72 1
        return sprintf('Representation of date and time in "%s" format', $this->format);
73
    }
74
75
}
76