Completed
Branch dev-0.1.x (cbb4c7)
by Josué
06:10
created

PdoOci8   B

Complexity

Total Complexity 54

Size/Duplication

Total Lines 480
Duplicated Lines 4.58 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 84%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 54
c 2
b 0
f 1
lcom 1
cbo 6
dl 22
loc 480
ccs 189
cts 225
cp 0.84
rs 7.0642

22 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 5 43 5
A getCharset() 0 6 1
A getConnection() 0 4 1
B getOracleConnection() 0 31 3
A getConnectionString() 0 10 1
B getConnectionStringItems() 0 34 6
A getDsnRegex() 0 12 1
A beginTransaction() 0 8 2
A commit() 0 12 2
A errorCode() 13 13 2
A errorInfo() 0 19 2
A exec() 0 11 2
A getStatementType() 0 10 1
A getAttribute() 0 8 2
A getAvailableDrivers() 0 4 1
A inTransaction() 0 5 1
A lastInsertId() 0 7 1
B prepare() 0 24 5
B query() 0 24 5
B quote() 0 22 4
A rollback() 0 20 3
A setAttribute() 4 20 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PdoOci8 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PdoOci8, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Jpina\PdoOci8;
4
5
use Jpina\Oci8\Oci8Connection;
6
use Jpina\Oci8\Oci8ConnectionInterface;
7
use Jpina\Oci8\Oci8Exception;
8
use Jpina\Oci8\Oci8PersistentConnection;
9
use Jpina\Oci8\Oci8StatementInterface;
10
11
/**
12
 * Custom PDO_OCI implementation via OCI8 driver
13
 *
14
 * @see http://php.net/manual/en/class.pdo.php
15
 */
