Skip to content

Commit 7cc2104

Browse files
authored
SQLFilter: replace internal array shape with class (#12232)
1 parent 4fd9e94 commit 7cc2104

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ parameters:
25632563
path: src/Query/Exec/MultiTableUpdateExecutor.php
25642564

25652565
-
2566-
message: '#^Parameter \#3 \$types of method Doctrine\\DBAL\\Connection\:\:executeStatement\(\) expects array\<int\<0, max\>\|string, Doctrine\\DBAL\\ArrayParameterType\|Doctrine\\DBAL\\ParameterType\|Doctrine\\DBAL\\Types\\Type\|string\>, list\<Doctrine\\DBAL\\ArrayParameterType\|Doctrine\\DBAL\\ParameterType\|Doctrine\\DBAL\\Types\\Type\|int\|string\> given\.$#'
2566+
message: '#^Parameter \#3 \$types of method Doctrine\\DBAL\\Connection\:\:executeStatement\(\) expects array\<int\<0, max\>\|string, Doctrine\\DBAL\\ArrayParameterType\|Doctrine\\DBAL\\ParameterType\|Doctrine\\DBAL\\Types\\Type\|string\>, list\<Doctrine\\DBAL\\ArrayParameterType\:\:ASCII\|Doctrine\\DBAL\\ArrayParameterType\:\:BINARY\|Doctrine\\DBAL\\ArrayParameterType\:\:INTEGER\|Doctrine\\DBAL\\ArrayParameterType\:\:STRING\|Doctrine\\DBAL\\ParameterType\:\:ASCII\|Doctrine\\DBAL\\ParameterType\:\:BINARY\|Doctrine\\DBAL\\ParameterType\:\:BOOLEAN\|Doctrine\\DBAL\\ParameterType\:\:INTEGER\|Doctrine\\DBAL\\ParameterType\:\:LARGE_OBJECT\|Doctrine\\DBAL\\ParameterType\:\:NULL\|Doctrine\\DBAL\\ParameterType\:\:STRING\|Doctrine\\DBAL\\Types\\Type\|int\|string\> given\.$#'
25672567
identifier: argument.type
25682568
count: 1
25692569
path: src/Query/Exec/MultiTableUpdateExecutor.php
@@ -2610,12 +2610,6 @@ parameters:
26102610
count: 1
26112611
path: src/Query/Expr/Select.php
26122612

2613-
-
2614-
message: '#^Property Doctrine\\ORM\\Query\\Filter\\SQLFilter\:\:\$parameters \(array\<string, array\{type\: string, value\: mixed, is_list\: bool\}\>\) does not accept non\-empty\-array\<string, array\{value\: mixed, type\: Doctrine\\DBAL\\ArrayParameterType\|Doctrine\\DBAL\\ParameterType\|int\|string, is_list\: bool\}\>\.$#'
2615-
identifier: assign.propertyType
2616-
count: 1
2617-
path: src/Query/Filter/SQLFilter.php
2618-
26192613
-
26202614
message: '#^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns int so it can be removed from the return type\.$#'
26212615
identifier: return.unusedType

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ parameters:
1212
message: '~^Match expression does not handle remaining values:~'
1313
path: src/Utility/PersisterHelper.php
1414

15+
# The return type is already narrow enough.
16+
- '~^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns ''[a-z_]+'' so it can be removed from the return type\.$~'
17+
- '~^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns Doctrine\\DBAL\\(?:Array)?ParameterType\:\:[A-Z_]+ so it can be removed from the return type\.$~'
18+
1519
# DBAL 4 compatibility
1620
-
1721
message: '~^Method Doctrine\\ORM\\Query\\AST\\Functions\\TrimFunction::getTrimMode\(\) never returns .* so it can be removed from the return type\.$~'

src/Query/Filter/Parameter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ORM\Query\Filter;
6+
7+
use Doctrine\DBAL\ArrayParameterType;
8+
use Doctrine\DBAL\ParameterType;
9+
10+
/** @internal */
11+
final class Parameter
12+
{
13+
/** @param ParameterType::*|ArrayParameterType::*|string $type */
14+
public function __construct(
15+
public readonly mixed $value,
16+
public readonly ParameterType|ArrayParameterType|int|string $type,
17+
public readonly bool $isList,
18+
) {
19+
}
20+
}

src/Query/Filter/SQLFilter.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class SQLFilter implements Stringable
2929
/**
3030
* Parameters for the filter.
3131
*
32-
* @phpstan-var array<string,array{type: string, value: mixed, is_list: bool}>
32+
* @phpstan-var array<string, Parameter>
3333
*/
3434
private array $parameters = [];
3535

@@ -49,7 +49,7 @@ final public function __construct(
4949
*/
5050
final public function setParameterList(string $name, array $values, string $type = Types::STRING): static
5151
{
52-
$this->parameters[$name] = ['value' => $values, 'type' => $type, 'is_list' => true];
52+
$this->parameters[$name] = new Parameter(value: $values, type: $type, isList: true);
5353

5454
// Keep the parameters sorted for the hash
5555
ksort($this->parameters);
@@ -71,11 +71,11 @@ final public function setParameterList(string $name, array $values, string $type
7171
*/
7272
final public function setParameter(string $name, mixed $value, string|null $type = null): static
7373
{
74-
if ($type === null) {
75-
$type = ParameterTypeInferer::inferType($value);
76-
}
77-
78-
$this->parameters[$name] = ['value' => $value, 'type' => $type, 'is_list' => false];
74+
$this->parameters[$name] = new Parameter(
75+
value: $value,
76+
type: $type ?? ParameterTypeInferer::inferType($value),
77+
isList: false,
78+
);
7979

8080
// Keep the parameters sorted for the hash
8181
ksort($this->parameters);
@@ -102,11 +102,11 @@ final public function getParameter(string $name): string
102102
throw new InvalidArgumentException("Parameter '" . $name . "' does not exist.");
103103
}
104104

105-
if ($this->parameters[$name]['is_list']) {
105+
if ($this->parameters[$name]->isList) {
106106
throw FilterException::cannotConvertListParameterIntoSingleValue($name);
107107
}
108108

109-
return $this->em->getConnection()->quote((string) $this->parameters[$name]['value']);
109+
return $this->em->getConnection()->quote((string) $this->parameters[$name]->value);
110110
}
111111

112112
/**
@@ -124,7 +124,7 @@ final public function getParameterList(string $name): string
124124
throw new InvalidArgumentException("Parameter '" . $name . "' does not exist.");
125125
}
126126

127-
if ($this->parameters[$name]['is_list'] === false) {
127+
if (! $this->parameters[$name]->isList) {
128128
throw FilterException::cannotConvertSingleParameterIntoListValue($name);
129129
}
130130

@@ -133,7 +133,7 @@ final public function getParameterList(string $name): string
133133

134134
$quoted = array_map(
135135
static fn (mixed $value): string => $connection->quote((string) $value),
136-
$param['value'],
136+
$param->value,
137137
);
138138

139139
return implode(',', $quoted);
@@ -152,7 +152,14 @@ final public function hasParameter(string $name): bool
152152
*/
153153
final public function __toString(): string
154154
{
155-
return serialize($this->parameters);
155+
return serialize(array_map(
156+
static fn (Parameter $value): array => [
157+
'value' => $value->value,
158+
'type' => $value->type,
159+
'is_list' => $value->isList,
160+
],
161+
$this->parameters,
162+
));
156163
}
157164

158165
/**

src/Query/ParameterTypeInferer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
final class ParameterTypeInferer
2626
{
2727
/**
28-
* Infers type of a given value, returning a compatible constant:
29-
* - Type (\Doctrine\DBAL\Types\Type::*)
30-
* - Connection (\Doctrine\DBAL\Connection::PARAM_*)
28+
* Infers the type of a given value
29+
*
30+
* @return ParameterType::*|ArrayParameterType::*|Types::*
3131
*/
3232
public static function inferType(mixed $value): ParameterType|ArrayParameterType|int|string
3333
{

0 commit comments

Comments
 (0)