Skip to content

Commit e90ba72

Browse files
committed
Add parameter types in AccessKey, fix deprecates PHPUnit method
1 parent 1d7e8ed commit e90ba72

7 files changed

+90
-43
lines changed

src/Helper/AccessKey.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,20 @@ final class AccessKey extends SplStack
2222
/**
2323
* Transforms the Key to a string
2424
*
25-
* @param int|string $key
26-
*
2725
* @return AccessKey<string>
2826
*/
29-
public static function create($key): AccessKey
27+
public static function create(string $key): AccessKey
3028
{
31-
// Ignore arrays and objects
32-
if (is_object($key) or is_array($key)) {
33-
$key = '';
34-
}
35-
36-
$key_string = strval($key);
37-
38-
$key = new self();
39-
$key->raw = $key_string;
40-
41-
$keys = explode('.', $key_string);
29+
$accessKey = new self();
30+
$accessKey->raw = $key;
4231

43-
foreach ($keys as $value) {
44-
$key->push($value);
32+
foreach (explode('.', $key) as $value) {
33+
$accessKey->push($value);
4534
}
4635

47-
$key->rewind();
36+
$accessKey->rewind();
4837

49-
return $key;
38+
return $accessKey;
5039
}
5140

5241
/**

src/Helper/AccessableTrait.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ final public function has($key): bool
6262
gettype($key),
6363
AccessKey::class
6464
), \E_USER_DEPRECATED);
65+
66+
$key = '';
6567
}
6668

6769
$key = $this->parseKey($key);
@@ -97,6 +99,17 @@ final public function has($key): bool
9799
*/
98100
public function get($key)
99101
{
102+
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
103+
trigger_error(sprintf(
104+
'%s::get(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.',
105+
get_class($this),
106+
gettype($key),
107+
AccessKey::class
108+
), \E_USER_DEPRECATED);
109+
110+
$key = '';
111+
}
112+
100113
$key = $this->parseKey($key);
101114

102115
$string = $key->shift();
@@ -136,17 +149,13 @@ private function getValue(string $key)
136149
* Parse a dot.notated.key to an object
137150
*
138151
* @param int|string|AccessKey<string> $key The key
139-
*
140-
* @return AccessKey<string> The parsed key
141152
*/
142153
private function parseKey($key): AccessKey
143154
{
144155
if (is_object($key) and $key instanceof AccessKey) {
145156
return $key;
146157
}
147158

148-
$key = AccessKey::create($key);
149-
150-
return $key;
159+
return AccessKey::create(strval($key));
151160
}
152161
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
// SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt
6+
//
7+
// SPDX-License-Identifier: GPL-3.0-or-later
8+
9+
namespace Art4\JsonApiClient\Tests\Fixtures;
10+
11+
use Art4\JsonApiClient\Accessable;
12+
use Art4\JsonApiClient\Helper\AccessableTrait;
13+
14+
class AccessableTraitMock implements Accessable {
15+
use AccessableTrait;
16+
}

tests/Fixtures/HelperTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ public static function jsonValuesProviderWithoutObjectAndString(): array
104104
return array_values($data);
105105
}
106106

107+
/**
108+
* Json Values Provider but without the string and int
109+
*
110+
* @return array<array<mixed>>
111+
*/
112+
public static function jsonValuesProviderWithoutStringAndInt(): array
113+
{
114+
$data = static::jsonValuesProvider();
115+
116+
unset($data[2]);
117+
unset($data[3]);
118+
119+
return array_values($data);
120+
}
121+
107122
/**
108123
* Json Values as string Provider
109124
*

tests/Unit/Helper/AccessableTraitTest.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@
88

99
namespace Art4\JsonApiClient\Tests\Unit\Helper;
1010

11-
use Art4\JsonApiClient\Helper\AccessableTrait;
12-
use Art4\JsonApiClient\Accessable;
11+
use Art4\JsonApiClient\Exception\AccessException;
12+
use Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock;
13+
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
1314
use PHPUnit\Framework\TestCase;
1415

1516
class AccessableTraitTest extends TestCase
1617
{
17-
public function testHasWithObjectAsKeyTriggersException(): void
18+
use HelperTrait;
19+
20+
/**
21+
* @dataProvider jsonValuesProviderWithoutStringAndInt
22+
*
23+
* @param mixed $key
24+
*/
25+
public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void
1826
{
19-
/** @var Accessable */
20-
$resource = $this->getMockForTrait(AccessableTrait::class);
27+
$resource = new AccessableTraitMock();
2128

2229
// PHPUnit 10 compatible way to test trigger_error().
2330
set_error_handler(
24-
function ($errno, $errstr): bool {
25-
$this->assertStringEndsWith(
26-
'::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
31+
function ($errno, $errstr) use ($key): bool {
32+
$this->assertSame(
33+
'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.',
2734
$errstr
2835
);
2936

@@ -33,19 +40,23 @@ function ($errno, $errstr): bool {
3340
E_USER_DEPRECATED
3441
);
3542

36-
$resource->has(new \stdClass());
43+
$resource->has($key);
3744
}
3845

39-
public function testHasWithArrayAsKeyTriggersException(): void
46+
/**
47+
* @dataProvider jsonValuesProviderWithoutStringAndInt
48+
*
49+
* @param mixed $key
50+
*/
51+
public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void
4052
{
41-
/** @var Accessable */
42-
$resource = $this->getMockForTrait(AccessableTrait::class);
53+
$resource = new AccessableTraitMock();
4354

4455
// PHPUnit 10 compatible way to test trigger_error().
4556
set_error_handler(
46-
function ($errno, $errstr): bool {
47-
$this->assertStringEndsWith(
48-
'::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
57+
function ($errno, $errstr) use ($key): bool {
58+
$this->assertSame(
59+
'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.',
4960
$errstr
5061
);
5162

@@ -55,6 +66,10 @@ function ($errno, $errstr): bool {
5566
E_USER_DEPRECATED
5667
);
5768

58-
$resource->has([]);
69+
try {
70+
$resource->get($key);
71+
} catch (AccessException $th) {
72+
// ignore AccessException
73+
}
5974
}
6075
}

tests/Unit/V1/ErrorCollectionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public function testCreate(): void
4949
$this->assertSame($collection->getKeys(), [0, 1]);
5050

5151
$this->assertFalse($collection->has('string'));
52-
53-
$this->assertTrue($collection->has(AccessKey::create(0)));
54-
52+
$this->assertTrue($collection->has(AccessKey::create('0')));
5553
$this->assertTrue($collection->has(0));
54+
5655
$error = $collection->get(0);
5756

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

6059
$this->assertTrue($collection->has(1));
60+
6161
$error = $collection->get(1);
6262

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

tests/Unit/V1/ResourceCollectionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testCreateWithEmptyArray(): void
4545

4646
// Test get() with various key types
4747
$this->assertFalse($collection->has(0));
48-
$this->assertFalse($collection->has(AccessKey::create(0)));
48+
$this->assertFalse($collection->has(AccessKey::create('0')));
4949
$this->assertFalse($collection->has('string'));
5050
}
5151

@@ -95,6 +95,9 @@ public function testCreateWithIdentifierAndMeta(): void
9595

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

98+
$this->assertSame($resource, $collection->get('0'));
99+
$this->assertSame($resource, $collection->get(AccessKey::create('0')));
100+
98101
$this->assertTrue($collection->has(1));
99102
$resource = $collection->get(1);
100103

0 commit comments

Comments
 (0)