From 8b08294b636b034ec613dbe791772e5253fe8f1b Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 11 May 2023 10:20:44 +0200 Subject: [PATCH] Remove unnecessary usages of DateTime --- .github/expected-missing-return-types.diff | 29 ++++++++++++------- UPGRADE-6.4.md | 7 +++++ .../RememberMe/DoctrineTokenProvider.php | 11 +++---- ...octrineDataCollectorWithDebugStackTest.php | 1 + .../Tests/Middleware/Debug/MiddlewareTest.php | 2 +- .../RememberMe/DoctrineTokenProviderTest.php | 4 +-- .../FrameworkBundle/Command/AboutCommand.php | 2 +- .../views/Collector/logger.html.twig | 2 +- .../DateTimeToArrayTransformer.php | 5 +--- ...ateTimeToHtml5LocalDateTimeTransformer.php | 5 +--- .../DateTimeToRfc3339Transformer.php | 5 +--- .../DateTimeToStringTransformer.php | 5 +--- .../Form/Extension/Core/Type/DateType.php | 2 +- .../Validator/ValidatorTypeGuesser.php | 6 ++++ .../Traits/DateTimeEqualsTrait.php | 2 +- .../Validator/ValidatorTypeGuesserTest.php | 1 + .../HttpFoundation/BinaryFileResponse.php | 2 +- .../Component/HttpFoundation/CHANGELOG.md | 5 ++++ .../Component/HttpFoundation/HeaderBag.php | 8 +++-- .../Component/HttpFoundation/Response.php | 23 +++++---------- .../HttpFoundation/Tests/HeaderBagTest.php | 2 +- .../HttpCache/ResponseCacheStrategyTest.php | 12 ++++---- src/Symfony/Component/Mailer/README.md | 2 +- .../Component/Mime/Header/DateHeader.php | 8 ++--- .../Component/Mime/Tests/EmailTest.php | 2 +- .../Mime/Tests/Header/DateHeaderTest.php | 4 +-- .../Mime/Tests/Header/HeadersTest.php | 2 +- .../Trigger/CronExpressionTrigger.php | 2 +- .../Tests/LoginLink/LoginLinkHandlerTest.php | 2 +- .../Normalizer/DateTimeNormalizer.php | 18 ++++++------ .../Tests/Encoder/XmlEncoderTest.php | 10 +++---- .../Normalizer/DateTimeNormalizerTest.php | 22 +++++++------- .../Features/CallbacksTestTrait.php | 8 ++--- .../Features/ContextMetadataTestTrait.php | 14 ++++----- .../Serializer/Tests/SerializerTest.php | 8 ++--- .../Validator/ConstraintValidator.php | 2 +- .../AbstractComparisonValidator.php | 12 +++----- .../Constraints/DateTimeValidator.php | 4 +-- .../Validator/Constraints/RangeValidator.php | 16 ++++------ .../AbstractComparisonValidatorTestCase.php | 2 +- .../Tests/Constraints/IsNullValidatorTest.php | 2 +- .../Tests/Constraints/RangeValidatorTest.php | 17 +++++------ .../Validator/Tests/Constraints/TypeTest.php | 4 +-- .../Tests/Fixtures/datetime-legacy.php | 2 +- .../VarExporter/Tests/Fixtures/datetime.php | 2 +- .../VarExporter/Tests/VarExporterTest.php | 2 +- .../Component/Yaml/Tests/InlineTest.php | 4 +-- 47 files changed, 152 insertions(+), 160 deletions(-) create mode 100644 UPGRADE-6.4.md diff --git a/.github/expected-missing-return-types.diff b/.github/expected-missing-return-types.diff index 2f04b85fcaf6..ee8e05bd837b 100644 --- a/.github/expected-missing-return-types.diff +++ b/.github/expected-missing-return-types.diff @@ -231,7 +231,7 @@ index 38618fc15e..eb599eb0b4 100644 { $this->clearEntityManagers(); diff --git a/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php b/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php -index 9d61be61bd..e89985de26 100644 +index 3af51ffd60..2743b97f31 100644 --- a/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php +++ b/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php @@ -70,5 +70,5 @@ class DoctrineTokenProvider implements TokenProviderInterface, TokenVerifierInte @@ -6182,7 +6182,7 @@ index 32c58447cd..ec35f6ee41 100644 { $compound = static fn (Options $options) => 'single_text' !== $options['widget']; diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php -index 3b68dc241a..474139a6ee 100644 +index 80023affcb..d6051f52d1 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -47,5 +47,5 @@ class DateType extends AbstractType @@ -7352,7 +7352,7 @@ index a87171d2ca..cec8a6f027 100644 { if ($this->client instanceof ResetInterface) { diff --git a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php -index cd716e590e..68a0479ef4 100644 +index 71e806fc15..d60290b3ed 100644 --- a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php +++ b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php @@ -362,5 +362,5 @@ class BinaryFileResponse extends Response @@ -7399,7 +7399,7 @@ index b74a02e2e1..51b4f7f1c3 100644 { foreach ($files as $key => $file) { diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php -index c49a239726..440f3297de 100644 +index 3128a1d833..6721bbe974 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -90,5 +90,5 @@ class HeaderBag implements \IteratorAggregate, \Countable @@ -7430,21 +7430,28 @@ index c49a239726..440f3297de 100644 + public function remove(string $key): void { $key = strtr($key, self::UPPER, self::LOWER); -@@ -214,5 +214,5 @@ class HeaderBag implements \IteratorAggregate, \Countable +@@ -198,5 +198,5 @@ class HeaderBag implements \IteratorAggregate, \Countable + * @throws \RuntimeException When the HTTP header is not parseable + */ +- public function getDate(string $key, \DateTimeInterface $default = null): ?\DateTimeInterface ++ public function getDate(string $key, \DateTimeInterface $default = null): ?\DateTimeImmutable + { + if (null === $value = $this->get($key)) { +@@ -216,5 +216,5 @@ class HeaderBag implements \IteratorAggregate, \Countable * @return void */ - public function addCacheControlDirective(string $key, bool|string $value = true) + public function addCacheControlDirective(string $key, bool|string $value = true): void { $this->cacheControl[$key] = $value; -@@ -242,5 +242,5 @@ class HeaderBag implements \IteratorAggregate, \Countable +@@ -244,5 +244,5 @@ class HeaderBag implements \IteratorAggregate, \Countable * @return void */ - public function removeCacheControlDirective(string $key) + public function removeCacheControlDirective(string $key): void { unset($this->cacheControl[$key]); -@@ -270,5 +270,5 @@ class HeaderBag implements \IteratorAggregate, \Countable +@@ -272,5 +272,5 @@ class HeaderBag implements \IteratorAggregate, \Countable * @return string */ - protected function getCacheControlHeader() @@ -12907,7 +12914,7 @@ index d53bbb196f..4eecf85f8e 100644 { return self::PROPERTY_CONSTRAINT; diff --git a/src/Symfony/Component/Validator/ConstraintValidator.php b/src/Symfony/Component/Validator/ConstraintValidator.php -index 8fa522314f..a4c0c5ec64 100644 +index 3c4167be33..59e5b3c09e 100644 --- a/src/Symfony/Component/Validator/ConstraintValidator.php +++ b/src/Symfony/Component/Validator/ConstraintValidator.php @@ -40,5 +40,5 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface @@ -12998,7 +13005,7 @@ index 1fdbf0bc3f..aad702453b 100644 + public function remove(int $offset): void; } diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php -index 29fd4daa8a..c358e6cc6f 100644 +index a81d589f2b..87155e513c 100644 --- a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -38,5 +38,5 @@ abstract class AbstractComparisonValidator extends ConstraintValidator @@ -13170,7 +13177,7 @@ index a50ea62ab6..5f5b5b4e5a 100644 { if (!$constraint instanceof Currency) { diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php -index f02ba54daf..e89c616ec8 100644 +index c88732d4d8..4c0e11eacf 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -25,5 +25,5 @@ class DateTimeValidator extends DateValidator @@ -13455,7 +13462,7 @@ index 3f8f951280..503e779244 100644 { if (!$constraint instanceof NotNull) { diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php -index 18c399cd08..c98312a9aa 100644 +index 73273d6788..f8d0945f23 100644 --- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php @@ -35,5 +35,5 @@ class RangeValidator extends ConstraintValidator diff --git a/UPGRADE-6.4.md b/UPGRADE-6.4.md new file mode 100644 index 000000000000..a82dd446f7b9 --- /dev/null +++ b/UPGRADE-6.4.md @@ -0,0 +1,7 @@ +UPGRADE FROM 6.3 to 6.4 +======================= + +HttpFoundation +-------------- + + * Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable` diff --git a/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php b/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php index 9d61be61bd3a..3af51ffd60de 100644 --- a/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php +++ b/src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php @@ -89,12 +89,12 @@ public function updateToken(string $series, #[\SensitiveParameter] string $token $sql = 'UPDATE rememberme_token SET value=:value, lastUsed=:lastUsed WHERE series=:series'; $paramValues = [ 'value' => $tokenValue, - 'lastUsed' => $lastUsed, + 'lastUsed' => \DateTimeImmutable::createFromInterface($lastUsed), 'series' => $series, ]; $paramTypes = [ 'value' => ParameterType::STRING, - 'lastUsed' => Types::DATETIME_MUTABLE, + 'lastUsed' => Types::DATETIME_IMMUTABLE, 'series' => ParameterType::STRING, ]; if (method_exists($this->conn, 'executeStatement')) { @@ -118,14 +118,14 @@ public function createNewToken(PersistentTokenInterface $token) 'username' => $token->getUserIdentifier(), 'series' => $token->getSeries(), 'value' => $token->getTokenValue(), - 'lastUsed' => $token->getLastUsed(), + 'lastUsed' => \DateTimeImmutable::createFromInterface($token->getLastUsed()), ]; $paramTypes = [ 'class' => ParameterType::STRING, 'username' => ParameterType::STRING, 'series' => ParameterType::STRING, 'value' => ParameterType::STRING, - 'lastUsed' => Types::DATETIME_MUTABLE, + 'lastUsed' => Types::DATETIME_IMMUTABLE, ]; if (method_exists($this->conn, 'executeStatement')) { $this->conn->executeStatement($sql, $paramValues, $paramTypes); @@ -186,6 +186,7 @@ public function updateExistingToken(PersistentTokenInterface $token, #[\Sensitiv $this->conn->beginTransaction(); try { $this->deleteTokenBySeries($tmpSeries); + $lastUsed = \DateTime::createFromInterface($lastUsed); $this->createNewToken(new PersistentToken($token->getClass(), $token->getUserIdentifier(), $tmpSeries, $token->getTokenValue(), $lastUsed)); $this->conn->commit(); @@ -220,7 +221,7 @@ private function addTableToSchema(Schema $schema): void $table = $schema->createTable('rememberme_token'); $table->addColumn('series', Types::STRING, ['length' => 88]); $table->addColumn('value', Types::STRING, ['length' => 88]); - $table->addColumn('lastUsed', Types::DATETIME_MUTABLE); + $table->addColumn('lastUsed', Types::DATETIME_IMMUTABLE); $table->addColumn('class', Types::STRING, ['length' => 100]); $table->addColumn('username', Types::STRING, ['length' => 200]); $table->setPrimaryKey(['series']); diff --git a/src/Symfony/Bridge/Doctrine/Tests/DataCollector/DoctrineDataCollectorWithDebugStackTest.php b/src/Symfony/Bridge/Doctrine/Tests/DataCollector/DoctrineDataCollectorWithDebugStackTest.php index 64bee1203b78..9cdb0c67f3d9 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/DataCollector/DoctrineDataCollectorWithDebugStackTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/DataCollector/DoctrineDataCollectorWithDebugStackTest.php @@ -115,6 +115,7 @@ public static function paramProvider(): array [true, [], true, true], [null, [], null, true], [new \DateTime('2011-09-11'), ['date'], '2011-09-11', true], + [new \DateTimeImmutable('2011-09-11'), ['date_immutable'], '2011-09-11', true], [fopen(__FILE__, 'r'), [], '/* Resource(stream) */', false, false], [ new \stdClass(), diff --git a/src/Symfony/Bridge/Doctrine/Tests/Middleware/Debug/MiddlewareTest.php b/src/Symfony/Bridge/Doctrine/Tests/Middleware/Debug/MiddlewareTest.php index 2bb628442459..20d90a4c9fdd 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Middleware/Debug/MiddlewareTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Middleware/Debug/MiddlewareTest.php @@ -126,7 +126,7 @@ public function testWithValueBound(callable $executeMethod) $stmt->bindValue(3, 5, ParameterType::INTEGER); $stmt->bindValue(4, $res = $this->getResourceFromString('mydata'), ParameterType::BINARY); $stmt->bindValue(5, ['foo', 'bar'], Types::SIMPLE_ARRAY); - $stmt->bindValue(6, new \DateTime('2022-06-12 11:00:00'), Types::DATETIME_MUTABLE); + $stmt->bindValue(6, new \DateTimeImmutable('2022-06-12 11:00:00'), Types::DATETIME_IMMUTABLE); $executeMethod($stmt); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Security/RememberMe/DoctrineTokenProviderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Security/RememberMe/DoctrineTokenProviderTest.php index 9d8b9256f4c9..e85ed662a642 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Security/RememberMe/DoctrineTokenProviderTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Security/RememberMe/DoctrineTokenProviderTest.php @@ -77,7 +77,7 @@ public function testVerifyOutdatedTokenAfterParallelRequest() $provider->createNewToken($token); // new request comes in requiring remember-me auth, which updates the token - $provider->updateExistingToken($token, $newValue, new \DateTime('-5 seconds')); + $provider->updateExistingToken($token, $newValue, new \DateTimeImmutable('-5 seconds')); $provider->updateToken($series, $newValue, new \DateTime('-5 seconds')); // parallel request comes in with the old remember-me cookie and session, which also requires reauth @@ -102,7 +102,7 @@ public function testVerifyOutdatedTokenAfterParallelRequestFailsAfter60Seconds() $provider->createNewToken($token); // new request comes in requiring remember-me auth, which updates the token - $provider->updateExistingToken($token, $newValue, new \DateTime('-61 seconds')); + $provider->updateExistingToken($token, $newValue, new \DateTimeImmutable('-61 seconds')); $provider->updateToken($series, $newValue, new \DateTime('-5 seconds')); // parallel request comes in with the old remember-me cookie and session, which also requires reauth diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php index 4f68228ee520..d0a62b4cdb2c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int ['Version', \PHP_VERSION], ['Architecture', (\PHP_INT_SIZE * 8).' bits'], ['Intl locale', class_exists(\Locale::class, false) && \Locale::getDefault() ? \Locale::getDefault() : 'n/a'], - ['Timezone', date_default_timezone_get().' ('.(new \DateTime())->format(\DateTime::W3C).')'], + ['Timezone', date_default_timezone_get().' ('.(new \DateTimeImmutable())->format(\DateTimeInterface::W3C).')'], ['OPcache', \extension_loaded('Zend OPcache') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL) ? 'true' : 'false'], ['APCu', \extension_loaded('apcu') && filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOL) ? 'true' : 'false'], ['Xdebug', \extension_loaded('xdebug') ? 'true' : 'false'], diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/logger.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/logger.html.twig index 78de5763f761..c2fc5dd323ee 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/logger.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/logger.html.twig @@ -375,7 +375,7 @@ %} -