Skip to content

Commit

Permalink
Add parameter types in AccessKey, fix deprecates PHPUnit method
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Oct 25, 2023
1 parent 1d7e8ed commit e90ba72
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 43 deletions.
25 changes: 7 additions & 18 deletions src/Helper/AccessKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,20 @@ final class AccessKey extends SplStack
/**
* Transforms the Key to a string
*
* @param int|string $key
*
* @return AccessKey<string>
*/
public static function create($key): AccessKey
public static function create(string $key): AccessKey
{
// Ignore arrays and objects
if (is_object($key) or is_array($key)) {
$key = '';
}

$key_string = strval($key);

$key = new self();
$key->raw = $key_string;

$keys = explode('.', $key_string);
$accessKey = new self();
$accessKey->raw = $key;

foreach ($keys as $value) {
$key->push($value);
foreach (explode('.', $key) as $value) {
$accessKey->push($value);
}

$key->rewind();
$accessKey->rewind();

return $key;
return $accessKey;
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/Helper/AccessableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ final public function has($key): bool
gettype($key),
AccessKey::class
), \E_USER_DEPRECATED);

$key = '';
}

$key = $this->parseKey($key);
Expand Down Expand Up @@ -97,6 +99,17 @@ final public function has($key): bool
*/
public function get($key)
{
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
trigger_error(sprintf(
'%s::get(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.',
get_class($this),
gettype($key),
AccessKey::class
), \E_USER_DEPRECATED);

$key = '';
}

$key = $this->parseKey($key);

$string = $key->shift();
Expand Down Expand Up @@ -136,17 +149,13 @@ private function getValue(string $key)
* Parse a dot.notated.key to an object
*
* @param int|string|AccessKey<string> $key The key
*
* @return AccessKey<string> The parsed key
*/
private function parseKey($key): AccessKey
{
if (is_object($key) and $key instanceof AccessKey) {
return $key;
}

$key = AccessKey::create($key);

return $key;
return AccessKey::create(strval($key));
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/AccessableTraitMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

// SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt
//
// SPDX-License-Identifier: GPL-3.0-or-later

namespace Art4\JsonApiClient\Tests\Fixtures;

use Art4\JsonApiClient\Accessable;
use Art4\JsonApiClient\Helper\AccessableTrait;

class AccessableTraitMock implements Accessable {
use AccessableTrait;
}
15 changes: 15 additions & 0 deletions tests/Fixtures/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ public static function jsonValuesProviderWithoutObjectAndString(): array
return array_values($data);
}

/**
* Json Values Provider but without the string and int
*
* @return array<array<mixed>>
*/
public static function jsonValuesProviderWithoutStringAndInt(): array
{
$data = static::jsonValuesProvider();

unset($data[2]);
unset($data[3]);

return array_values($data);
}

/**
* Json Values as string Provider
*
Expand Down
47 changes: 31 additions & 16 deletions tests/Unit/Helper/AccessableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@

namespace Art4\JsonApiClient\Tests\Unit\Helper;

use Art4\JsonApiClient\Helper\AccessableTrait;
use Art4\JsonApiClient\Accessable;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock;
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
use PHPUnit\Framework\TestCase;

class AccessableTraitTest extends TestCase
{
public function testHasWithObjectAsKeyTriggersException(): void
use HelperTrait;

/**
* @dataProvider jsonValuesProviderWithoutStringAndInt
*
* @param mixed $key
*/
public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void
{
/** @var Accessable */
$resource = $this->getMockForTrait(AccessableTrait::class);
$resource = new AccessableTraitMock();

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertStringEndsWith(
'::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
function ($errno, $errstr) use ($key): bool {
$this->assertSame(
'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
$errstr
);

Expand All @@ -33,19 +40,23 @@ function ($errno, $errstr): bool {
E_USER_DEPRECATED
);

$resource->has(new \stdClass());
$resource->has($key);

Check failure on line 43 in tests/Unit/Helper/AccessableTraitTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has() expects Art4\JsonApiClient\Helper\AccessKey|int|string, mixed given.
}

public function testHasWithArrayAsKeyTriggersException(): void
/**
* @dataProvider jsonValuesProviderWithoutStringAndInt
*
* @param mixed $key
*/
public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void
{
/** @var Accessable */
$resource = $this->getMockForTrait(AccessableTrait::class);
$resource = new AccessableTraitMock();

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertStringEndsWith(
'::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
function ($errno, $errstr) use ($key): bool {
$this->assertSame(
'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
$errstr
);

Expand All @@ -55,6 +66,10 @@ function ($errno, $errstr): bool {
E_USER_DEPRECATED
);

$resource->has([]);
try {
$resource->get($key);

Check failure on line 70 in tests/Unit/Helper/AccessableTraitTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get() expects Art4\JsonApiClient\Helper\AccessKey|int|string, mixed given.
} catch (AccessException $th) {
// ignore AccessException
}
}
}
6 changes: 3 additions & 3 deletions tests/Unit/V1/ErrorCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public function testCreate(): void
$this->assertSame($collection->getKeys(), [0, 1]);

$this->assertFalse($collection->has('string'));

$this->assertTrue($collection->has(AccessKey::create(0)));

$this->assertTrue($collection->has(AccessKey::create('0')));
$this->assertTrue($collection->has(0));

$error = $collection->get(0);

$this->assertInstanceOf(Accessable::class, $error);

$this->assertTrue($collection->has(1));

$error = $collection->get(1);

$this->assertInstanceOf(Accessable::class, $error);
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/V1/ResourceCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testCreateWithEmptyArray(): void

// Test get() with various key types
$this->assertFalse($collection->has(0));
$this->assertFalse($collection->has(AccessKey::create(0)));
$this->assertFalse($collection->has(AccessKey::create('0')));
$this->assertFalse($collection->has('string'));
}

Expand Down Expand Up @@ -95,6 +95,9 @@ public function testCreateWithIdentifierAndMeta(): void

$this->assertInstanceOf(Accessable::class, $resource);

$this->assertSame($resource, $collection->get('0'));
$this->assertSame($resource, $collection->get(AccessKey::create('0')));

$this->assertTrue($collection->has(1));
$resource = $collection->get(1);

Expand Down

0 comments on commit e90ba72

Please sign in to comment.