Skip to content

Commit 2b58c73

Browse files
boulei_nNicochou
authored andcommitted
[Mime] add union types
Co-authored-by: Nicochou <[email protected]>
1 parent 6dfb174 commit 2b58c73

18 files changed

+48
-95
lines changed

Address.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,21 @@ public function getEncodedName(): string
9090
return sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName()));
9191
}
9292

93-
/**
94-
* @param Address|string $address
95-
*/
96-
public static function create($address): self
93+
public static function create(Address|string $address): self
9794
{
9895
if ($address instanceof self) {
9996
return $address;
10097
}
101-
if (\is_string($address)) {
102-
if (false === strpos($address, '<')) {
103-
return new self($address);
104-
}
10598

106-
if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) {
107-
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $address, self::class));
108-
}
99+
if (false === strpos($address, '<')) {
100+
return new self($address);
101+
}
109102

110-
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
103+
if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) {
104+
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $address, self::class));
111105
}
112106

113-
throw new InvalidArgumentException(sprintf('An address can be an instance of Address or a string ("%s" given).', get_debug_type($address)));
107+
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
114108
}
115109

116110
/**

Crypto/SMimeEncrypter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class SMimeEncrypter extends SMime
2626
* @param string|string[] $certificate The path (or array of paths) of the file(s) containing the X.509 certificate(s)
2727
* @param int|null $cipher A set of algorithms used to encrypt the message. Must be one of these PHP constants: https://www.php.net/manual/en/openssl.ciphers.php
2828
*/
29-
public function __construct($certificate, int $cipher = null)
29+
public function __construct(string|array $certificate, int $cipher = null)
3030
{
3131
if (!\extension_loaded('openssl')) {
3232
throw new \LogicException('PHP extension "openssl" is required to use SMime.');

Email.php

+12-36
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ public function getDate(): ?\DateTimeImmutable
7171
}
7272

7373
/**
74-
* @param Address|string $address
75-
*
7674
* @return $this
7775
*/
78-
public function returnPath($address)
76+
public function returnPath(Address|string $address)
7977
{
8078
return $this->setHeaderBody('Path', 'Return-Path', Address::create($address));
8179
}
@@ -86,11 +84,9 @@ public function getReturnPath(): ?Address
8684
}
8785

8886
/**
89-
* @param Address|string $address
90-
*
9187
* @return $this
9288
*/
93-
public function sender($address)
89+
public function sender(Address|string $address)
9490
{
9591
return $this->setHeaderBody('Mailbox', 'Sender', Address::create($address));
9692
}
@@ -101,21 +97,17 @@ public function getSender(): ?Address
10197
}
10298

10399
/**
104-
* @param Address|string ...$addresses
105-
*
106100
* @return $this
107101
*/
108-
public function addFrom(...$addresses)
102+
public function addFrom(Address|string ...$addresses)
109103
{
110104
return $this->addListAddressHeaderBody('From', $addresses);
111105
}
112106

113107
/**
114-
* @param Address|string ...$addresses
115-
*
116108
* @return $this
117109
*/
118-
public function from(...$addresses)
110+
public function from(Address|string ...$addresses)
119111
{
120112
return $this->setListAddressHeaderBody('From', $addresses);
121113
}
@@ -129,21 +121,17 @@ public function getFrom(): array
129121
}
130122

131123
/**
132-
* @param Address|string ...$addresses
133-
*
134124
* @return $this
135125
*/
136-
public function addReplyTo(...$addresses)
126+
public function addReplyTo(Address|string ...$addresses)
137127
{
138128
return $this->addListAddressHeaderBody('Reply-To', $addresses);
139129
}
140130

141131
/**
142-
* @param Address|string ...$addresses
143-
*
144132
* @return $this
145133
*/
146-
public function replyTo(...$addresses)
134+
public function replyTo(Address|string ...$addresses)
147135
{
148136
return $this->setListAddressHeaderBody('Reply-To', $addresses);
149137
}
@@ -157,21 +145,17 @@ public function getReplyTo(): array
157145
}
158146

