Skip to content

Commit

Permalink
fix: fix deprecation warning on arrayAccess::offsetGet (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored Sep 25, 2024
1 parent 81f55d6 commit 82eb15b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/Base/Concern/OffsetGetByPhpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
// phpcs:ignore Generic.Files.OneTraitPerFile, Generic.Files.OneObjectStructurePerFile

declare(strict_types=1);

namespace MyParcelNL\Pdk\Base\Concern;

/*
* Ugly fix for the following error in php 8:
* Return type of MyParcelNL\Pdk\Base\Model\Model::offsetGet($offset) should either be compatible with
* ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to
* temporarily suppress the notice.
*
* @TODO: Remove this when we no longer have to support PHP 7.
*/
if (PHP_VERSION_ID >= 80000) {
trait OffsetGetByPhpVersion
{
use OffsetGetPhp8;
}
} else {
trait OffsetGetByPhpVersion
{
use OffsetGetPhp7;
}
}
20 changes: 20 additions & 0 deletions src/Base/Concern/OffsetGetPhp7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Base\Concern;

trait OffsetGetPhp7
{
/**
* Get the value for a given offset.
*
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->getAttribute($offset);
}
}
21 changes: 21 additions & 0 deletions src/Base/Concern/OffsetGetPhp8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/** @noinspection PhpUndefinedClassInspection */

declare(strict_types=1);

namespace MyParcelNL\Pdk\Base\Concern;

trait OffsetGetPhp8
{
/**
* Get the value for a given offset.
*
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset): mixed
{
return $this->getAttribute($offset);
}
}
14 changes: 2 additions & 12 deletions src/Base/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayAccess;
use MyParcelNL\Pdk\Base\Concern\HasAttributes;
use MyParcelNL\Pdk\Base\Concern\OffsetGetByPhpVersion;
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Contract\ModelInterface;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
Expand All @@ -17,6 +18,7 @@
*/
class Model implements StorableArrayable, ArrayAccess, ModelInterface
{
use OffsetGetByPhpVersion;
use HasAttributes;

/**
Expand Down Expand Up @@ -191,18 +193,6 @@ public function offsetExists($offset): bool
return null !== $this->getAttribute($offset);
}

/**
* Get the value for a given offset.
*
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->getAttribute($offset);
}

/**
* Set the value for a given offset.
*
Expand Down

0 comments on commit 82eb15b

Please sign in to comment.