OracleProcessor   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 156
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A processColumnListing() 0 10 1
A processInsertGetId() 0 12 1
A prepareStatement() 0 8 1
B incrementBySequence() 0 16 5
A bindValues() 0 14 3
A getPdoType() 0 12 4
A saveLob() 0 23 3
1
<?php
2
3
namespace Yajra\Oci8\Query\Processors;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Processors\Processor;
7
use PDO;
8
9
class OracleProcessor extends Processor
10
{
11
    /**
12
     * Process an "insert get ID" query.
13
     *
14
     * @param  Builder $query
15
     * @param  string $sql
16
     * @param  array $values
17
     * @param  string $sequence
18
     * @return int
19
     */
20
    public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
21
    {
22
        $id        = 0;
23
        $parameter = 0;
24
        $statement = $this->prepareStatement($query, $sql);
25
        $values    = $this->incrementBySequence($values, $sequence);
26
        $parameter = $this->bindValues($values, $statement, $parameter);
0 ignored issues
show
Bug introduced by
It seems like $statement defined by $this->prepareStatement($query, $sql) on line 24 can also be of type object<Yajra\Pdo\Oci8>; however, Yajra\Oci8\Query\Process...Processor::bindValues() does only seem to accept object<PDOStatement>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
0 ignored issues
show
Bug introduced by
The method bindParam does only exist in PDOStatement, but not in Yajra\Pdo\Oci8.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28
        $statement->execute();
0 ignored issues
show
Bug introduced by
The method execute does only exist in PDOStatement, but not in Yajra\Pdo\Oci8.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29
30
        return (int) $id;
31
    }
32
33
    /**
34
     * Get prepared statement.
35
     *
36
     * @param Builder $query
37
     * @param string $sql
38
     * @return \PDOStatement|\Yajra\Pdo\Oci8
39
     */
40
    private function prepareStatement(Builder $query, $sql)
41
    {
42
        /** @var \Yajra\Oci8\Oci8Connection $connection */
43
        $connection = $query->getConnection();
44
        $pdo        = $connection->getPdo();
45
46
        return $pdo->prepare($sql);
47
    }
48
49
    /**
50
     * Insert a new record and get the value of the primary key.
51
     *
52
     * @param array $values
53
     * @param string $sequence
54
     * @return array
55
     */
56
    protected function incrementBySequence(array $values, $sequence)
57
    {
58
        $builder     = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[4]['object'];
59
        $builderArgs = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[3]['args'];
60
61
        if (! isset($builderArgs[1][0][$sequence])) {
62
            if (method_exists($builder, 'getModel')) {
63
                $model = $builder->getModel();
64
                if ($model->sequence && $model->incrementing) {
65
                    $values[] = (int) $model->getConnection()->getSequence()->nextValue($model->sequence);
66
                }
67
            }
68
        }
69
70
        return $values;
71
    }
72
73
    /**
74
     * Bind values to PDO statement.
75
     *
76
     * @param array $values
77
     * @param \PDOStatement $statement
78
     * @param int $parameter
79
     * @return int
80
     */
81
    private function bindValues(&$values, $statement, $parameter)
82
    {
83
        $count = count($values);
84
        for ($i = 0; $i < $count; $i++) {
85
            if (is_object($values[$i])) {
86
                $values[$i] = (string) $values[$i];
87
            }
88
            $type = $this->getPdoType($values[$i]);
89
            $statement->bindParam($parameter, $values[$i], $type);
90
            $parameter++;
91
        }
92
93
        return $parameter;
94
    }
95
96
    /**
97
     * Get PDO Type depending on value.
98
     *
99
     * @param mixed $value
100
     * @return int
101
     */
102
    private function getPdoType($value)
103
    {
104
        if (is_int($value)) {
105
            return PDO::PARAM_INT;
106
        } elseif (is_bool($value)) {
107
            return PDO::PARAM_BOOL;
108
        } elseif (is_null($value)) {
109
            return PDO::PARAM_NULL;
110
        } else {
111
            return PDO::PARAM_STR;
112
        }
113
    }
114
115
    /**
116
     * Save Query with Blob returning primary key value.
117
     *
118
     * @param  Builder $query
119
     * @param  string $sql
120
     * @param  array $values
121
     * @param  array $binaries
122
     * @return int
123
     */
124
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
125
    {
126
        $id        = 0;
127
        $parameter = 0;
128
        $statement = $this->prepareStatement($query, $sql);
129
130
        $parameter = $this->bindValues($values, $statement, $parameter);
0 ignored issues
show
Bug introduced by
It seems like $statement defined by $this->prepareStatement($query, $sql) on line 128 can also be of type object<Yajra\Pdo\Oci8>; however, Yajra\Oci8\Query\Process...Processor::bindValues() does only seem to accept object<PDOStatement>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
131
132
        $countBinary = count($binaries);
133
        for ($i = 0; $i < $countBinary; $i++) {
134
            $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1);
0 ignored issues
show
Bug introduced by
The method bindParam does only exist in PDOStatement, but not in Yajra\Pdo\Oci8.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
135
            $parameter++;
136
        }
137
138
        // bind output param for the returning clause.
139
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
140
141
        if (! $statement->execute()) {
0 ignored issues
show
Bug introduced by
The method execute does only exist in PDOStatement, but not in Yajra\Pdo\Oci8.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142
            return false;
143
        }
144
145
        return (int) $id;
146
    }
147
148
    /**
149
     * Process the results of a column listing query.
150
     *
151
     * @param  array $results
152
     * @return array
153
     */
154
    public function processColumnListing($results)
155
    {
156
        $mapping = function ($r) {
157
            $r = (object) $r;
158
159
            return $r->column_name;
160
        };
161
162
        return array_map($mapping, $results);
163
    }
164
}
165