Passed
Push — dbal ( 620c4a...2c143b )
by Greg
23:32 queued 07:20
created

Query   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A firstRow() 0 3 1
A set() 0 13 6
A from() 0 8 1
A rows() 0 3 1
A distinct() 0 8 1
A pluck() 0 3 1
A __construct() 0 7 1
A first() 0 3 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2024 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
    public function from(string $table): self
57
    {
58
        return new self(
59
            columns: $this->columns,
60
            distinct: $this->distinct,
61
            table: DB::prefix($table),
62
            offset: $this->offset,
63
            limit: $this->limit,
64
        );
65
    }
66
67
    /**
68
     * This is an update query.  Return the count of updated rows.
69
     *
70
     * @param array<string,string|Expression> $updates
71
     *
72
     * @return int
73
     */
74
    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

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