Passed
Pull Request — 2.x (#231)
by
unknown
20:48
created

SQLServerUpsertQuery::returning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\SQLServer\Query;
13
14
use Cycle\Database\Driver\DriverInterface;
15
use Cycle\Database\Driver\SQLServer\SQLServerDriver;
16
use Cycle\Database\Exception\BuilderException;
17
use Cycle\Database\Exception\ReadonlyConnectionException;
18
use Cycle\Database\Injection\FragmentInterface;
19
use Cycle\Database\Query\QueryParameters;
20
use Cycle\Database\Query\ReturningInterface;
21
use Cycle\Database\Query\UpsertQuery;
22
use Cycle\Database\Query\QueryInterface;
23
use Cycle\Database\StatementInterface;
24
25
class SQLServerUpsertQuery extends UpsertQuery implements ReturningInterface
26
{
27
    /**
28
     * @var SQLServerDriver|null
29
     */
30
    protected ?DriverInterface $driver = null;
31
32
    /**
33
     * @var list<FragmentInterface|non-empty-string>
0 ignored issues
show
Bug introduced by
The type Cycle\Database\Driver\SQLServer\Query\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...
34
     */
35
    protected array $returningColumns = [];
36
37
    public function withDriver(DriverInterface $driver, ?string $prefix = null): QueryInterface
38
    {
39
        $driver instanceof SQLServerDriver or throw new BuilderException(
40
            'SQLServer UpsertQuery can be used only with SQLServer driver',
41
        );
42
43
        return parent::withDriver($driver, $prefix);
44
    }
45
46
    public function returning(string|FragmentInterface ...$columns): self
47
    {
48
        $columns === [] and throw new BuilderException('RETURNING clause should contain at least 1 column.');
49
50
        $this->returningColumns = \array_values($columns);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($columns) of type array is incompatible with the declared type Cycle\Database\Driver\SQLServer\Query\list of property $returningColumns.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
52
        return $this;
53
    }
54
55
    public function run(): mixed
56
    {
57
        if ($this->returningColumns === []) {
58
            return parent::run();
59
        }
60
61
        $params = new QueryParameters();
62
        $queryString = $this->sqlStatement($params);
63
64
        $this->driver->isReadonly() and throw ReadonlyConnectionException::onWriteStatementExecution();
0 ignored issues
show
Bug introduced by
The method isReadonly() does not exist on null. ( Ignorable by Annotation )

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

64
        $this->driver->/** @scrutinizer ignore-call */ 
65
                       isReadonly() and throw ReadonlyConnectionException::onWriteStatementExecution();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
        $result = $this->driver->query($queryString, $params->getParameters());
67
68
        try {
69
            if (\count($this->returningColumns) === 1) {
70
                return $result->fetchColumn();
71
            }
72
            return $result->fetch(StatementInterface::FETCH_ASSOC);
73
        } finally {
74
            $result->close();
75
        }
76
    }
77
78
    public function getTokens(): array
79
    {
80
        return parent::getTokens() + [
81
            'return' => $this->returningColumns,
82
        ];
83
    }
84
}
85