Skip to content

Commit

Permalink
Using Accessable::has() with object or array is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Oct 19, 2023
1 parent c921149 commit d3e2710
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Accessable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Accessable
/**
* Get a value by a key
*
* @param int|string|AccessKey<string> $key The key
* @param mixed $key The key
*
* @return mixed
*/
Expand All @@ -29,7 +29,7 @@ public function get($key);
*
* @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.
*
* @param int|string|AccessKey<string> $key The key
* @param mixed $key The key
*
* @return bool
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Helper/AccessableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ final public function getKeys(): array
*/
final public function has($key): bool
{
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
trigger_error(sprintf(
'%s::has(): 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 = $this->parseKey($key);

$string = $key->shift();
Expand Down
9 changes: 9 additions & 0 deletions src/V1/ResourceNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function __construct($data, Manager $manager, Accessable $parent)
*/
public function has($key)
{
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {

Check failure on line 50 in src/V1/ResourceNull.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Result of && is always false.

Check failure on line 50 in src/V1/ResourceNull.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Result of || is always false.

Check failure on line 50 in src/V1/ResourceNull.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Result of && is always false.

Check failure on line 50 in src/V1/ResourceNull.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Result of || is always false.
trigger_error(sprintf(
'%s::has(): 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);
}

return false;
}

Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Helper/AccessableTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Unit\Helper;

use Art4\JsonApiClient\Helper\AccessableTrait;
use PHPUnit\Framework\TestCase;

class AccessableTraitTest extends TestCase
{
public function testHasWithObjectAsKeyTriggersException(): void
{
$resource = $this->getMockForTrait(AccessableTrait::class);

// 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.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

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

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

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Call to an undefined method PHPUnit\Framework\MockObject\MockObject::has().

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

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Call to an undefined method PHPUnit\Framework\MockObject\MockObject::has().
}

public function testHasWithArrayAsKeyTriggersException(): void
{
$resource = $this->getMockForTrait(AccessableTrait::class);

// 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.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

$resource->has([]);

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

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Call to an undefined method PHPUnit\Framework\MockObject\MockObject::has().

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

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Call to an undefined method PHPUnit\Framework\MockObject\MockObject::has().
}
}
5 changes: 3 additions & 2 deletions tests/Unit/V1/ErrorCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Art4\JsonApiClient\Accessable;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\Helper\AccessKey;
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
use Art4\JsonApiClient\V1\ErrorCollection;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -47,10 +48,10 @@ public function testCreate(): void

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

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

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

$this->assertTrue($collection->has(0));
$error = $collection->get(0);

Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/V1/ResourceCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Art4\JsonApiClient\Accessable;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\Helper\AccessKey;
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
use Art4\JsonApiClient\V1\ResourceCollection;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -41,11 +42,10 @@ public function testCreateWithEmptyArray(): void
$this->assertInstanceOf(Accessable::class, $collection);

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

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

Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/V1/ResourceNullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,54 @@ public function testGetThrowsException(): void

$resource->get('something');
}

public function testHasWithObjectAsKeyTriggersException(): void
{
$resource = new ResourceNull(
null,
$this->manager,
$this->parent
);

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'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.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

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

Check failure on line 90 in tests/Unit/V1/ResourceNullTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\V1\ResourceNull::has() expects Art4\JsonApiClient\Helper\AccessKey|int|string, stdClass given.

Check failure on line 90 in tests/Unit/V1/ResourceNullTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\V1\ResourceNull::has() expects Art4\JsonApiClient\Helper\AccessKey|int|string, stdClass given.
}

public function testHasWithArrayAsKeyTriggersException(): void
{
$resource = new ResourceNull(
null,
$this->manager,
$this->parent
);

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'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.',
$errstr
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED
);

$resource->has([]);

Check failure on line 115 in tests/Unit/V1/ResourceNullTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\V1\ResourceNull::has() expects Art4\JsonApiClient\Helper\AccessKey|int|string, array given.

Check failure on line 115 in tests/Unit/V1/ResourceNullTest.php

View workflow job for this annotation

GitHub Actions / Run PHPStan with PHP 8.2

Parameter #1 $key of method Art4\JsonApiClient\V1\ResourceNull::has() expects Art4\JsonApiClient\Helper\AccessKey|int|string, array given.
}
}

0 comments on commit d3e2710

Please sign in to comment.