Oci8Connection   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 212
Duplicated Lines 13.68 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 25
c 3
b 1
f 0
lcom 1
cbo 3
dl 29
loc 212
ccs 110
cts 110
cp 1
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A changePassword() 7 7 1
A close() 15 15 3
A commit() 0 7 1
A connect() 0 12 1
A copyLob() 0 7 1
A freeDescriptor() 0 7 1
A getClientMayorVersion() 0 5 1
A getClientVersion() 0 4 1
A getNewCollection() 7 7 1
A getNewCursor() 0 7 1
A getNewDescriptor() 0 7 1
A getServerMayorVersion() 0 5 1
A getServerVersion() 0 7 1
A isLobEqual() 0 7 1
A parse() 0 7 1
A setAction() 0 7 1
A setClientIdentifier() 0 7 1
A setClientInfo() 0 7 1
A setInternalDebug() 0 4 1
A setModuleName() 0 7 1
A rollback() 0 7 1
A setEdition() 0 7 1

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
namespace Jpina\Oci8;
4
5
/**
6
 * Class Connection
7
 * @package Jpina\Oci8
8
 * @see http://php.net/manual/en/book.oci8.php
9
 */
10
class Oci8Connection extends AbstractOci8Base implements Oci8ConnectionInterface
11
{
12
    /**
13
     * Connection constructor.
14
     * @param $username
15
     * @param $password
16
     * @param null $connectionString
17
     * @param null $characterSet
18
     * @param null $sessionMode
19
     * @throws \Jpina\Oci8\Oci8Exception
20
     */
21 8
    public function __construct(
22
        $username,
23
        $password,
24
        $connectionString = null,
25
        $characterSet = null,
26
        $sessionMode = null
27
    ) {
28 8
        $this->resource = $this->connect($username, $password, $connectionString, $characterSet, $sessionMode);
29 5
    }
30
31 3 View Code Duplication
    public function changePassword($username, $oldPassword, $newPassword)
32
    {
33 3
        set_error_handler(static::getErrorHandler());
34 3
        $isSuccess = oci_password_change($this->resource, $username, $oldPassword, $newPassword);
35 1
        restore_error_handler();
36 2
        return $isSuccess;
37
    }
38
39 2 View Code Duplication
    public function close()
40
    {
41 2
        $isSuccess = false;
42 2
        if ($this->resource) {
43 1
            set_error_handler(static::getErrorHandler());
44 1
            $isSuccess = oci_close($this->resource);
45 1
            restore_error_handler();
46 1
        }
47
48 2
        if ($isSuccess) {
49 1
            $this->resource = null;
50 1
        }
51
52 2
        return $isSuccess;
53
    }
54
55 2
    public function commit()
56
    {
57 2
        set_error_handler(static::getErrorHandler());
58 2
        $isSuccess = oci_commit($this->resource);
59 1
        restore_error_handler();
60 1
        return $isSuccess;
61
    }
62
63
    /**
64
     * Connect to the Oracle server using a unique connection
65
     *
66
     * @param string $username
67
     * @param string $password
68
     * @param string $connectionString
69
     * @param string $characterSet
70
     * @param int $sessionMode
71
     * @return resource
72
     * @throws \Jpina\Oci8\Oci8Exception
73
     * @see http://php.net/manual/en/function.oci-new-connect.php
74
     */
75 4
    protected function connect(
76
        $username,
77
        $password,
78
        $connectionString = null,
79
        $characterSet = null,
80
        $sessionMode = null
81
    ) {
82 4
        set_error_handler(static::getErrorHandler());
83 4
        $connection = oci_new_connect($username, $password, $connectionString, $characterSet, $sessionMode);
84 3
        restore_error_handler();
85 3
        return $connection;
86
    }
87
88 1
    public function copyLob($lobTo, $lobFrom, $length = 0)
89
    {
90 1
        set_error_handler(static::getErrorHandler());
91 1
        $isSuccess = oci_lob_copy($lobTo, $lobFrom, $length);
92 1
        restore_error_handler();
93 1
        return $isSuccess;
94
    }
95
96 2
    public function freeDescriptor($descriptor)
97
    {
98 2
        set_error_handler(static::getErrorHandler());
99 2
        $isSuccess = oci_free_descriptor($descriptor);
100 1
        restore_error_handler();
101 1
        return $isSuccess;
102
    }
103
104 1
    public function getClientMayorVersion()
105
    {
106 1
        $clientVersion = $this->getClientVersion();
107 1
        return (int) substr($clientVersion, 0, strpos($clientVersion, '.'));
108
    }
109
110 2
    public function getClientVersion()
111
    {
112 2
        return oci_client_version();
113
    }
114
115 2 View Code Duplication
    public function getNewCollection($tdo, $schema = null)
116
    {
117 2
        set_error_handler(static::getErrorHandler());
118 2
        $collection = oci_new_collection($this->resource, $tdo, $schema);
119 1
        restore_error_handler();
120 1
        return $collection;
121
    }
122
123 2
    public function getNewCursor()
124
    {
125 2
        set_error_handler(static::getErrorHandler());
126 2
        $cursor = oci_new_cursor($this->resource);
127 1
        restore_error_handler();
128 1
        return new Oci8Cursor($cursor);
129
    }
130
131 3
    public function getNewDescriptor($type = OCI_DTYPE_LOB)
132
    {
133 3
        set_error_handler(static::getErrorHandler());
134 3
        $descriptor = oci_new_descriptor($this->resource, $type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $descriptor is correct as oci_new_descriptor($this->resource, $type) (which targets oci_new_descriptor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135 2
        restore_error_handler();
136 2
        return $descriptor;
137
    }
138
139 2
    public function getServerMayorVersion()
140
    {
141 2
        preg_match('/\\d+(:?\\.\\d+)+/', $this->getServerVersion(), $matches);
142 1
        return (int) substr($matches[0], 0, strpos($matches[0], '.'));
143
    }
144
145 4
    public function getServerVersion()
146
    {
147 4
        set_error_handler(static::getErrorHandler());
148 4
        $serverVersion = oci_server_version($this->resource);
149 2
        restore_error_handler();
150 2
        return $serverVersion;
151
    }
152
153 3
    public function isLobEqual($lob1, $lob2)
154
    {
155 3
        set_error_handler(static::getErrorHandler());
156 3
        $isEquals = oci_lob_is_equal($lob1, $lob2);
157 2
        restore_error_handler();
158 2
        return $isEquals;
159
    }
160
161 52
    public function parse($sqlText)
162
    {
163 52
        set_error_handler(static::getErrorHandler());
164 52
        $resource = oci_parse($this->resource, $sqlText);
165 51
        restore_error_handler();
166 51
        return new Oci8Statement($resource);
167
    }
168
169 2
    public function setAction($actionName)
170
    {
171 2
        set_error_handler(static::getErrorHandler());
172 2
        $isSuccess = oci_set_action($this->resource, $actionName);
173 1
        restore_error_handler();
174 1
        return $isSuccess;
175
    }
176
177 2
    public function setClientIdentifier($clientIdentifier)
178
    {
179 2
        set_error_handler(static::getErrorHandler());
180 2
        $isSuccess = oci_set_client_identifier($this->resource, $clientIdentifier);
181 1
        restore_error_handler();
182 1
        return $isSuccess;
183
    }
184
185 2
    public function setClientInfo($clientInfo)
186
    {
187 2
        set_error_handler(static::getErrorHandler());
188 2
        $isSuccess = oci_set_client_info($this->resource, $clientInfo);
189 1
        restore_error_handler();
190 1
        return $isSuccess;
191
    }
192
193 2
    public static function setEdition($edition)
194
    {
195 2
        set_error_handler(static::getErrorHandler());
196 2
        $isSuccess = oci_set_edition($edition);
197 2
        restore_error_handler();
198 2
        return $isSuccess;
199
    }
200
201 1
    public function setInternalDebug($onOff)
202
    {
203 1
        oci_internal_debug($onOff);
204 1
    }
205
206 2
    public function setModuleName($moduleName)
207
    {
208 2
        set_error_handler(static::getErrorHandler());
209 2
        $isSuccess = oci_set_module_name($this->resource, $moduleName);
210 1
        restore_error_handler();
211 1
        return $isSuccess;
212
    }
213
214 2
    public function rollback()
215
    {
216 2
        set_error_handler(static::getErrorHandler());
217 2
        $isSuccess = oci_rollback($this->resource);
218 1
        restore_error_handler();
219 1
        return $isSuccess;
220
    }
221
}
222