16
class PdoOci8 extends \PDO
17
{
18
    const OCI_ATTR_SESSION_MODE = 8000;
19
20
    const OCI_ATTR_RETURN_LOBS = 8001;
21
22
    /**
23
     * @var Oci8ConnectionInterface
24
     */
25
    protected $connection;
26
27
    /**
28
     * @var int
29
     */
30
    private $autoCommitMode = false;
0 ignored issues
show
Unused Code introduced by
The property $autoCommitMode is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    /**
33
     * @var array
34
     */
35
    protected $options = array();
36
37
    // TODO Receive Oci8ConnectionInterface?
38 13
    public function __construct($dsn, $username = '', $password = '', $options = array())
39
    {
40 13
        $this->options = array(
41 13
            \PDO::ATTR_AUTOCOMMIT         => true,
42 13
            \PDO::ATTR_CASE               => \PDO::CASE_NATURAL,
43 13
            \PDO::ATTR_CLIENT_VERSION     => '',
44 13
            \PDO::ATTR_CONNECTION_STATUS  => '',
45 13
            \PDO::ATTR_DRIVER_NAME        => 'oci',
46 13
            \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_SILENT,
47 13
            \PDO::ATTR_ORACLE_NULLS       => \PDO::NULL_NATURAL,
48 13
            \PDO::ATTR_PERSISTENT         => false,
49 13
            \PDO::ATTR_PREFETCH           => 100,
50 13
            \PDO::ATTR_SERVER_INFO        => '',
51 13
            \PDO::ATTR_SERVER_VERSION     => '',
52 13
            \PDO::ATTR_TIMEOUT            => 600,
53 13
            \PDO::ATTR_STRINGIFY_FETCHES  => false,
54 13
            \PDO::ATTR_STATEMENT_CLASS    => null,
55 13
            \PDO::ATTR_EMULATE_PREPARES   => false,
56 13
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_BOTH,
57 13
            static::OCI_ATTR_SESSION_MODE => OCI_DEFAULT,
58 13
            static::OCI_ATTR_RETURN_LOBS  => false,
59
        );
60
61 13 View Code Duplication
        foreach ($options as $option => $value) {
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...
62 3
            if (array_key_exists($option, $this->options)) {
63 3
                $this->options[$option] = $value;
64 3
            }
65 13
        }
66
67 13
        foreach ($options as $attribute => $value) {
68 3
            $this->setAttribute($attribute, $value);
69 13
        }
70
71 13
        $connection = $this->getOracleConnection($dsn, $username, $password);
72 11
        $this->setAttribute(\PDO::ATTR_CLIENT_VERSION, $connection->getClientVersion());
73 11
        $this->setAttribute(\PDO::ATTR_SERVER_VERSION, $connection->getServerVersion());
74
75 11
        if ($this->getAttribute(\PDO::ATTR_AUTOCOMMIT) === false) {
76
            $this->beginTransaction();
77
        }
78
79 11
        $this->connection = $connection;
80 11
    }
81
82 12
    protected function getCharset($dsn)
83
    {
84 12
        $connectionStringItems = $this->getConnectionStringItems($dsn);
85
86 12
        return $connectionStringItems['charset'];
87
    }
88
89
    /**
90
     * @return Oci8ConnectionInterface
91
     */
92 42
    protected function getConnection()
93
    {
94 42
        return $this->connection;
95
    }
96
97
    /**
98
     * @param string $dsn
99
     * @param string $username
100
     * @param string $password
101
     * @return Oci8ConnectionInterface
102
     */
103 13
    protected function getOracleConnection($dsn, $username = null, $password = null)
104
    {
105 13
        $connectionString = $this->getConnectionString($dsn);
106 12
        $charset = $this->getCharset($dsn);
107 12
        $sessionMode = $this->getAttribute(static::OCI_ATTR_SESSION_MODE);
108
109 12
        $connection = null;
110
        try {
111 12
            if ($this->getAttribute(\PDO::ATTR_PERSISTENT)) {
112 1
                $connection = new Oci8PersistentConnection(
113 1
                    $username,
114 1
                    $password,
115 1
                    $connectionString,
116 1
                    $charset,
117
                    $sessionMode
118 1
                );
119 1
            } else {
120 11
                $connection = new Oci8Connection(
121 11
                    $username,
122 11
                    $password,
123 11
                    $connectionString,
124 11
                    $charset,
125
                    $sessionMode
126 11
                );
127
            }
128 12
        } catch (Oci8Exception $ex) {
129 1
            throw new PdoOci8Exception($ex->getMessage(), $ex->getCode(), $ex);
130
        }
131
132 11
        return $connection;
133
    }
134
135 13
    protected function getConnectionString($dsn)
136
    {
137 13
        $connectionStringItems = $this->getConnectionStringItems($dsn);
138 12
        $hostname = $connectionStringItems['hostname'];
139 12
        $port = $connectionStringItems['port'];
140 12
        $database = $connectionStringItems['database'];
141 12
        $connectionString = "//{$hostname}:{$port}/{$database}";
142
143 12
        return $connectionString;
144
    }
145
146 13
    protected function getConnectionStringItems($dsn)
147
    {
148 13
        $dsnRegex = $this->getDsnRegex();
149 13
        $matches = array();
150 13
        if (preg_match("/^{$dsnRegex}$/i", $dsn, $matches) !== 1) {
151 1
            throw new PdoOci8Exception('Invalid DSN');
152
        }
153
154
        // TODO Remove the variables below
155 12
        $hostname = 'localhost';
156 12
        $port     = 1521;
157 12
        $database = null;
0 ignored issues
show
Unused Code introduced by
$database is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158 12
        $charset  = 'AL32UTF8';
159
160 12
        switch (count($matches)) {
161 12
            case 16:
162 10
                $charset  = empty($matches[15]) ? $charset : $matches[15];
163
                // fall through next case to get the rest of the variables
164 12
            case 14:
165 12
                $port     = empty($matches[12]) ? $port : (int)$matches[12];
166 12
                $hostname = $matches[4];
167 12
                $database = $matches[13];
168 12
                break;
169
            default:
170
                $database = $matches[1];
171 12
        }
172
173
        return array(
174 12
            'hostname' => $hostname,
175 12
            'port'     => $port,
176 12
            'database' => $database,
177 12
            'charset'  => $charset,
178 12
        );
179
    }
180
181 13
    protected function getDsnRegex()
182
    {
183 13
        $hostnameRegrex = "(([a-z]|[a-z][a-z0-9\-_]*[a-z0-9])\.)*([a-z]|[a-z][a-z0-9\-_]*[a-z0-9])";
184
        $validIpAddressRegex = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}" .
185 13
            "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
186 13
        $hostnameOrIpAddressRegrex = "\/\/({$hostnameRegrex}|{$validIpAddressRegex})";
187 13
        $databaseRegex = "([a-z_][a-z\-_\d]*)|({$hostnameOrIpAddressRegrex}(:(\d+))?\/([a-z_][a-z\-_\d]*))";
188 13
        $charsetRegex = "(charset=([a-z\d\-_]+))?";
189 13
        $dsnRegex = "oci\:database=({$databaseRegex});?{$charsetRegex}";
190
191 13
        return $dsnRegex;
192
    }
193
194
    /**
195
     * @link http://php.net/manual/en/pdo.begintransaction.php
196
     * @return bool
197
     */
198 4
    public function beginTransaction()
199
    {
200 4
        if ($this->getAttribute(\PDO::ATTR_AUTOCOMMIT) === false) {
201 1
            return false;
202
        }
203
204 4
        return $this->setAttribute(\PDO::ATTR_AUTOCOMMIT, false);
205
    }
