Skip to content

Commit cbe2de8

Browse files
committed
Adjust types in lib/VCardConverter.php
1 parent f456d35 commit cbe2de8

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

lib/VCardConverter.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabre\VObject;
44

5+
use Sabre\VObject\Component\VCard;
56
use Sabre\VObject\Property\Binary;
67
use Sabre\VObject\Property\Uri;
78

@@ -29,6 +30,10 @@ class VCardConverter
2930
*
3031
* If input and output version are identical, a clone is returned.
3132
*
33+
* @param VCard<int, mixed> $input
34+
*
35+
* @return VCard<int, mixed>
36+
*
3237
* @throws InvalidDataException
3338
*/
3439
public function convert(Component\VCard $input, int $targetVersion): Component\VCard
@@ -64,6 +69,10 @@ public function convert(Component\VCard $input, int $targetVersion): Component\V
6469
/**
6570
* Handles conversion of a single property.
6671
*
72+
* @param VCard<int, mixed> $input
73+
* @param VCard<int, mixed> $output
74+
* @param Property<mixed, mixed> $property
75+
*
6776
* @throws InvalidDataException
6877
*/
6978
protected function convertProperty(Component\VCard $input, Component\VCard $output, Property $property, int $targetVersion): void
@@ -94,8 +103,11 @@ protected function convertProperty(Component\VCard $input, Component\VCard $outp
94103

95104
if (Document::VCARD30 === $targetVersion) {
96105
if ($property instanceof Property\Uri && in_array($property->name, ['PHOTO', 'LOGO', 'SOUND'])) {
97-
/** @var Property\Uri $newProperty */
98-
$newProperty = $this->convertUriToBinary($output, $newProperty);
106+
/* We need to tell phpstan that newProperty (as well as property) is instanceof Property\Uri */
107+
/** @var Property\Uri<string, mixed> $uriProperty */
108+
$uriProperty = $newProperty;
109+
/** @var Property\Uri<string, mixed> $newProperty */
110+
$newProperty = $this->convertUriToBinary($output, $uriProperty);
99111
} elseif ($property instanceof Property\VCard\DateAndOrTime) {
100112
// In vCard 4, the birth year may be optional. This is not the
101113
// case for vCard 3. Apple has a workaround for this that
@@ -154,12 +166,16 @@ protected function convertProperty(Component\VCard $input, Component\VCard $outp
154166
}
155167

156168
if ($property instanceof Property\Binary) {
157-
/** @var Property\Binary $newProperty */
158-
$newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters);
169+
/* We need to tell phpstan that newProperty (as well as property) is instanceof Property\Uri */
170+
/** @var Property\Binary<string, mixed> $binaryProperty */
171+
$binaryProperty = $newProperty;
172+
/** @var Property\Binary<string, mixed> $newProperty */
173+
$newProperty = $this->convertBinaryToUri($output, $binaryProperty, $parameters);
159174
} elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) {
160175
// If a property such as BDAY contained 'X-APPLE-OMIT-YEAR',
161176
// then we're stripping the year from the vcard 4 value.
162177
$parts = DateTimeParser::parseVCardDateTime($property->getValue());
178+
/* @phpstan-ignore-next-line 'Call to an undefined method Sabre\VObject\Node::getValue().' */
163179
if ($parts['year'] === $property['X-APPLE-OMIT-YEAR']->getValue()) {
164180
$newValue = '--'.$parts['month'].'-'.$parts['date'];
165181
$newProperty->setValue($newValue);
@@ -251,15 +267,19 @@ protected function convertProperty(Component\VCard $input, Component\VCard $outp
251267
*
252268
* vCard 4.0 no longer supports BINARY properties.
253269
*
254-
* @param array $parameters list of parameters that will eventually be added to
255-
* the new property
270+
* @param Component\VCard<mixed, mixed> $output
271+
* @param Property\Binary<mixed, mixed> $newProperty
272+
* @param array<mixed, mixed> $parameters list of parameters that will eventually be added to
273+
* the new property
274+
*
275+
* @return Uri<mixed, mixed>
256276
*
257277
* @throws InvalidDataException
258278
*/
259279
protected function convertBinaryToUri(Component\VCard $output, Property\Binary $newProperty, array &$parameters): Uri
260280
{
261281
$value = $newProperty->getValue();
262-
/** @var Uri $newProperty */
282+
/** @var Uri<mixed, mixed> $newProperty */
263283
$newProperty = $output->createProperty(
264284
$newProperty->name,
265285
null, // no value
@@ -304,7 +324,10 @@ protected function convertBinaryToUri(Component\VCard $output, Property\Binary $
304324
* be valid in vCard 3.0 as well, we should convert those to BINARY if
305325
* possible, to improve compatibility.
306326
*
307-
* @return Property\Binary|Property\Uri|null
327+
* @param Component\VCard<mixed, mixed> $output
328+
* @param Property\Uri<mixed, mixed> $newProperty
329+
*
330+
* @return Property\Binary<mixed, mixed>|Property\Uri<mixed, mixed>
308331
*
309332
* @throws InvalidDataException
310333
*/
@@ -317,7 +340,7 @@ protected function convertUriToBinary(Component\VCard $output, Property\Uri $new
317340
return $newProperty;
318341
}
319342

320-
/** @var Binary $newProperty */
343+
/** @var Binary<mixed, mixed> $newProperty */
321344
$newProperty = $output->createProperty(
322345
$newProperty->name,
323346
null, // no value
@@ -352,6 +375,9 @@ protected function convertUriToBinary(Component\VCard $output, Property\Uri $new
352375

353376
/**
354377
* Adds parameters to a new property for vCard 4.0.
378+
*
379+
* @param Property<mixed, mixed> $newProperty
380+
* @param array<mixed, mixed> $parameters
355381
*/
356382
protected function convertParameters40(Property $newProperty, array $parameters): void
357383
{
@@ -388,6 +414,9 @@ protected function convertParameters40(Property $newProperty, array $parameters)
388414

389415
/**
390416
* Adds parameters to a new property for vCard 3.0.
417+
*
418+
* @param Property<mixed, mixed> $newProperty
419+
* @param array<mixed, mixed> $parameters
391420
*/
392421
protected function convertParameters30(Property $newProperty, array $parameters): void
393422
{

0 commit comments

Comments
 (0)