From ef17dbe53786cee81b71253517427bbddb9e7501 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 26 Feb 2023 19:13:36 +0100 Subject: [PATCH] Use closures in portability converter --- psalm.xml.dist | 6 +++ src/Portability/Converter.php | 85 +++++++++++++++-------------------- 2 files changed, 43 insertions(+), 48 deletions(-) diff --git a/psalm.xml.dist b/psalm.xml.dist index 5fffb030256..e7d763547ac 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -221,12 +221,18 @@ + + + + + + diff --git a/src/Portability/Converter.php b/src/Portability/Converter.php index 4af5cad08d7..c645a58dcd3 100644 --- a/src/Portability/Converter.php +++ b/src/Portability/Converter.php @@ -4,6 +4,8 @@ namespace Doctrine\DBAL\Portability; +use Closure; + use function array_change_key_case; use function array_map; use function array_reduce; @@ -12,23 +14,12 @@ final class Converter { - /** @var callable */ - private $convertNumeric; - - /** @var callable */ - private $convertAssociative; - - /** @var callable */ - private $convertOne; - - /** @var callable */ - private $convertAllNumeric; - - /** @var callable */ - private $convertAllAssociative; - - /** @var callable */ - private $convertFirstColumn; + private readonly Closure $convertNumeric; + private readonly Closure $convertAssociative; + private readonly Closure $convertOne; + private readonly Closure $convertAllNumeric; + private readonly Closure $convertAllAssociative; + private readonly Closure $convertFirstColumn; /** * @param bool $convertEmptyStringToNull Whether each empty string should be converted to NULL @@ -42,13 +33,13 @@ public function __construct(bool $convertEmptyStringToNull, bool $rightTrimStrin $convertNumeric = $this->createConvertRow($convertValue, null); $convertAssociative = $this->createConvertRow($convertValue, $case); - $this->convertNumeric = $this->createConvert($convertNumeric, [self::class, 'id']); - $this->convertAssociative = $this->createConvert($convertAssociative, [self::class, 'id']); - $this->convertOne = $this->createConvert($convertValue, [self::class, 'id']); + $this->convertNumeric = $this->createConvert($convertNumeric); + $this->convertAssociative = $this->createConvert($convertAssociative); + $this->convertOne = $this->createConvert($convertValue); - $this->convertAllNumeric = $this->createConvertAll($convertNumeric, [self::class, 'id']); - $this->convertAllAssociative = $this->createConvertAll($convertAssociative, [self::class, 'id']); - $this->convertFirstColumn = $this->createConvertAll($convertValue, [self::class, 'id']); + $this->convertAllNumeric = $this->createConvertAll($convertNumeric); + $this->convertAllAssociative = $this->createConvertAll($convertAssociative); + $this->convertFirstColumn = $this->createConvertAll($convertValue); } /** @@ -157,18 +148,18 @@ private static function rightTrimString(mixed $value): mixed * @param bool $convertEmptyStringToNull Whether each empty string should be converted to NULL * @param bool $rightTrimString Whether each string should right-trimmed * - * @return callable|null The resulting function or NULL if no conversion is needed + * @return Closure|null The resulting function or NULL if no conversion is needed */ - private function createConvertValue(bool $convertEmptyStringToNull, bool $rightTrimString): ?callable + private function createConvertValue(bool $convertEmptyStringToNull, bool $rightTrimString): ?Closure { $functions = []; if ($convertEmptyStringToNull) { - $functions[] = [self::class, 'convertEmptyStringToNull']; + $functions[] = self::convertEmptyStringToNull(...); } if ($rightTrimString) { - $functions[] = [self::class, 'rightTrimString']; + $functions[] = self::rightTrimString(...); } return $this->compose(...$functions); @@ -177,12 +168,12 @@ private function createConvertValue(bool $convertEmptyStringToNull, bool $rightT /** * Creates a function that will convert each array-row retrieved from the database * - * @param callable|null $function The function that will convert each value - * @param int|null $case Column name case + * @param Closure|null $function The function that will convert each value + * @param int|null $case Column name case * - * @return callable|null The resulting function or NULL if no conversion is needed + * @return Closure|null The resulting function or NULL if no conversion is needed */ - private function createConvertRow(?callable $function, ?int $case): ?callable + private function createConvertRow(?Closure $function, ?int $case): ?Closure { $functions = []; @@ -191,9 +182,7 @@ private function createConvertRow(?callable $function, ?int $case): ?callable } if ($case !== null) { - $functions[] = static function (array $row) use ($case): array { - return array_change_key_case($row, $case); - }; + $functions[] = static fn (array $row): array => array_change_key_case($row, $case); } return $this->compose(...$functions); @@ -203,13 +192,12 @@ private function createConvertRow(?callable $function, ?int $case): ?callable * Creates a function that will be applied to the return value of Statement::fetch*() * or an identity function if no conversion is needed * - * @param callable|null $function The function that will convert each tow - * @param callable $id Identity function + * @param Closure|null $function The function that will convert each tow */ - private function createConvert(?callable $function, callable $id): callable + private function createConvert(?Closure $function): Closure { if ($function === null) { - return $id; + return self::id(...); } return /** @@ -232,13 +220,12 @@ static function (mixed $value) use ($function): mixed { * Creates a function that will be applied to the return value of Statement::fetchAll*() * or an identity function if no transformation is required * - * @param callable|null $function The function that will transform each value - * @param callable $id Identity function + * @param Closure|null $function The function that will transform each value */ - private function createConvertAll(?callable $function, callable $id): callable + private function createConvertAll(?Closure $function): Closure { if ($function === null) { - return $id; + return self::id(...); } return $this->createMapper($function); @@ -247,9 +234,11 @@ private function createConvertAll(?callable $function, callable $id): callable /** * Creates a function that maps each value of the array using the given function * - * @param callable $function The function that maps each value of the array + * @param Closure $function The function that maps each value of the array + * + * @return Closure(array):array */ - private function createMapper(callable $function): callable + private function createMapper(Closure $function): Closure { return static fn (array $array): array => array_map($function, $array); } @@ -257,15 +246,15 @@ private function createMapper(callable $function): callable /** * Creates a composition of the given set of functions * - * @param callable(T):T ...$functions The functions to compose + * @param Closure(T):T ...$functions The functions to compose * - * @return callable(T):T|null + * @return Closure(T):T|null * * @template T */ - private function compose(callable ...$functions): ?callable + private function compose(Closure ...$functions): ?Closure { - return array_reduce($functions, static function (?callable $carry, callable $item): callable { + return array_reduce($functions, static function (?Closure $carry, Closure $item): Closure { if ($carry === null) { return $item; }