8000 bug #33275 [Intl] make polyfill classes abstract, fix edge case (nico… · symfony/symfony@9475b2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9475b2e

Browse files
bug #33275 [Intl] make polyfill classes abstract, fix edge case (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Intl] make polyfill classes abstract, fix edge case | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - While working on return types, I keep stumbling on those classes that are `@internal`, but for which we must not add return types, because they're polyfills and are extended by stubs found in the `Resources` folder of the component. Making the polyfills abstract fixes the linting issue. This made me discover we have a glitch in the `getPattern()` implementation, that makes our version diverge from intl's. Fixed here too. On 4.4 because let's not disrupt 3.4. Commits ------- c757b95 [Intl] make polyfill classes abstract, fix edge case
2 parents 7046cac + c757b95 commit 9475b2e

File tree

8 files changed

+47
-48
lines changed

8 files changed

+47
-48
lines changed

src/Symfony/Component/Intl/Collator/Collator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @internal
3535
*/
36-
class Collator
36+
abstract class Collator
3737
{
3838
/* Attribute constants */
3939
const FRENCH_COLLATION = 0;
@@ -86,13 +86,13 @@ public function __construct(?string $locale)
8686
*
8787
* @param string|null $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en")
8888
*
89-
* @return self
89+
* @return static
9090
*
9191
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
9292
*/
9393
public static function create($locale)
9494
{
95-
return new self($locale);
95+
return new static($locale);
9696
}
9797

9898
/**

src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*
4747
* @internal
4848
*/
49-
class IntlDateFormatter
49+
abstract class IntlDateFormatter
5050
{
5151
/**
5252
* The error code from the last operation.
@@ -78,9 +78,9 @@ class IntlDateFormatter
7878
*/
7979
private $defaultDateFormats = [
8080
self::NONE => '',
81-
self::FULL => 'EEEE, LLLL d, y',
82-
self::LONG => 'LLLL d, y',
83-
self::MEDIUM => 'LLL d, y',
81+
self::FULL => 'EEEE, MMMM d, y',
82+
self::LONG => 'MMMM d, y',
83+
self::MEDIUM => 'MMM d, y',
8484
self::SHORT => 'M/d/yy',
8585
];
8686

@@ -160,7 +160,7 @@ public function __construct(?string $locale, ?int $datetype, ?int $timetype, $ti
160160
* One of the calendar constants
161161
* @param string|null $pattern Optional pattern to use when formatting
162162
*
163-
* @return self
163+
* @return static
164164
*
165165
* @see https://php.net/intldateformatter.create
166166
* @see http://userguide.icu-project.org/formatparse/datetime
@@ -170,7 +170,7 @@ public function __construct(?string $locale, ?int $datetype, ?int $timetype, $ti
170170
*/
171171
public static function create($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
172172
{
173-
return new self($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
173+
return new static($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
174174
}
175175

176176
/**
@@ -600,14 +600,19 @@ protected function createDateTime($timestamp)
600600
*/
601601
protected function getDefaultPattern()
602602
{
603-
$patternParts = [];
603+
$pattern = '';
604604
if (self::NONE !== $this->datetype) {
605-
$patternParts[] = $this->defaultDateFormats[$this->datetype];
605+
$pattern = $this->defaultDateFormats[$this->datetype];
606606
}
607607
if (self::NONE !== $this->timetype) {
608-
$patternParts[] = $this->defaultTimeFormats[$this->timetype];
608+
if (self::FULL === $this->datetype || self::LONG === $this->datetype) {
609+
$pattern .= ' \'at\' ';
610+
} elseif (self::NONE !== $this->datetype) {
611+
$pattern .= ', ';
612+
}
613+
$pattern .= $this->defaultTimeFormats[$this->timetype];
609614
}
610615

611-
return implode(', ', $patternParts);
616+
return $pattern;
612617
}
613618
}

src/Symfony/Component/Intl/Locale/Locale.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @internal
2626
*/
27-
class Locale
27+
abstract class Locale
2828
{
2929
const DEFAULT_LOCALE = null;
3030

src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* @internal
4242
*/
43-
class NumberFormatter
43+
abstract class NumberFormatter
4444
{
4545
/* Format style constants */
4646
const PATTERN_DECIMAL = 0;
@@ -286,7 +286,7 @@ public function __construct(?string $locale = 'en', int $style = null, $pattern
286286
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax
287287
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation
288288
*
289-
* @return self
289+
* @return static
290290
*
291291
* @see https://php.net/numberformatter.create
292292
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
@@ -298,7 +298,7 @@ public function __construct(?string $locale = 'en', int $style = null, $pattern
298298
*/
299299
public static function create($locale = 'en', $style = null, $pattern = null)
300300
{
301-
return new self($locale, $style, $pattern);
301+
return new static($locale, $style, $pattern);
302302
}
303303

304304
/**

src/Symfony/Component/Intl/Tests/Collator/CollatorTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CollatorTest extends AbstractCollatorTest
1919
public function testConstructorWithUnsupportedLocale()
2020
{
2121
$this->expectException('Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException');
22-
new Collator('pt_BR');
22+
$this->getCollator('pt_BR');
2323
}
2424

2525
public function testCompare()
@@ -90,12 +90,14 @@ public function testSetStrength()
9090

9191
public function testStaticCreate()
9292
{
93-
$collator = Collator::create('en');
93+
$collator = $this->getCollator('en');
94+
$collator = $collator::create('en');
9495
$this->assertInstanceOf('\Symfony\Component\Intl\Collator\Collator', $collator);
9596
}
9697

9798
protected function getCollator($locale)
9899
{
99-
return new Collator($locale);
100+
return new class($locale) extends Collator {
101+
};
100102
}
101103
}

src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ protected function tearDown(): void
4343

4444
/**
4545
* When a time zone is not specified, it uses the system default however it returns null in the getter method.
46-
*
47-
* @see StubIntlDateFormatterTest::testDefaultTimeZoneIntl()
4846
*/
4947
public function testConstructorDefaultTimeZone()
5048
{
@@ -60,14 +58,14 @@ public function testConstructorDefaultTimeZone()
6058

6159
public function testConstructorWithoutDateType()
6260
{
63-
$formatter = new IntlDateFormatter('en', null, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN);
61+
$formatter = $this->getDateFormatter('en', null, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN);
6462

65-
$this->assertSame('EEEE, LLLL d, y, h:mm a', $formatter->getPattern());
63+
$this->assertSame('EEEE, MMMM d, y \'at\' h:mm a', $formatter->getPattern());
6664
}
6765

6866
public function testConstructorWithoutTimeType()
6967
{
70-
$formatter = new IntlDateFormatter('en', IntlDateFormatter::SHORT, null, 'UTC', IntlDateFormatter::GREGORIAN);
68+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::SHORT, null, 'UTC', IntlDateFormatter::GREGORIAN);
7169

7270
$this->assertSame('M/d/yy, h:mm:ss a zzzz', $formatter->getPattern());
7371
}
@@ -963,14 +961,7 @@ protected function assertIsIntlSuccess($formatter, $errorMessage, $errorCode)
963961
}
964962

965963
/**
966-
* @param $locale
967-
* @param $datetype
968-
* @param $timetype
969-
* @param null $timezone
970-
* @param int $calendar
971-
* @param null $pattern
972-
*
973-
* @return mixed
964+
* @return IntlDateFormatter|\IntlDateFormatter
974965
*/
975966
abstract protected function getDateFormatter($locale, $datetype, $timetype, $timezone = null, $calendar = IntlDateFormatter::GREGORIAN, $pattern = null);
976967

src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,32 @@ class IntlDateFormatterTest extends AbstractIntlDateFormatterTest
1818
{
1919
public function testConstructor()
2020
{
21-
$formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
21+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
2222
$this->assertEquals('y-M-d', $formatter->getPattern());
2323
}
2424

2525
public function testConstructorWithoutLocale()
2626
{
27-
$formatter = new IntlDateFormatter(null, IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
27+
$formatter = $this->getDateFormatter(null, IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
2828
$this->assertEquals('y-M-d', $formatter->getPattern());
2929
}
3030

3131
public function testConstructorWithoutCalendar()
3232
{
33-
$formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', null, 'y-M-d');
33+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', null, 'y-M-d');
3434
$this->assertEquals('y-M-d', $formatter->getPattern());
3535
}
3636

3737
public function testConstructorWithUnsupportedLocale()
3838
{
3939
$this->expectException('Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException');
40-
new IntlDateFormatter('pt_BR', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
40+
$this->getDateFormatter('pt_BR', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
4141
}
4242

4343
public function testStaticCreate()
4444
{
45-
$formatter = IntlDateFormatter::create('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
45+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
46+
$formatter = $formatter::create('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
4647
$this->assertInstanceOf('\Symfony\Component\Intl\DateFormatter\IntlDateFormatter', $formatter);
4748
}
4849

@@ -75,7 +76,7 @@ public function testFormatWithUnimplementedChars()
7576
{
7677
$this->expectException('Symfony\Component\Intl\Exception\NotImplementedException');
7778
$pattern = 'Y';
78-
$formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, $pattern);
79+
$formatter = $this->getDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, $pattern);
7980
$formatter->format(0);
8081
}
8182

@@ -178,7 +179,8 @@ public function testParseThreeDigitsYears()
178179

179180
protected function getDateFormatter($locale, $datetype, $timetype, $timezone = null, $calendar = IntlDateFormatter::GREGORIAN, $pattern = null)
180181
{
181-
return new IntlDateFormatter($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
182+
return new class($locale, $datetype, $timetype, $timezone, $calendar, $pattern) extends IntlDateFormatter {
183+
};
182184
}
183185

184186
protected function getIntlErrorMessage()

src/Symfony/Component/Intl/Tests/NumberFormatter/NumberFormatterTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ class NumberFormatterTest extends AbstractNumberFormatterTest
2323
public function testConstructorWithUnsupportedLocale()
2424
{
2525
$this->expectException('Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException');
26-
new NumberFormatter('pt_BR');
26+
$this->getNumberFormatter('pt_BR');
2727
}
2828

2929
public function testConstructorWithUnsupportedStyle()
3030
{
3131
$this->expectException('Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException');
32-
new NumberFormatter('en', NumberFormatter::PATTERN_DECIMAL);
32+
$this->getNumberFormatter('en', NumberFormatter::PATTERN_DECIMAL);
3333
}
3434

3535
public f E40E unction testConstructorWithPatternDifferentThanNull()
3636
{
3737
$this->expectException('Symfony\Component\Intl\Exception\MethodArgumentNotImplementedException');
38-
new NumberFormatter('en', NumberFormatter::DECIMAL, '');
38+
$this->getNumberFormatter('en', NumberFormatter::DECIMAL, '');
3939
}
4040

4141
public function testSetAttributeWithUnsupportedAttribute()
@@ -62,10 +62,8 @@ public function testConstructWithoutLocale()
6262

6363
public function testCreate()
6464
{
65-
$this->assertInstanceOf(
66-
'\Symfony\Component\Intl\NumberFormatter\NumberFormatter',
67-
NumberFormatter::create('en', NumberFormatter::DECIMAL)
68-
);
65+
$formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
66+
$this->assertInstanceOf(NumberFormatter::class, $formatter::create('en', NumberFormatter::DECIMAL));
6967
}
7068

7169
public function testFormatWithCurrencyStyle()
@@ -171,7 +169,8 @@ public function testSetTextAttribute()
171169

172170
protected function getNumberFormatter($locale = 'en', $style = null, $pattern = null)
173171
{
174-
return new NumberFormatter($locale, $style, $pattern);
172+
return new class($locale, $style, $pattern) extends NumberFormatter {
173+
};
175174
}
176175

177176
protected function getIntlErrorMessage()

0 commit comments

Comments
 (0)
0