Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix deprecation warning on arrayAccess::offsetGet #305

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Base/Concern/OffsetGetByPhpVersion.php#L22

Only one object structure is allowed in a file

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Base/Concern/OffsetGetByPhpVersion.php#L22

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