From 2c9b283e01ce8d8fc5fb25b31615a79df96e2ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ro=C3=9Fkamp?= Date: Wed, 14 Oct 2015 12:34:59 +0200 Subject: [PATCH] [Form] Simplify DateTimeToStringTransformer Avoid unneeded catch and re-throw of the same exception. --- .../DateTimeToStringTransformer.php | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php index fdcfeee8ec..80c31a725c 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php @@ -90,7 +90,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer * Transforms a DateTime object into a date string with the configured format * and timezone. * - * @param \DateTime|\DateTimeInterface $dateTime A DateTime object + * @param \DateTime|\DateTimeInterface $value A DateTime object * * @return string A value as produced by PHP's date() function * @@ -142,21 +142,21 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer throw new TransformationFailedException('Expected a string.'); } + $outputTz = new \DateTimeZone($this->outputTimezone); + $dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz); + + $lastErrors = \DateTime::getLastErrors(); + + if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) { + throw new TransformationFailedException( + implode(', ', array_merge( + array_values($lastErrors['warnings']), + array_values($lastErrors['errors']) + )) + ); + } + try { - $outputTz = new \DateTimeZone($this->outputTimezone); - $dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz); - - $lastErrors = \DateTime::getLastErrors(); - - if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) { - throw new TransformationFailedException( - implode(', ', array_merge( - array_values($lastErrors['warnings']), - array_values($lastErrors['errors']) - )) - ); - } - // On PHP versions < 5.3.7 we need to emulate the pipe operator // and reset parts not given in the format to their equivalent // of the UNIX base timestamp. @@ -224,8 +224,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer if ($this->inputTimezone !== $this->outputTimezone) { $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); } - } catch (TransformationFailedException $e) { - throw $e; } catch (\Exception $e) { throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); }