PgTimestamp   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 36.99 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 27
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromPg() 0 6 2
A toPg() 8 8 2
A toPgStandardFormat() 0 8 2
A checkData() 19 19 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
/*
3
 * This file is part of PommProject's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter;
11
12
use PommProject\Foundation\Session\Session;
13
use PommProject\Foundation\Exception\ConverterException;
14
15
/**
16
 * PgTimestamp
17
 *
18
 * Date and timestamp converter
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2017 Grégoire HUBERT
22
 * @author    Grégoire HUBERT <[email protected]>
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 * @see       ConverterInterface
25
 */
26
class PgTimestamp implements ConverterInterface
27
{
28
    const TS_FORMAT = 'Y-m-d H:i:s.uP';
29
30
    /**
31
     * fromPg
32
     *
33
     * @see ConverterInterface
34
     */
35
    public function fromPg($data, $type, Session $session)
36
    {
37
        $data = trim($data);
38
39
        return $data !== '' ? new \DateTime($data) : null;
40
    }
41
42
    /**
43
     * toPg
44
     *
45
     * @see ConverterInterface
46
     */
47 View Code Duplication
    public function toPg($data, $type, Session $session)
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...
48
    {
49
        return
50
            $data !== null
51
            ? sprintf("%s '%s'", $type, $this->checkData($data)->format(static::TS_FORMAT))
52
            : sprintf("NULL::%s", $type)
53
            ;
54
    }
55
56
    /**
57
     * toPgStandardFormat
58
     *
59
     * @see ConverterInterface
60
     */
61
    public function toPgStandardFormat($data, $type, Session $session)
62
    {
63
        return
64
            $data !== null
65
            ? $this->checkData($data)->format(static::TS_FORMAT)
66
            : null
67
            ;
68
    }
69
70
    /**
71
     * checkData
72
     *
73
     * Ensure a DateTime instance.
74
     *
75
     * @param  mixed $data
76
     * @throws ConverterException
77
     * @return \DateTime
78
     */
79 View Code Duplication
    protected function checkData($data)
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...
80
    {
81
        if (!$data instanceof \DateTimeInterface) {
82
            try {
83
                $data = new \DateTime($data);
84
            } catch (\Exception $e) {
85
                throw new ConverterException(
86
                    sprintf(
87
                        "Cannot convert data from invalid datetime representation '%s'.",
88
                        $data
89
                    ),
90
                    null,
91
                    $e
92
                );
93
            }
94
        }
95
96
        return $data;
97
    }
98
}
99