206
207
    /**
208
     * @link http://php.net/manual/en/pdo.commit.php
209
     * @return bool
210
     */
211 1
    public function commit()
212
    {
213
        try {
214 1
            $isSuccess = $this->getConnection()->commit();
215 1
        } catch (Oci8Exception $ex) {
216
            $isSuccess = false;
217
        }
218
219 1
        $this->setAttribute(\PDO::ATTR_AUTOCOMMIT, true);
220
221 1
        return $isSuccess;
222
    }
223
224
    /**
225
     * @link http://php.net/manual/en/pdo.errorcode.php
226
     * @return null|string
227
     */
228 1 View Code Duplication
    public function errorCode()
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...
229
    {
230 1
        $connection = $this->getConnection();
231 1
        $driverError = $connection->getError();
232 1
        if (!$driverError) {
233 1
            return null;
234
        }
235
236
        $error = $this->errorInfo();
237
        $sqlStateErrorCode = $error[0];
238
239
        return $sqlStateErrorCode;
240
    }
241
242
    /**
243
     * @link http://php.net/manual/en/pdo.errorinfo.php
244
     * @return array
245
     */
246 1
    public function errorInfo()
247
    {
248 1
        $error = $this->getConnection()->getError();
249 1
        if (is_array($error)) {
250
            $errorInfo = array(
251
                OracleSqlStateCode::getSqlStateErrorCode((int)$error['code']),
252
                $error['message'],
253
                $error['code']
254
            );
255
        } else {
256
            $errorInfo = array(
257 1
                '00000',
258 1
                null,
259
                null
260 1
            );
261
        }
262
263 1
        return $errorInfo;
264
    }
265
266
    /**
267
     * @param string $statement
268
     *
269
     * @link http://php.net/manual/en/pdo.exec.php
270
     * @return bool|int
271
     */
272 4
    public function exec($statement)
273
    {
274 4
        $statement = $this->prepare($statement, $this->options);
275 4
        $type      = $this->getStatementType($statement);
276 4
        if ($type === 'SELECT') {
277 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method PDO::exec of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
278
        }
279 3
        $statement->execute();
280
281 3
        return $statement->rowCount();
282
    }
283
284
    /**
285
     * @param $statement
286
     * @return PdoOci8Statement
287
     */
288 4
    protected function getStatementType($statement)
289
    {
290 4
        $class = new \ReflectionClass($statement);
291 4
        $property = $class->getProperty('statement');
292 4
        $property->setAccessible(true);
293
        /** @var Oci8StatementInterface $rawStatement */
294 4
        $oci8Statement = $property->getValue($statement);
295
296 4
        return $oci8Statement->getType();
297
    }
298
299
    /**
300
     * @param int $attribute
301
     *
302
     * @link http://php.net/manual/en/pdo.getattribute.php
303
     * @return mixed
304
     */
305 12
    public function getAttribute($attribute)
306
    {
307 12
        if (array_key_exists($attribute, $this->options)) {
308 12
            return $this->options[$attribute];
309
        }
310
311
        return null;
312
    }
313
314
    /**
315
     * @link http://php.net/manual/en/pdo.getavailabledrivers.php
316
     * @return array
317
     */
318 1
    public static function getAvailableDrivers()
319
    {
320 1
        return array('oci');
321
    }
322
323
    /**
324
     * @link http://php.net/manual/en/pdo.intransaction.php
325
     * @return bool
326
     */
327 3
    public function inTransaction()
328
    {
329 3
        $inTransaction = !$this->getAttribute(\PDO::ATTR_AUTOCOMMIT);
330 3
        return $inTransaction;
331
    }
332
333
    /**
334
     * @param string $name
335
     *
336
     * @link http://php.net/manual/en/pdo.lastinsertid.php
337
     * @return string
338
     */
339
    public function lastInsertId($name = null)
340
    {
341
        $statement = $this->query("select {$name}.currval from dual");
342
        $lastInsertedId = $statement->fetch();
343
344
        return $lastInsertedId;
345
    }
346
347
    /**
348
     * @param string $statement
349
     * @param array $driver_options
350
     *
351
     * @link http://php.net/manual/en/pdo.prepare.php
352
     * @return bool|PdoOci8Statement
353
     */
354 38
    public function prepare($statement, $driver_options = array())
355
    {
356
        // TODO Use $driver_options, eg. configure a cursor
357 38
        $exception = null;
0 ignored issues
show
Unused Code introduced by
$exception is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
358
        try {
359 38
            return new PdoOci8Statement($this->getConnection(), $statement, $driver_options);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Jpina\PdoOci...ment, $driver_options); (Jpina\PdoOci8\PdoOci8Statement) is incompatible with the return type of the parent method PDO::prepare of type PDOStatement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
360
        } catch (\Exception $ex) {
361
            $exception = new PdoOci8Exception($ex->getMessage(), $ex->getCode(), $ex);
362
        }
