Skip to content

Commit 7553603

Browse files
committed
Add native types to SQLFilter
1 parent 1712e3c commit 7553603

File tree

5 files changed

+58
-170
lines changed

5 files changed

+58
-170
lines changed

lib/Doctrine/ORM/Query/Filter/SQLFilter.php

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,28 @@
2525
*/
2626
abstract class SQLFilter
2727
{
28-
/**
29-
* The entity manager.
30-
*
31-
* @var EntityManagerInterface
32-
*/
33-
private $em;
34-
3528
/**
3629
* Parameters for the filter.
3730
*
3831
* @psalm-var array<string,array{type: string, value: mixed, is_list: bool}>
3932
*/
40-
private $parameters = [];
33+
private array $parameters = [];
4134

42-
/**
43-
* @param EntityManagerInterface $em The entity manager.
44-
*/
45-
final public function __construct(EntityManagerInterface $em)
46-
{
47-
$this->em = $em;
35+
final public function __construct(
36+
private EntityManagerInterface $em
37+
) {
4838
}
4939

5040
/**
5141
* Sets a parameter list that can be used by the filter.
5242
*
53-
* @param string $name Name of the parameter.
5443
* @param array<mixed> $values List of parameter values.
5544
* @param string $type The parameter type. If specified, the given value will be run through
5645
* the type conversion of this type.
5746
*
5847
* @return $this
5948
*/
60-
final public function setParameterList(string $name, array $values, string $type = Types::STRING): self
49+
final public function setParameterList(string $name, array $values, string $type = Types::STRING): static
6150
{
6251
$this->parameters[$name] = ['value' => $values, 'type' => $type, 'is_list' => true];
6352

@@ -73,15 +62,13 @@ final public function setParameterList(string $name, array $values, string $type
7362
/**
7463
* Sets a parameter that can be used by the filter.
7564
*
76-
* @param string $name Name of the parameter.
77-
* @param mixed $value Value of the parameter.
78-
* @param string|null $type The parameter type. If specified, the given value will be run through
79-
* the type conversion of this type. This is usually not needed for
80-
* strings and numeric types.
65+
* @param string|null $type The parameter type. If specified, the given value will be run through
66+
* the type conversion of this type. This is usually not needed for
67+
* strings and numeric types.
8168
*
8269
* @return $this
8370
*/
84-
final public function setParameter($name, $value, $type = null): self
71+
final public function setParameter(string $name, mixed $value, ?string $type = null): static
8572
{
8673
if ($type === null) {
8774
$type = ParameterTypeInferer::inferType($value);
@@ -104,13 +91,11 @@ final public function setParameter($name, $value, $type = null): self
10491
* The function is responsible for the right output escaping to use the
10592
* value in a query.
10693
*
107-
* @param string $name Name of the parameter.
108-
*
10994
* @return string The SQL escaped parameter to use in a query.
11095
*
11196
* @throws InvalidArgumentException
11297
*/
113-
final public function getParameter($name)
98+
final public function getParameter(string $name): string
11499
{
115100
if (! isset($this->parameters[$name])) {
116101
throw new InvalidArgumentException("Parameter '" . $name . "' does not exist.");
@@ -132,10 +117,6 @@ final public function getParameter($name)
132117
* value in a query, separating each entry by comma to inline it into
133118
* an IN() query part.
134119
*
135-
* @param string $name Name of the parameter.
136-
*
137-
* @return string The SQL escaped parameter to use in a query.
138-
*
139120
* @throws InvalidArgumentException
140121
*/
141122
final public function getParameterList(string $name): string
@@ -151,31 +132,26 @@ final public function getParameterList(string $name): string
151132
$param = $this->parameters[$name];
152133
$connection = $this->em->getConnection();
153134

154-
$quoted = array_map(static function ($value) use ($connection, $param) {
155-
return $connection->quote($value, $param['type']);
156-
}, $param['value']);
135+
$quoted = array_map(
136+
static fn (mixed $value): string => (string) $connection->quote($value, $param['type']),
137+
$param['value']
138+
);
157139

158140
return implode(',', $quoted);
159141
}
160142

161143
/**
162144
* Checks if a parameter was set for the filter.
163-
*
164-
* @param string $name Name of the parameter.
165-
*
166-
* @return bool
167145
*/
168-
final public function hasParameter($name)
146+
final public function hasParameter(string $name): bool
169147
{
170148
return isset($this->parameters[$name]);
171149
}
172150

173151
/**
174152
* Returns as string representation of the SQLFilter parameters (the state).
175-
*
176-
* @return string String representation of the SQLFilter.
177153
*/
178-
final public function __toString()
154+
final public function __toString(): string
179155
{
180156
return serialize($this->parameters);
181157
}
@@ -191,10 +167,9 @@ final protected function getConnection(): Connection
191167
/**
192168
* Gets the SQL query part to add to a query.
193169
*
194-
* @param string $targetTableAlias
195170
* @psalm-param ClassMetadata<object> $targetEntity
196171
*
197172
* @return string The constraint SQL if there is available, empty string otherwise.
198173
*/
199-
abstract public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias);
174+
abstract public function addFilterConstraint(ClassMetadata $targetEntity, string $targetTableAlias): string;
200175
}

lib/Doctrine/ORM/Query/FilterCollection.php

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,70 +29,47 @@ class FilterCollection
2929
*/
3030
public const FILTERS_STATE_DIRTY = 2;
3131

32-
/**
33-
* The used Configuration.
34-
*
35-
* @var Configuration
36-
*/
37-
private $config;
38-
39-
/**
40-
* The EntityManager that "owns" this FilterCollection instance.
41-
*
42-
* @var EntityManagerInterface
43-
*/
44-
private $em;
32+
private Configuration $config;
4533

4634
/**
4735
* Instances of enabled filters.
4836
*
49-
* @var SQLFilter[]
50-
* @psalm-var array<string, SQLFilter>
37+
* @var array<string, SQLFilter>
5138
*/
52-
private $enabledFilters = [];
39+
private array $enabledFilters = [];
5340

54-
/**
55-
* The filter hash from the last time the query was parsed.
56-
*
57-
* @var string
58-
*/
59-
private $filterHash = '';
41+
/** The filter hash from the last time the query was parsed. */
42+
private string $filterHash = '';
6043

6144
/**
6245
* The current state of this filter.
6346
*
64-
* @var int
6547
* @psalm-var self::FILTERS_STATE_*
6648
*/
67-
private $filtersState = self::FILTERS_STATE_CLEAN;
49+
private int $filtersState = self::FILTERS_STATE_CLEAN;
6850

69-
public function __construct(EntityManagerInterface $em)
70-
{
71-
$this->em = $em;
51+
public function __construct(
52+
private EntityManagerInterface $em
53+
) {
7254
$this->config = $em->getConfiguration();
7355
}
7456

7557
/**
7658
* Gets all the enabled filters.
7759
*
78-
* @return SQLFilter[] The enabled filters.
79-
* @psalm-return array<string, SQLFilter>
60+
* @return array<string, SQLFilter> The enabled filters.
8061
*/
81-
public function getEnabledFilters()
62+
public function getEnabledFilters(): array
8263
{
8364
return $this->enabledFilters;
8465
}
8566

8667
/**
8768
* Enables a filter from the collection.
8869
*
89-
* @param string $name Name of the filter.
90-
*
91-
* @return SQLFilter The enabled filter.
92-
*
9370
* @throws InvalidArgumentException If the filter does not exist.
9471
*/
95-
public function enable($name)
72+
public function enable(string $name): SQLFilter
9673
{
9774
if (! $this->has($name)) {
9875
throw new InvalidArgumentException("Filter '" . $name . "' does not exist.");
@@ -117,13 +94,9 @@ public function enable($name)
11794
/**
11895
* Disables a filter.
11996
*
120-
* @param string $name Name of the filter.
121-
*
122-
* @return SQLFilter The disabled filter.
123-
*
12497
* @throws InvalidArgumentException If the filter does not exist.
12598
*/
126-
public function disable($name)
99+
public function disable(string $name): SQLFilter
127100
{
128101
// Get the filter to return it
129102
$filter = $this->getFilter($name);
@@ -138,13 +111,9 @@ public function disable($name)
138111
/**
139112
* Gets an enabled filter from the collection.
140113
*
141-
* @param string $name Name of the filter.
142-
*
143-
* @return SQLFilter The filter.
144-
*
145114
* @throws InvalidArgumentException If the filter is not enabled.
146115
*/
147-
public function getFilter($name)
116+
public function getFilter(string $name): SQLFilter
148117
{
149118
if (! $this->isEnabled($name)) {
150119
throw new InvalidArgumentException("Filter '" . $name . "' is not enabled.");
@@ -155,44 +124,32 @@ public function getFilter($name)
155124

156125
/**
157126
* Checks whether filter with given name is defined.
158-
*
159-
* @param string $name Name of the filter.
160-
*
161-
* @return bool true if the filter exists, false if not.
162127
*/
163-
public function has($name)
128+
public function has(string $name): bool
164129
{
165130
return $this->config->getFilterClassName($name) !== null;
166131
}
167132

168133
/**
169134
* Checks if a filter is enabled.
170-
*
171-
* @param string $name Name of the filter.
172-
*
173-
* @return bool True if the filter is enabled, false otherwise.
174135
*/
175-
public function isEnabled($name)
136+
public function isEnabled(string $name): bool
176137
{
177138
return isset($this->enabledFilters[$name]);
178139
}
179140

180141
/**
181142
* Checks if the filter collection is clean.
182-
*
183-
* @return bool
184143
*/
185-
public function isClean()
144+
public function isClean(): bool
186145
{
187146
return $this->filtersState === self::FILTERS_STATE_CLEAN;
188147
}
189148

190149
/**
191150
* Generates a string of currently enabled filters to use for the cache id.
192-
*
193-
* @return string
194151
*/
195-
public function getHash()
152+
public function getHash(): string
196153
{
197154
// If there are only clean filters, the previous hash can be returned
198155
if ($this->filtersState === self::FILTERS_STATE_CLEAN) {
@@ -213,10 +170,8 @@ public function getHash()
213170

214171
/**
215172
* Sets the filter state to dirty.
216-
*
217-
* @return void
218173
*/
219-
public function setFiltersStateDirty()
174+
public function setFiltersStateDirty(): void
220175
{
221176
$this->filtersState = self::FILTERS_STATE_DIRTY;
222177
}

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,12 +1837,6 @@
18371837
</NonInvariantDocblockPropertyType>
18381838
</file>
18391839
<file src="lib/Doctrine/ORM/Query/Filter/SQLFilter.php">
1840-
<MissingClosureParamType occurrences="1">
1841-
<code>$value</code>
1842-
</MissingClosureParamType>
1843-
<MissingClosureReturnType occurrences="1">
1844-
<code>static function ($value) use ($connection, $param) {</code>
1845-
</MissingClosureReturnType>
18461840
<PropertyTypeCoercion occurrences="1">
18471841
<code>$this-&gt;parameters</code>
18481842
</PropertyTypeCoercion>

0 commit comments

Comments
 (0)