Skip to content

Commit 99f23ff

Browse files
committed
[Mime] Deprecate Address::fromString()
1 parent 61d79e1 commit 99f23ff

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Symfony/Component/Mime/Address.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ public static function create($address): self
8989
return $address;
9090
}
9191
if (\is_string($address)) {
92-
return self::fromString($address);
92+
if (false === strpos($address, '<')) {
93+
return new self($address);
94+
}
95+
96+
if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) {
97+
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $address, static::class));
98+
}
99+
100+
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
93101
}
94102

95103
throw new InvalidArgumentException(sprintf('An address can be an instance of Address or a string ("%s" given).', get_debug_type($address)));
@@ -110,14 +118,19 @@ public static function createArray(array $addresses): array
110118
return $addrs;
111119
}
112120

121+
/**
122+
* @deprecated since Symfony 5.2, use "create()" instead.
123+
*/
113124
public static function fromString(string $string): self
114125
{
126+
trigger_deprecation('symfony/mime', '5.2', '"%s()" is deprecated, use "%s::create()" instead.', __METHOD__, __CLASS__);
127+
115128
if (false === strpos($string, '<')) {
116129
return new self($string, '');
117130
}
118131

119132
if (!preg_match(self::FROM_STRING_PATTERN, $string, $matches)) {
120-
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $string, static::class));
133+
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $string, self::class));
121134
}
122135

123136
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));

src/Symfony/Component/Mime/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* Deprecated `Address::fromString()`, use `Address::create()` instead
8+
49
4.4.0
510
-----
611

src/Symfony/Component/Mime/Tests/AddressTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ public function testCreate()
4444
$this->assertEquals($a, Address::create('fabien@symfony.com'));
4545
}
4646

47+
/**
48+
* @dataProvider fromStringProvider
49+
*/
50+
public function testCreateWithString($string, $displayName, $addrSpec)
51+
{
52+
$address = Address::create($string);
53+
$this->assertEquals($displayName, $address->getName());
54+
$this->assertEquals($addrSpec, $address->getAddress());
55+
$fromToStringAddress = Address::create($address->toString());
56+
$this->assertEquals($displayName, $fromToStringAddress->getName());
57+
$this->assertEquals($addrSpec, $fromToStringAddress->getAddress());
58+
}
59+
4760
public function testCreateWrongArg()
4861
{
4962
$this->expectException(\InvalidArgumentException::class);
@@ -81,6 +94,7 @@ public function nameEmptyDataProvider(): array
8194

8295
/**
8396
* @dataProvider fromStringProvider
97+
* @group legacy
8498
*/
8599
public function testFromString($string, $displayName, $addrSpec)
86100
{
@@ -92,6 +106,9 @@ public function testFromString($string, $displayName, $addrSpec)
92106
$this->assertEquals($addrSpec, $fromToStringAddress->getAddress());
93107
}
94108

109+
/**
110+
* @group legacy
111+
*/
95112
public function testFromStringFailure()
96113
{
97114
$this->expectException(InvalidArgumentException::class);

src/Symfony/Component/Mime/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1",
2021
"symfony/polyfill-intl-idn": "^1.10",
2122
"symfony/polyfill-mbstring": "^1.0",
2223
"symfony/polyfill-php80": "^1.15"

0 commit comments

Comments
 (0)