TypeConverter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 16
loc 118
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
getTypeClassName() 0 1 ?
A __construct() 0 8 2
A fromPg() 0 10 2
A toPg() 8 8 2
A toPgStandardFormat() 8 8 2
A checkData() 0 10 2
A createObjectFrom() 0 14 2

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 Pomm'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\Exception\ConverterException;
13
use PommProject\Foundation\Converter\Type\BaseRange;
14
use PommProject\Foundation\Session\Session;
15
16
/**
17
 * TypeConverter
18
 *
19
 * Abstract class for converter that use object types like point, circle,
20
 * numrange etc.
21
 *
22
 * @package   Foundation
23
 * @copyright 2014 - 2017 Grégoire HUBERT
24
 * @author    Grégoire HUBERT
25
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
26
 */
27
abstract class TypeConverter implements ConverterInterface
28
{
29
    protected $class_name;
30
31
    /**
32
     * getTypeClassName
33
     *
34
     * Return the type class name
35
     *
36
     * @return string
37
     */
38
    abstract protected function getTypeClassName();
39
40
    /**
41
     * __construct
42
     *
43
     * Set the type class name.
44
     *
45
     * @param  string $class_name
46
     */
47
    public function __construct($class_name = null)
48
    {
49
        $this->class_name =
50
            $class_name === null
51
            ? $this->getTypeClassName()
52
            : $class_name
53
            ;
54
    }
55
56
    /**
57
     * fromPg
58
     *
59
     * @see ConverterInterface
60
     */
61
    public function fromPg($data, $type, Session $session)
62
    {
63
        $data = trim($data);
64
65
        return
66
            $data !== ''
67
            ? $this->createObjectFrom($data)
68
            : null
69
            ;
70
    }
71
72
    /**
73
     * toPg
74
     *
75
     * @see ConverterInterface
76
     */
77 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...
78
    {
79
        return
80
            $data !== null
81
            ? sprintf("%s('%s')", $type, $this->checkData($data))
82
            : sprintf("NULL::%s", $type)
83
            ;
84
    }
85
86
    /**
87
     * toPgStandardFormat
88
     *
89
     * @see ConverterInterface
90
     */
91 View Code Duplication
    public function toPgStandardFormat($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...
92
    {
93
        return
94
            $data !== null
95
            ? sprintf("%s", str_replace('"', '""', (string) $this->checkData($data)))
96
            : null
97
            ;
98
    }
99
100
    /**
101
     * checkData
102
     *
103
     * Check if data is suitable for Pg conversion. If not an attempt is made
104
     * to build the object from the given definition.
105
     *
106
     * @param  mixed    $data
107
     * @return object
108
     */
109
    public function checkData($data)
110
    {
111
        $class_name = $this->getTypeClassName();
112
113
        if (!$data instanceof $class_name) {
114
            $data = $this->createObjectFrom($data);
115
        }
116
117
        return $data;
118
    }
119
120
    /**
121
     * createObjectFrom
122
     *
123
     * Create a range object from a given definition. If the object creation
124
     * fails, an exception is thrown.
125
     *
126
     * @param  mixed $data
127
     * @return BaseRange
128
     * @throws ConverterException
129
     */
130
    protected function createObjectFrom($data)
131
    {
132
        $class_name = $this->class_name;
133
134
        try {
135
            return new $class_name($data);
136
        } catch (\InvalidArgumentException $e) {
137
            throw new ConverterException(
138
                sprintf("Unable to create a '%s' instance.", $class_name),
139
                null,
140
                $e
141
            );
142
        }
143
    }
144
}
145