159147
/**
160-
* @param Address|string ...$addresses
161-
*
162148
* @return $this
163149
*/
164-
public function addTo(...$addresses)
150+
public function addTo(Address|string ...$addresses)
165151
{
166152
return $this->addListAddressHeaderBody('To', $addresses);
167153
}
168154

169155
/**
170-
* @param Address|string ...$addresses
171-
*
172156
* @return $this
173157
*/
174-
public function to(...$addresses)
158+
public function to(Address|string ...$addresses)
175159
{
176160
return $this->setListAddressHeaderBody('To', $addresses);
177161
}
@@ -185,21 +169,17 @@ public function getTo(): array
185169
}
186170

187171
/**
188-
* @param Address|string ...$addresses
189-
*
190172
* @return $this
191173
*/
192-
public function addCc(...$addresses)
174+
public function addCc(Address|string ...$addresses)
193175
{
194176
return $this->addListAddressHeaderBody('Cc', $addresses);
195177
}
196178

197179
/**
198-
* @param Address|string ...$addresses
199-
*
200180
* @return $this
201181
*/
202-
public function cc(...$addresses)
182+
public function cc(Address|string ...$addresses)
203183
{
204184
return $this->setListAddressHeaderBody('Cc', $addresses);
205185
}
@@ -213,21 +193,17 @@ public function getCc(): array
213193
}
214194

215195
/**
216-
* @param Address|string ...$addresses
217-
*
218196
* @return $this
219197
*/
220-
public function addBcc(...$addresses)
198+
public function addBcc(Address|string ...$addresses)
221199
{
222200
return $this->addListAddressHeaderBody('Bcc', $addresses);
223201
}
224202

225203
/**
226-
* @param Address|string ...$addresses
227-
*
228204
* @return $this
229205
*/
230-
public function bcc(...$addresses)
206+
public function bcc(Address|string ...$addresses)
231207
{
232208
return $this->setListAddressHeaderBody('Bcc', $addresses);
233209
}

Header/DateHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(string $name, \DateTimeInterface $date)
3030
/**
3131
* @param \DateTimeInterface $body
3232
*/
33-
public function setBody($body)
33+
public function setBody(mixed $body)
3434
{
3535
$this->setDateTime($body);
3636
}

Header/HeaderInterface.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ interface HeaderInterface
2222
* Sets the body.
2323
*
2424
* The type depends on the Header concrete class.
25-
*
26-
* @param mixed $body
2725
*/
28-
public function setBody($body);
26+
public function setBody(mixed $body);
2927

3028
/**
3129
* Gets the body.

Header/Headers.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,25 @@ public function addMailboxListHeader(string $name, array $addresses): self
8585
}
8686

8787
/**
88-
* @param Address|string $address
89-
*
9088
* @return $this
9189
*/
92-
public function addMailboxHeader(string $name, $address): self
90+
public function addMailboxHeader(string $name, Address|string $address): self
9391
{
9492
return $this->add(new MailboxHeader($name, Address::create($address)));
9593
}
9694

9795
/**
98-
* @param string|array $ids
99-
*
10096
* @return $this
10197
*/
102-
public function addIdHeader(string $name, $ids): self
98+
public function addIdHeader(string $name, string|array $ids): self
10399
{
104100
return $this->add(new IdentificationHeader($name, $ids));
105101
}
106102

