Skip to content

Commit

Permalink
fix: fix deprecation warning on arrayAccess::offsetGet
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 11, 2024
1 parent d784a66 commit 12a9fa2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
25 changes: 25 additions & 0 deletions src/Base/Concern/OffsetGetByPhpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Base\Concern;

/*
* Ugly fix for the following error in php 7:
* 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

Check notice on line 21 in src/Base/Concern/OffsetGetByPhpVersion.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Base/Concern/OffsetGetByPhpVersion.php#L21

Only one object structure is allowed in a file

Check notice on line 21 in src/Base/Concern/OffsetGetByPhpVersion.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Base/Concern/OffsetGetByPhpVersion.php#L21

Only one trait is allowed in a file
{
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

Check warning on line 17 in src/Base/Concern/OffsetGetPhp8.php

View check run for this annotation

Codecov / codecov/patch

src/Base/Concern/OffsetGetPhp8.php#L17

Added line #L17 was not covered by tests
{
return $this->getAttribute($offset);

Check warning on line 19 in src/Base/Concern/OffsetGetPhp8.php

View check run for this annotation

Codecov / codecov/patch

src/Base/Concern/OffsetGetPhp8.php#L19

Added line #L19 was not covered by tests
}
}
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 12a9fa2

Please sign in to comment.