Issues (265)

src/Query/ActiveQuery.php (2 issues)

Labels
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\Query;
13
14
use Cycle\Database\Driver\DriverInterface;
15
use Cycle\Database\Exception\BuilderException;
16
use Cycle\Database\Exception\StatementException;
17
18
/**
19
 * QueryBuilder classes generate set of control tokens for query compilers, this is query level
20
 * abstraction.
21
 *
22
 * @internal
23
 */
24
abstract class ActiveQuery implements QueryInterface, \Stringable
25
{
26
    protected ?DriverInterface $driver = null;
27
    protected ?string $prefix = null;
28
29
    public function withDriver(DriverInterface $driver, ?string $prefix = null): QueryInterface
30 72
    {
31
        $query = clone $this;
32 72
        $query->driver = $driver;
33
        $query->prefix = $prefix;
34 72
35 72
        return $query;
36 72
    }
37
38
    public function getDriver(): ?DriverInterface
39
    {
40 8
        return $this->driver;
41
    }
42 8
43
    public function getPrefix(): ?string
44
    {
45 8
        return $this->prefix;
46
    }
47
48
    /**
49
     * Generate SQL query, must have associated driver instance.
50
     *
51 8
     * @psalm-return non-empty-string
52 8
     */
53 8
    public function sqlStatement(?QueryParameters $parameters = null): string
54
    {
55
        $this->driver === null and throw new BuilderException('Unable to build query without associated driver');
56
57 2178
        return $this->driver->getQueryCompiler()->compile(
0 ignored issues
show
The method getQueryCompiler() 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

57
        return $this->driver->/** @scrutinizer ignore-call */ getQueryCompiler()->compile(

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...
58
            $parameters ?? new QueryParameters(),
59 2178
            $this->prefix,
0 ignored issues
show
It seems like $this->prefix can also be of type null; however, parameter $prefix of Cycle\Database\Driver\CompilerInterface::compile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
            /** @scrutinizer ignore-type */ $this->prefix,
Loading history...
60 2178
            $this,
61 2178
        );
62
    }
63 2178
64
    /**
65
     * Compile and run query.
66 8
     *
67
     * @throws BuilderException
68 8
     * @throws StatementException
69
     */
70
    abstract public function run(): mixed;
71 112
72
    public function __toString(): string
73 112
    {
74
        $parameters = new QueryParameters();
75
76
        return Interpolator::interpolate(
77
            $this->sqlStatement($parameters),
78
            $parameters->getParameters(),
79
        );
80
    }
81 2014
82
    public function __debugInfo(): array
83 2014
    {
84
        $parameters = new QueryParameters();
85 2014
86 2014
        try {
87 2014
            $queryString = $this->sqlStatement($parameters);
88
        } catch (\Throwable $e) {
89
            $queryString = "[ERROR: {$e->getMessage()}]";
90
        }
91
92
        return [
93
            'queryString' => Interpolator::interpolate($queryString, $parameters->getParameters()),
94
            'parameters'  => $parameters->getParameters(),
95
            'driver'      => $this->driver,
96
        ];
97
    }
98
99
    /**
100
     * Helper methods used to correctly fetch and split identifiers provided by function
101
     * parameters. Example: fI(['name, email']) => 'name', 'email'
102
     */
103
    protected function fetchIdentifiers(array $identifiers): array
104 1950
    {
105
        if (\count($identifiers) === 1 && \is_string($identifiers[0])) {
106 1950
            return \array_map('trim', \explode(',', $identifiers[0]));
107 674
        }
108
109
        if (\count($identifiers) === 1 && \is_array($identifiers[0])) {
110 1934
            return $identifiers[0];
111 1904
        }
112
113
        return $identifiers;
114 62
    }
115
}
116