Passed
Push — dbal ( 337422...2a3d8b )
by Greg
13:40
created

Query   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A distinct() 0 8 1
A __construct() 0 7 1
A firstRow() 0 3 1
A set() 0 13 6
A from() 0 8 1
A rows() 0 3 1
A pluck() 0 3 1
A first() 0 3 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB;
21
22
use Fisharebest\Webtrees\Arr;
23
use Fisharebest\Webtrees\DB;
24
use LogicException;
25
26
/**
27
 * Simplify constructor arguments for doctrine/dbal.
28
 *
29
 * @internal - use DB::select(), DB::update(), DB::insertInto(), DB::deleteFrom()
30
 */
31
final class Query
32
{
33
    /**
34
     * @param array<int|Expression,string|Expression> $columns
35
     */
36
    public function __construct(
37
        private readonly array $columns = [],
38
        private readonly bool $distinct = false,
39
        private readonly string $table = '',
40
        private readonly int $offset = 0,
41
        private readonly int $limit = 0,
42
    ) {
43
    }
44
45
    public function distinct(): self
46
    {
47
        return new self(
48
            columns: $this->columns,
49
            distinct: true,
50
            table: $this->table,
51
            offset: $this->offset,
52
            limit: $this->limit,
53
        );
54
    }
55
56
    /**
57
     * @param non-empty-string $table
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
58
     */
59
    public function from(string $table): self
60
    {
61
        return new self(
62
            columns: $this->columns,
63
            distinct: $this->distinct,
64
            table: DB::prefix($table),
65
            offset: $this->offset,
66
            limit: $this->limit,
67
        );
68
    }
69
70
    /**
71
     * This is an update query.  Return the count of updated rows.
72
     *
73
     * @param array<string,string|Expression> $updates
74
     *
75
     * @return int
76
     */
77
    public function set(array $updates): int
0 ignored issues
show
Unused Code introduced by
The parameter $updates is not used and could be removed. ( Ignorable by Annotation )

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

77
    public function set(/** @scrutinizer ignore-unused */ array $updates): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        if (
80
            $this->columns !== [] ||
81
            $this->distinct === true ||
82
            $this->table === '' ||
83
            $this->offset !== 0 ||
84
            $this->limit !== 0
85
        ) {
86
            throw new LogicException('Invalid SQL query definition');
87
        }
88
89
        return 0;
90
    }
91
92
    /**
93
     * @return Arr<int,object>
94
     */
95
    public function rows(): Arr
96
    {
97
        return new Arr();
98
    }
99
100
    /**
101
     * @return Arr<int|string,int|string>
102
     */
103
    public function pluck(): Arr
104
    {
105
        return new Arr();
106
    }
107
108
    public function firstRow(): object
109
    {
110
        return (object) [];
111
    }
112
113
    public function first(): string|int|float|null
114
    {
115
        return 0;
116
    }
117
}
118