107103
/**
108-
* @param Address|string $path
109-
*
110104
* @return $this
111105
*/
112-
public function addPathHeader(string $name, $path): self
106+
public function addPathHeader(string $name, Address|string $path): self
113107
{
114108
return $this->add(new PathHeader($name, $path instanceof Address ? $path : new Address($path)));
115109
}
@@ -141,7 +135,7 @@ public function addParameterizedHeader(string $name, string $value, array $param
141135
/**
142136
* @return $this
143137
*/
144-
public function addHeader(string $name, $argument, array $more = []): self
138+
public function addHeader(string $name, mixed $argument, array $more = []): self
145139
{
146140
$parts = explode('\\', self::HEADER_CLASS_MAP[strtolower($name)] ?? UnstructuredHeader::class);
147141
$method = 'add'.ucfirst(array_pop($parts));
@@ -254,15 +248,15 @@ public function toArray(): array
254248
return $arr;
255249
}
256250

257-
public function getHeaderBody($name)
251+
public function getHeaderBody(string $name)
258252
{
259253
return $this->has($name) ? $this->get($name)->getBody() : null;
260254
}
261255

262256
/**
263257
* @internal
264258
*/
265-
public function setHeaderBody(string $type, string $name, $body): void
259+
public function setHeaderBody(string $type, string $name, mixed $body): void
266260
{
267261
if ($this->has($name)) {
268262
$this->get($name)->setBody($body);
@@ -288,7 +282,7 @@ public function getHeaderParameter(string $name, string $parameter): ?string
288282
/**
289283
* @internal
290284
*/
291-
public function setHeaderParameter(string $name, string $parameter, $value): void
285+
public function setHeaderParameter(string $name, string $parameter, ?string $value): void
292286
{
293287
if (!$this->has($name)) {
294288
throw new LogicException(sprintf('Unable to set parameter "%s" on header "%s" as the header is not defined.', $parameter, $name));

Header/IdentificationHeader.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@ final class IdentificationHeader extends AbstractHeader
2424
private $ids = [];
2525
private $idsAsAddresses = [];
2626

27-
/**
28-
* @param string|array $ids
29-
*/
30-
public function __construct(string $name, $ids)
27+
public function __construct(string $name, string|array $ids)
3128
{
3229
parent::__construct($name);
3330

3431
$this->setId($ids);
3532
}
3633

3734
/**
38-
* @param string|array $body a string ID or an array of IDs
35+
* @param string|string[] $body a string ID or an array of IDs
3936
*
4037
* @throws RfcComplianceException
4138
*/
42-
public function setBody($body)
39+
public function setBody(mixed $body)
4340
{
4441
$this->setId($body);
4542
}
@@ -52,11 +49,11 @@ public function getBody(): array
5249
/**
5350
* Set the ID used in the value of this header.
5451
*
55-
* @param string|array $id
52+
* @param string|string[] $id
5653
*
5754
* @throws RfcComplianceException
5855
*/
59-
public function setId($id)
56+
public function setId(string|array $id)
6057
{
6158
$this->setIds(\is_array($id) ? $id : [$id]);
6259
}

Header/MailboxHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(string $name, Address $address)
3535
*
3636
* @throws RfcComplianceException
3737
*/
38-
public function setBody($body)
38+
public function setBody(mixed $body)
3939
{
4040
$this->setAddress($body);
4141
}

Header/MailboxListHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(string $name, array $addresses)
3838
*
3939
* @throws RfcComplianceException
4040
*/
41-
public function setBody($body)
41+
public function setBody(mixed $body)
4242
{
4343
$this->setAddresses($body);
4444
}

Header/PathHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(string $name, Address $address)
3535
*
3636
* @throws RfcComplianceException
3737
*/
38-
public function setBody($body)
38+
public function setBody(mixed $body)
3939
{
4040
$this->setAddress($body);
4141
}

Header/UnstructuredHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(string $name, string $value)
3030
/**
3131
* @param string $body
3232
*/
33-
public function setBody($body)
33+
public function setBody(mixed $body)
3434
{
3535
$this->setValue($body);
3636
}

MimeTypeGuesserInterface.php

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public function isGuesserSupported(): bool;
2626
/**
2727
* Guesses the MIME type of the file with the given path.
2828
*
29-
* @param string $path The path to the file
30-
*
3129
* @return string|null The MIME type or null, if none could be guessed
3230
*
3331
* @throws \LogicException If the guesser is not supported

Part/DataPart.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public function __construct($body, string $filename = null, string $contentType
3939

4040
parent::__construct($body, null, $subtype, $encoding);
4141

42-
$this->filename = $filename;
43-
$this->setName($filename);
42+
if (null !== $filename) {
43+
$this->filename = $filename;
44+
$this->setName($filename);
45+
}
4446
$this->setDisposition('attachment');
4547
}
4648

Part/Multipart/FormDataPart.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function prepareFields(array $fields): array
8383
return $values;
8484
}
8585

86-
private function preparePart(string $name, $value): TextPart
86+
private function preparePart(string $name, string|TextPart $value): TextPart
8787
{
8888
if (\is_string($value)) {
8989
return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));

0 commit comments

Comments
 (0)