363
364
        // TODO Create error handler
365
        $errorMode = $this->getAttribute(\PDO::ATTR_ERRMODE);
366
        switch ($errorMode) {
367
            case \PDO::ERRMODE_EXCEPTION:
368
                throw $exception;
369
            case \PDO::ERRMODE_WARNING:
370
                trigger_error($exception->getMessage(), E_WARNING);
371
                break;
372
            case \PDO::ERRMODE_SILENT:
373
            default:
374
        }
375
376
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method PDO::prepare of type PDOStatement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
377
    }
378
379
    /**
380
     * @param string $statement
381
     * @param int $mode
382
     * @param int|string|object $arg3
383
     * @param array $ctorargs
384
     *
385
     * @link http://php.net/manual/en/pdo.query.php
386
     * @return bool|PdoOci8Statement
387
     */
388 3
    public function query($statement, $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, $ctorargs = array())
389
    {
390 3
        $result = false;
391
        try {
392 3
            $statement = $this->prepare($statement);
393
            switch ($mode) {
394 3
                case \PDO::FETCH_CLASS:
395
                    $statement->setFetchMode($mode, $arg3, $ctorargs);
396
                    break;
397 3
                case \PDO::FETCH_INTO:
398
                    $statement->setFetchMode($mode, $arg3);
399
                    break;
400 3
                case \PDO::FETCH_COLUMN:
401 3
                default:
402 3
                    $statement->setFetchMode($mode);
403 3
            }
404 3
            $statement->execute();
405 3
            $result = $statement;
406 3
        } catch (\PDOException $ex) {
407
            //TODO Handle Exception
408
        }
409
410 3
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (Jpina\PdoOci8\PdoOci8Statement|false) is incompatible with the return type of the parent method PDO::query of type PDOStatement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
411
    }
412
413
    /**
414
     * @param string $string
415
     * @param int $parameter_type
416
     *
417
     * @link http://php.net/manual/en/pdo.quote.php
418
     * @return bool|string
419
     */
420 5
    public function quote($string, $parameter_type = \PDO::PARAM_STR)
421
    {
422 5
        if (!is_scalar($string)) {
423 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method PDO::quote of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
424
        }
425
426
        try {
427 4
            if ($parameter_type !== \PDO::PARAM_STR) {
428 1
                $quotedString = (string)$string;
429 1
                $quotedString = "'" . $quotedString . "'";
430 1
                return $quotedString;
431
            }
432 4
        } catch (\Exception $ex) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $ex) { return false; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
433
            return false;
434
        }
435
436 4
        $quotedString = str_replace("\'", "'", $string);
437 4
        $quotedString = str_replace("'", "''", $quotedString);
438 4
        $quotedString = "'" . $quotedString . "'";
439
440 4
        return $quotedString;
441
    }
442
443
    /**
444
     * @link http://php.net/manual/en/pdo.rollback.php
445
     * @return bool
446
     */
447 2
    public function rollback()
448
    {
449 2
        if (!$this->inTransaction()) {
450 1
            throw new PdoOci8Exception('There is no active transaction');
451
        }
452
453
        try {
454 1
            $isSuccess = $this->getConnection()->rollback();
455 1
        } catch (Oci8Exception $ex) {
456
            $isSuccess = false;
457
        }
458
459
        // TODO Restore autocommit mode
460
        // If the database was set to autocommit mode
461
        // this function will restore autocommit mode after
462
        // it has rolled back the transaction
463 1
        $this->setAttribute(\PDO::ATTR_AUTOCOMMIT, true);
464
465 1
        return $isSuccess;
466
    }
467
468
    /**
469
     * @param int $attribute
470
     * @param mixed $value
471
     *
472
     * @link http://php.net/manual/en/pdo.setattribute.php
473
     * @return bool
474
     */
475 11
    public function setAttribute($attribute, $value)
476
    {
477
        $readOnlyAttributes = array(
478 11
            \PDO::ATTR_CLIENT_VERSION,
479 11
            \PDO::ATTR_CONNECTION_STATUS,
480 11
            \PDO::ATTR_DRIVER_NAME,
481 11
            \PDO::ATTR_PERSISTENT,
482 11
            \PDO::ATTR_SERVER_INFO,
483 11
            \PDO::ATTR_SERVER_VERSION,
484 11
        );
485
486 11 View Code Duplication
        if (array_search($attribute, $readOnlyAttributes) !== false ||
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...
487 11
            !array_key_exists($attribute, $this->options)) {
488 11
            return false;
489
        }
490
491 5
        $this->options[$attribute] = $value;
492
493 5
        return true;
494
    }
495
}
496