whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
788 stars 228 forks source link

Using PHP 8, we got a series of warnings due to the contracts: JsonSerializable and ArrayAccess #370

Closed HenriqueSPin closed 2 years ago

HenriqueSPin commented 2 years ago

In Layout class, a set of interfaces is implemented: JsonSerializable and ArrayAccess.

PHP 8 method signatures need to define the methods parameters types and return types in order to comply with these interfaces.

For instance, the method:

    public function offsetSet($offset, $value)

It should look like:

    public function offsetSet(mixed $offset, mixed $value): void

Otherwise, we get this warning in logs:

Return type of Whitecube\NovaFlexibleContent\Layouts\Layout::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/henriquespin/Code/orca/vendor/whitecube/nova-flexible-content/src/Layouts/Layout.php on line 493

PHP versions before 8.0 parse the attribute syntax as a code comment and do not cause any syntax errors. Adding #[\ReturnTypeWillChange] attribute to a class method does not cause any issues, but omits the deprecation notice in PHP 8.1.

This is the first built-in attribute added to PHP.

class Layout implements ArrayAccess {
    #[\ReturnTypeWillChange]
    public function offsetGet(mixed $offset) {}
    // ...
}

https://php.watch/versions/8.1/ReturnTypeWillChange

toonvandenbos commented 2 years ago

Hi, thanks for reporting this. Could you make a PR? Thanks!

HenriqueSPin commented 2 years ago

https://github.com/whitecube/nova-flexible-content/pull/371

toonvandenbos commented 2 years ago

371 merged in v1.0.1.