Passed
Pull Request — main (#122)
by Andreas
02:10
created

CrateStatement::bindValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\DBAL\Driver\PDOCrate;
24
25
use Crate\PDO\PDOStatement;
26
use Crate\PDO\PDOInterface;
27
use Doctrine\DBAL\Driver\PDO\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Crate\DBAL\Driver\PDOCrate\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
use Doctrine\DBAL\Driver\PDOStatementImplementations;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Driver\PDOStatementImplementations was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Doctrine\DBAL\Driver\Statement as StatementInterface;
30
use Doctrine\DBAL\Driver\Result as ResultInterface;
31
use Doctrine\DBAL\ParameterType;
32
use Doctrine\Deprecations\Deprecation;
33
use PDO;
34
35
/**
36
 * @internal
37
 */
38
final class CrateStatement implements StatementInterface
39
{
40
    private PDOInterface $pdo;
41
    private PDOStatement $stmt;
42
43
    /**
44
     * @param string              $sql
45
     * @param array<string,mixed> $options
46
     */
47 110
    public function __construct(PDOInterface $pdo, $sql, $options = [])
48
    {
49 110
        $this->pdo  = $pdo;
50 110
        $this->stmt = $pdo->prepare($sql, $options);
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 108
    public function execute($params = null): ResultInterface
57
    {
58
59 108
        if ($params !== null) {
60 6
            Deprecation::trigger(
61 6
                'doctrine/dbal',
62 6
                'https://github.com/doctrine/dbal/pull/5556',
63 6
                'Passing $params to Statement::execute() is deprecated. Bind parameters using'
64 6
                . ' Statement::bindParam() or Statement::bindValue() instead.',
65 6
            );
66
        }
67 108
        $this->stmt->execute($params);
68 108
        return new Result($this);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function columnCount(): int
75
    {
76
        return $this->stmt->columnCount();
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 90
    public function rowCount(): int
83
    {
84 90
        return $this->stmt->rowCount();
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 86
    public function bindValue($param, $value, $type = ParameterType::STRING)
91
    {
92 86
        return $this->stmt->bindValue($param, $value, $type);
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 12
    public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
99
    {
100 12
        return $this->stmt->bindParam($param, $variable, $type, $length);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->stmt->bindParam($...riable, $type, $length) targeting Crate\PDO\PDOStatement::bindParam() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 72
    public function fetch(
107
        $fetch_style = PDO::FETCH_ASSOC,
108
        $cursor_orientation = PDO::FETCH_ORI_NEXT,
109
        $cursor_offset = 0,
110
    ) {
111 72
        return $this->stmt->fetch($fetch_style, $cursor_orientation, $cursor_offset);
112
    }
113
114
    /**
115
     * @phpstan-param PDO::FETCH_* $mode
116
     *
117
     * @return list<mixed>
0 ignored issues
show
Bug introduced by
The type Crate\DBAL\Driver\PDOCrate\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
118
     *
119
     * @throws Exception
120
     */
121
    public function fetchAll(int $mode): array
122
    {
123
        return $this->stmt->fetchAll($mode);
0 ignored issues
show
Deprecated Code introduced by
The function Crate\PDO\PDOStatement::fetchAll() has been deprecated: Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
        return /** @scrutinizer ignore-deprecated */ $this->stmt->fetchAll($mode);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->stmt->fetchAll($mode) returns the type array<mixed,mixed> which is incompatible with the documented return type Crate\DBAL\Driver\PDOCrate\list.
Loading history...
124
    }
125
126 8
    public function fetchColumn($column_number = 0)
127
    {
128 8
        return $this->stmt->fetchColumn($column_number);
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function closeCursor(): bool
135
    {
136
        return $this->stmt->closeCursor();
137
    }
138
139
    /**
140
     * Gets the wrapped CrateDB PDOStatement.
141
     *
142
     * @return PDOStatement
143
     */
144 2
    public function getWrappedStatement(): PDOStatement
145
    {
146 2
        return $this->stmt;
147
    }
148
}
149