Skip to content

Commit

Permalink
Merge branch '4.3' into 4.4
Browse files Browse the repository at this point in the history
* 4.3:
  [Intl] use strict comparisons
  Fix s-maxage=3 transient test
  • Loading branch information
nicolas-grekas committed Aug 8, 2019
2 parents e0ccbf6 + 38f08ba commit cf57007
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
Expand Up @@ -660,15 +660,15 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
$this->assertTraceContains('miss');
$this->assertTraceContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));

$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));

// expires the cache
$values = $this->getMetaStorageValues();
Expand All @@ -688,7 +688,7 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
$this->assertTraceContains('invalid');
$this->assertTraceContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));

$this->setNextResponse();

Expand All @@ -698,7 +698,7 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
}

public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpiredWithStatus304()
Expand All @@ -711,7 +711,7 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
$this->assertTraceContains('miss');
$this->assertTraceContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));

$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
Expand Down Expand Up @@ -739,15 +739,15 @@ public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAft
$this->assertTraceContains('store');
$this->assertTraceNotContains('miss');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));

$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
$this->assertRegExp('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
}

public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
Expand Down
Expand Up @@ -26,7 +26,7 @@ class Hour1200Transformer extends HourTransformer
public function format(\DateTime $dateTime, int $length): string
{
$hourOfDay = $dateTime->format('g');
$hourOfDay = '12' == $hourOfDay ? '0' : $hourOfDay;
$hourOfDay = '12' === $hourOfDay ? '0' : $hourOfDay;

return $this->padLeft($hourOfDay, $length);
}
Expand Down
Expand Up @@ -33,9 +33,11 @@ public function format(\DateTime $dateTime, int $length): string
*/
public function normalizeHour(int $hour, string $marker = null): int
{
if ('AM' == $marker) {
$marker = (string) $marker;

if ('AM' === $marker) {
$hour = 0;
} elseif ('PM' == $marker) {
} elseif ('PM' === $marker) {
$hour = 12;
}

Expand Down
Expand Up @@ -26,7 +26,7 @@ class Hour2401Transformer extends HourTransformer
public function format(\DateTime $dateTime, int $length): string
{
$hourOfDay = $dateTime->format('G');
$hourOfDay = ('0' == $hourOfDay) ? '24' : $hourOfDay;
$hourOfDay = '0' === $hourOfDay ? '24' : $hourOfDay;

return $this->padLeft($hourOfDay, $length);
}
Expand All @@ -36,7 +36,7 @@ public function format(\DateTime $dateTime, int $length): string
*/
public function normalizeHour(int $hour, string $marker = null): int
{
if ((null === $marker && 24 === $hour) || 'AM' == $marker) {
if ((null === $marker && 24 == $hour) || 'AM' == $marker) {
$hour = 0;
} elseif ('PM' == $marker) {
$hour = 12;
Expand Down
Expand Up @@ -102,7 +102,7 @@ public static function getEtcTimeZoneId($formattedTimeZone)
if (preg_match('/GMT(?P<signal>[+-])(?P<hours>\d{2}):?(?P<minutes>\d{2})/', $formattedTimeZone, $matches)) {
$hours = (int) $matches['hours'];
$minutes = (int) $matches['minutes'];
$signal = '-' == $matches['signal'] ? '+' : '-';
$signal = '-' === $matches['signal'] ? '+' : '-';

if (0 < $minutes) {
throw new NotImplementedException(sprintf('It is not possible to use a GMT time zone with minutes offset different than zero (0). GMT time zone tried: %s.', $formattedTimeZone));
Expand Down
58 changes: 33 additions & 25 deletions src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php
Expand Up @@ -272,19 +272,19 @@ public function __construct(?string $locale = 'en', int $style = null, $pattern
throw new MethodArgumentNotImplementedException(__METHOD__, 'pattern');
}

$this->style = $style;
$this->style = null !== $style ? (int) $style : null;
}

/**
* Static constructor.
*
* @param string $locale The locale code. The only supported locale is "en" (or null using the default locale, i.e. "en")
* @param int $style Style of the formatting, one of the format style constants.
* The only currently supported styles are NumberFormatter::DECIMAL
* and NumberFormatter::CURRENCY.
* @param string $pattern Not supported. A pattern string in case $style is NumberFormat::PATTERN_DECIMAL or
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation
* @param string|null $locale The locale code. The only supported locale is "en" (or null using the default locale, i.e. "en")
* @param int $style Style of the formatting, one of the format style constants.
* The only currently supported styles are NumberFormatter::DECIMAL
* and NumberFormatter::CURRENCY.
* @param string $pattern Not supported. A pattern string in case $style is NumberFormat::PATTERN_DECIMAL or
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation
*
* @return self
*
Expand Down Expand Up @@ -314,7 +314,7 @@ public static function create($locale = 'en', $style = null, $pattern = null)
*/
public function formatCurrency($value, $currency)
{
if (self::DECIMAL == $this->style) {
if (self::DECIMAL === $this->style) {
return $this->format($value);
}

Expand Down Expand Up @@ -353,19 +353,21 @@ public function formatCurrency($value, $currency)
*/
public function format($value, $type = self::TYPE_DEFAULT)
{
$type = (int) $type;

// The original NumberFormatter does not support this format type
if (self::TYPE_CURRENCY == $type) {
if (self::TYPE_CURRENCY === $type) {
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING);

return false;
}

if (self::CURRENCY == $this->style) {
if (self::CURRENCY === $this->style) {
throw new NotImplementedException(sprintf('%s() method does not support the formatting of currencies (instance with CURRENCY style). %s', __METHOD__, NotImplementedException::INTL_INSTALL_MESSAGE));
}

// Only the default type is supported.
if (self::TYPE_DEFAULT != $type) {
if (self::TYPE_DEFAULT !== $type) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'type', $type, 'Only TYPE_DEFAULT is supported');
}

Expand All @@ -385,7 +387,7 @@ public function format($value, $type = self::TYPE_DEFAULT)
*
* @param int $attr An attribute specifier, one of the numeric attribute constants
*
* @return bool|int The attribute value on success or false on error
* @return int|false The attribute value on success or false on error
*
* @see https://php.net/numberformatter.getattribute
*/
Expand Down Expand Up @@ -438,7 +440,7 @@ public function getLocale($type = Locale::ACTUAL_LOCALE)
/**
* Not supported. Returns the formatter's pattern.
*
* @return bool|string The pattern string used by the formatter or false on error
* @return string|false The pattern string used by the formatter or false on error
*
* @see https://php.net/numberformatter.getpattern
*
Expand All @@ -454,7 +456,7 @@ public function getPattern()
*
* @param int $attr A symbol specifier, one of the format symbol constants
*
* @return bool|string The symbol value or false on error
* @return string|false The symbol value or false on error
*
* @see https://php.net/numberformatter.getsymbol
*/
Expand All @@ -468,7 +470,7 @@ public function getSymbol($attr)
*
* @param int $attr An attribute specifier, one of the text attribute constants
*
* @return bool|string The attribute value or false on error
* @return string|false The attribute value or false on error
*
* @see https://php.net/numberformatter.gettextattribute
*/
Expand All @@ -484,7 +486,7 @@ public function getTextAttribute($attr)
* @param string $currency Parameter to receive the currency name (reference)
* @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended
*
* @return bool|string The parsed numeric value or false on error
* @return float|false The parsed numeric value or false on error
*
* @see https://php.net/numberformatter.parsecurrency
*
Expand All @@ -508,7 +510,9 @@ public function parseCurrency($value, &$currency, &$position = null)
*/
public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
{
if (self::TYPE_DEFAULT == $type || self::TYPE_CURRENCY == $type) {
$type = (int) $type;

if (self::TYPE_DEFAULT === $type || self::TYPE_CURRENCY === $type) {
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING);

return false;
Expand Down Expand Up @@ -565,6 +569,8 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
*/
public function setAttribute($attr, $value)
{
$attr = (int) $attr;

if (!\in_array($attr, self::$supportedAttributes)) {
$message = sprintf(
'The available attributes are: %s',
Expand All @@ -574,7 +580,7 @@ public function setAttribute($attr, $value)
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'attr', $value, $message);
}

if (self::$supportedAttributes['ROUNDING_MODE'] == $attr && $this->isInvalidRoundingMode($value)) {
if (self::$supportedAttributes['ROUNDING_MODE'] === $attr && $this->isInvalidRoundingMode($value)) {
$message = sprintf(
'The supported values for ROUNDING_MODE are: %s',
implode(', ', array_keys(self::$roundingModes))
Expand All @@ -583,11 +589,11 @@ public function setAttribute($attr, $value)
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'attr', $value, $message);
}

if (self::$supportedAttributes['GROUPING_USED'] == $attr) {
if (self::$supportedAttributes['GROUPING_USED'] === $attr) {
$value = $this->normalizeGroupingUsedValue($value);
}

if (self::$supportedAttributes['FRACTION_DIGITS'] == $attr) {
if (self::$supportedAttributes['FRACTION_DIGITS'] === $attr) {
$value = $this->normalizeFractionDigitsValue($value);
if ($value < 0) {
// ignore negative values but do not raise an error
Expand Down Expand Up @@ -751,7 +757,7 @@ private function formatNumber($value, int $precision): string
*/
private function getUninitializedPrecision($value, int $precision): int
{
if (self::CURRENCY == $this->style) {
if (self::CURRENCY === $this->style) {
return $precision;
}

Expand Down Expand Up @@ -782,11 +788,13 @@ private function isInitializedAttribute(string $attr): bool
*/
private function convertValueDataType($value, int $type)
{
if (self::TYPE_DOUBLE == $type) {
$type = (int) $type;

if (self::TYPE_DOUBLE === $type) {
$value = (float) $value;
} elseif (self::TYPE_INT32 == $type) {
} elseif (self::TYPE_INT32 === $type) {
$value = $this->getInt32Value($value);
} elseif (self::TYPE_INT64 == $type) {
} elseif (self::TYPE_INT64 === $type) {
$value = $this->getInt64Value($value);
}

Expand Down

0 comments on commit cf57007

Please sign in to comment.