jeroendesloovere / vcard

This vCard PHP library can easily parse or generate/export vCards as .vcf
https://packagist.org/packages/jeroendesloovere/vcard
MIT License
503 stars 192 forks source link

Add return type of inherited methods from Iterator in VCardParser #208

Open codeflow-biz opened 2 years ago

codeflow-biz commented 2 years ago

PHP8.1 (at least - did not test with PHP8.0) logs deprecated issues because the 5 inherited methods from the Iterator base class in VCardParser do not declare the return type:

[20-Oct-2022 12:29:54 Europe/Vienna] PHP Deprecated:  Return type of JeroenDesloovere\VCard\VCardParser::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in .\VCardParser.php on line 72
[20-Oct-2022 12:29:54 Europe/Vienna] PHP Deprecated:  Return type of JeroenDesloovere\VCard\VCardParser::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in .\VCardParser.php on line 84
[20-Oct-2022 12:29:54 Europe/Vienna] PHP Deprecated:  Return type of JeroenDesloovere\VCard\VCardParser::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in .\VCardParser.php on line 79
[20-Oct-2022 12:29:54 Europe/Vienna] PHP Deprecated:  Return type of JeroenDesloovere\VCard\VCardParser::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in .\VCardParser.php on line 89
[20-Oct-2022 12:29:54 Europe/Vienna] PHP Deprecated:  Return type of JeroenDesloovere\VCard\VCardParser::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in .\VCardParser.php on line 67

Fix: Add return type :mixed to methods current() and key(), add return type :bool to method valid(), add return type :void to methods rewind(), next().

Xavlight commented 1 year ago

Same problem.

Do you have a solution?

Xav

Xavlight commented 1 year ago

Hi,

I was able to fix the problem by naming the type of the functions public (void, mixed, bool). No more deprecation messages.

Change in class : VCardParser.php

...
public function rewind(): void {
    $this->position = 0;
}

public function current(): mixed {
    if ($this->valid()) {
        return $this->getCardAtIndex($this->position);
    }
}

public function key(): mixed {
    return $this->position;
}

public function next(): void {
    $this->position++;
}

public function valid(): bool {
    return !empty($this->vcardObjects[$this->position]);
}
...

I just saw that the answer was already there, by [codeflow-biz]. But I didn't understand.

Xav