Passed
Push — master ( 3e0ae5...66dbef )
by Melech
01:47 queued 20s
created

SqlQueryBuilder::withAddedWhere()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\QueryBuilder\Abstract;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
17
use Valkyrja\Orm\Constant\Statement;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Orm\Constant\Statement 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...
18
use Valkyrja\Orm\Data\Join;
19
use Valkyrja\Orm\Data\Where;
20
use Valkyrja\Orm\Data\WhereGroup;
21
use Valkyrja\Orm\QueryBuilder\Contract\QueryBuilder as Contract;
22
23
/**
24
 * Class SqlQueryBuilder.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
abstract class SqlQueryBuilder implements Contract
29
{
30
    /** @var string */
31
    protected string $alias = '';
32
    /** @var Join[] */
33
    protected array $joins = [];
34
    /** @var array<Where|WhereGroup> */
35
    protected array $where = [];
36
37
    /**
38
     * @param non-empty-string $from The 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...
39
     */
40
    public function __construct(
41
        protected string $from,
42
    ) {
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    #[Override]
49
    public function withFrom(string $table): static
50
    {
51
        $new = clone $this;
52
53
        $new->from = $table;
54
55
        return $new;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    #[Override]
62
    public function withAlias(string $alias): static
63
    {
64
        $new = clone $this;
65
66
        $new->alias = $alias;
67
68
        return $new;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    #[Override]
75
    public function withJoin(Join ...$joins): static
76
    {
77
        $new = clone $this;
78
79
        $new->joins = $joins;
80
81
        return $new;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    #[Override]
88
    public function withAddedJoin(Join ...$joins): static
89
    {
90
        $new = clone $this;
91
92
        $new->joins = array_merge($new->joins, $joins);
93
94
        return $new;
95
    }
96
97
    #[Override]
98
    public function withWhere(Where|WhereGroup ...$where): static
99
    {
100
        $new = clone $this;
101
102
        $new->where = $where;
103
104
        return $new;
105
    }
106
107
    #[Override]
108
    public function withAddedWhere(Where|WhereGroup ...$where): static
109
    {
110
        $new = clone $this;
111
112
        $new->where = array_merge($new->where, $where);
113
114
        return $new;
115
    }
116
117
    /**
118
     * Get the alias of a query statement.
119
     *
120
     * @return string
121
     */
122
    protected function getAliasQuery(): string
123
    {
124
        return $this->alias === ''
125
            ? ''
126
            : " $this->alias";
127
    }
128
129
    /**
130
     * Get the where of a query statement.
131
     *
132
     * @return string
133
     */
134
    protected function getWhereQuery(): string
135
    {
136
        return empty($this->where)
137
            ? ''
138
            : ' ' . Statement::WHERE . ' ' . implode(' ', $this->where);
139
    }
140
141
    /**
142
     * Get the joins of a query statement.
143
     *
144
     * @return string
145
     */
146
    protected function getJoinQuery(): string
147
    {
148
        return empty($this->joins)
149
            ? ''
150
            : ' ' . implode(' ', $this->joins);
151
    }
152
}
153