Skip to content

Commit d3e2710

Browse files
committed
Using Accessable::has() with object or array is deprecated
1 parent c921149 commit d3e2710

File tree

7 files changed

+133
-7
lines changed

7 files changed

+133
-7
lines changed

src/Accessable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Accessable
1818
/**
1919
* Get a value by a key
2020
*
21-
* @param int|string|AccessKey<string> $key The key
21+
* @param mixed $key The key
2222
*
2323
* @return mixed
2424
*/
@@ -29,7 +29,7 @@ public function get($key);
2929
*
3030
* @deprecated `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors.
3131
*
32-
* @param int|string|AccessKey<string> $key The key
32+
* @param mixed $key The key
3333
*
3434
* @return bool
3535
*/

src/Helper/AccessableTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ final public function getKeys(): array
5555
*/
5656
final public function has($key): bool
5757
{
58+
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
59+
trigger_error(sprintf(
60+
'%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.',
61+
get_class($this),
62+
gettype($key),
63+
AccessKey::class
64+
), \E_USER_DEPRECATED);
65+
}
66+
5867
$key = $this->parseKey($key);
5968

6069
$string = $key->shift();

src/V1/ResourceNull.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public function __construct($data, Manager $manager, Accessable $parent)
4747
*/
4848
public function has($key)
4949
{
50+
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
51+
trigger_error(sprintf(
52+
'%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.',
53+
get_class($this),
54+
gettype($key),
55+
AccessKey::class
56+
), \E_USER_DEPRECATED);
57+
}
58+
5059
return false;
5160
}
5261

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Unit\Helper;
10+
11+
use Art4\JsonApiClient\Helper\AccessableTrait;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class AccessableTraitTest extends TestCase
15+
{
16+
public function testHasWithObjectAsKeyTriggersException(): void
17+
{
18+
$resource = $this->getMockForTrait(AccessableTrait::class);
19+
20+
// PHPUnit 10 compatible way to test trigger_error().
21+
set_error_handler(
22+
function ($errno, $errstr): bool {
23+
$this->assertStringEndsWith(
24+
'::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
25+
$errstr
26+
);
27+
28+
restore_error_handler();
29+
return true;
30+
},
31+
E_USER_DEPRECATED
32+
);
33+
34+
$resource->has(new \stdClass());
35+
}
36+
37+
public function testHasWithArrayAsKeyTriggersException(): void
38+
{
39+
$resource = $this->getMockForTrait(AccessableTrait::class);
40+
41+
// PHPUnit 10 compatible way to test trigger_error().
42+
set_error_handler(
43+
function ($errno, $errstr): bool {
44+
$this->assertStringEndsWith(
45+
'::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
46+
$errstr
47+
);
48+
49+
restore_error_handler();
50+
return true;
51+
},
52+
E_USER_DEPRECATED
53+
);
54+
55+
$resource->has([]);
56+
}
57+
}

tests/Unit/V1/ErrorCollectionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Art4\JsonApiClient\Accessable;
1212
use Art4\JsonApiClient\Exception\AccessException;
1313
use Art4\JsonApiClient\Exception\ValidationException;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
1516
use Art4\JsonApiClient\V1\ErrorCollection;
1617
use PHPUnit\Framework\TestCase;
@@ -47,10 +48,10 @@ public function testCreate(): void
4748

4849
$this->assertSame($collection->getKeys(), [0, 1]);
4950

50-
$this->assertFalse($collection->has(new \stdClass()));
51-
$this->assertFalse($collection->has([]));
5251
$this->assertFalse($collection->has('string'));
5352

53+
$this->assertTrue($collection->has(AccessKey::create(0)));
54+
5455
$this->assertTrue($collection->has(0));
5556
$error = $collection->get(0);
5657

tests/Unit/V1/ResourceCollectionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Art4\JsonApiClient\Accessable;
1212
use Art4\JsonApiClient\Exception\AccessException;
1313
use Art4\JsonApiClient\Exception\ValidationException;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
1516
use Art4\JsonApiClient\V1\ResourceCollection;
1617
use PHPUnit\Framework\TestCase;
@@ -41,11 +42,10 @@ public function testCreateWithEmptyArray(): void
4142
$this->assertInstanceOf(Accessable::class, $collection);
4243

4344
$this->assertSame($collection->getKeys(), []);
44-
$this->assertFalse($collection->has(0));
4545

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

tests/Unit/V1/ResourceNullTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,54 @@ public function testGetThrowsException(): void
6464

6565
$resource->get('something');
6666
}
67+
68+
public function testHasWithObjectAsKeyTriggersException(): void
69+
{
70+
$resource = new ResourceNull(
71+
null,
72+
$this->manager,
73+
$this->parent
74+
);
75+
76+
// PHPUnit 10 compatible way to test trigger_error().
77+
set_error_handler(
78+
function ($errno, $errstr): bool {
79+
$this->assertSame(
80+
'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
81+
$errstr
82+
);
83+
84+
restore_error_handler();
85+
return true;
86+
},
87+
E_USER_DEPRECATED
88+
);
89+
90+
$resource->has(new \stdClass());
91+
}
92+
93+
public function testHasWithArrayAsKeyTriggersException(): void
94+
{
95+
$resource = new ResourceNull(
96+
null,
97+
$this->manager,
98+
$this->parent
99+
);
100+
101+
// PHPUnit 10 compatible way to test trigger_error().
102+
set_error_handler(
103+
function ($errno, $errstr): bool {
104+
$this->assertSame(
105+
'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.',
106+
$errstr
107+
);
108+
109+
restore_error_handler();
110+
return true;
111+
},
112+
E_USER_DEPRECATED
113+
);
114+
115+
$resource->has([]);
116+
}
67117
}

0 commit comments

Comments
 (0)