Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ parameters:
path: src/Query/Exec/MultiTableUpdateExecutor.php

-
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\.$#'
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\.$#'
identifier: argument.type
count: 1
path: src/Query/Exec/MultiTableUpdateExecutor.php
Expand Down Expand Up @@ -2610,12 +2610,6 @@ parameters:
count: 1
path: src/Query/Expr/Select.php

-
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\}\>\.$#'
identifier: assign.propertyType
count: 1
path: src/Query/Filter/SQLFilter.php

-
message: '#^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns int so it can be removed from the return type\.$#'
identifier: return.unusedType
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ parameters:
message: '~^Match expression does not handle remaining values:~'
path: src/Utility/PersisterHelper.php

# The return type is already narrow enough.
- '~^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns ''[a-z_]+'' so it can be removed from the return type\.$~'
- '~^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns Doctrine\\DBAL\\(?:Array)?ParameterType\:\:[A-Z_]+ so it can be removed from the return type\.$~'

# DBAL 4 compatibility
-
message: '~^Method Doctrine\\ORM\\Query\\AST\\Functions\\TrimFunction::getTrimMode\(\) never returns .* so it can be removed from the return type\.$~'
Expand Down
20 changes: 20 additions & 0 deletions src/Query/Filter/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query\Filter;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;

/** @internal */
final class Parameter
{
/** @param ParameterType::*|ArrayParameterType::*|string $type */
public function __construct(
public readonly mixed $value,
public readonly ParameterType|ArrayParameterType|int|string $type,
public readonly bool $isList,
) {
}
}
31 changes: 19 additions & 12 deletions src/Query/Filter/SQLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class SQLFilter implements Stringable
/**
* Parameters for the filter.
*
* @phpstan-var array<string,array{type: string, value: mixed, is_list: bool}>
* @phpstan-var array<string, Parameter>
*/
private array $parameters = [];

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

// Keep the parameters sorted for the hash
ksort($this->parameters);
Expand All @@ -71,11 +71,11 @@ final public function setParameterList(string $name, array $values, string $type
*/
final public function setParameter(string $name, mixed $value, string|null $type = null): static
{
if ($type === null) {
$type = ParameterTypeInferer::inferType($value);
}

$this->parameters[$name] = ['value' => $value, 'type' => $type, 'is_list' => false];
$this->parameters[$name] = new Parameter(
value: $value,
type: $type ?? ParameterTypeInferer::inferType($value),
isList: false,
);

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

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

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

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

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

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

$quoted = array_map(
static fn (mixed $value): string => $connection->quote((string) $value),
$param['value'],
$param->value,
);

return implode(',', $quoted);
Expand All @@ -152,7 +152,14 @@ final public function hasParameter(string $name): bool
*/
final public function __toString(): string
{
return serialize($this->parameters);
return serialize(array_map(
static fn (Parameter $value): array => [
'value' => $value->value,
'type' => $value->type,
'is_list' => $value->isList,
],
$this->parameters,
));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Query/ParameterTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
final class ParameterTypeInferer
{
/**
* Infers type of a given value, returning a compatible constant:
* - Type (\Doctrine\DBAL\Types\Type::*)
* - Connection (\Doctrine\DBAL\Connection::PARAM_*)
* Infers the type of a given value
*
* @return ParameterType::*|ArrayParameterType::*|Types::*
*/
public static function inferType(mixed $value): ParameterType|ArrayParameterType|int|string
{
Expand Down
Loading