Skip to content

Commit

Permalink
remove array access support
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 13, 2025
1 parent 92f5bb1 commit 837e421
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 246 deletions.
48 changes: 1 addition & 47 deletions src/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
namespace Chevere\Parameter;

use ArgumentCountError;
use ArrayAccess;
use Chevere\Parameter\Interfaces\ArgumentsInterface;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Parameter\Interfaces\ParametersAccessInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Parameter\Traits\ExceptionErrorMessageTrait;
use InvalidArgumentException;
use OutOfBoundsException;
use ReflectionClass;
use Throwable;
use TypeError;
use function Chevere\Message\message;
Expand All @@ -43,11 +41,6 @@ final class Arguments implements ArgumentsInterface
*/
private array $null = [];

/**
* @var array<string>
*/
private array $reflected = [];

/**
* @var string[]
*/
Expand All @@ -62,11 +55,8 @@ final class Arguments implements ArgumentsInterface
// @phpstan-ignore-next-line
public function __construct(
private ParametersInterface $parameters,
array|ArrayAccess $arguments
array $arguments
) {
if ($arguments instanceof ArrayAccess) {
$arguments = $this->getArrayAccessArray($arguments);
}
$this->isIterable = $parameters->keys() === ['K', 'V'];
$this->isPositional = array_is_list($arguments) && ! $this->isIterable;
if ($this->isIterable) {
Expand Down Expand Up @@ -405,40 +395,4 @@ private function isSkipOptional(string $name): bool
return $this->parameters()->optionalKeys()->contains($name)
&& ! $this->has($name);
}

/**
* @param ArrayAccess<int|string, mixed> $arguments
* @return array<int|string, mixed>
*/
private function getArrayAccessArray(ArrayAccess $arguments): array
{
$array = [];
$cast = (array) $arguments;
$reflector = new ReflectionClass($arguments);
$properties = $reflector->getProperties();
foreach ($properties as $property) {
$name = $property->getName();
$array[$name] = $property->getValue($arguments);
$this->reflected[] = $name;
}
$this->fixScopeArrayCast($array, $cast);

return $array;
}

/**
* @param array<int|string, mixed> $array
* @param array<int|string, mixed> $cast
*/
private function fixScopeArrayCast(array &$array, array $cast): void
{
foreach ($cast as $key => $value) {
$key = strval($key);
if (str_contains($key, "\x00")
|| in_array($key, $this->reflected, true)) {
continue;
}
$array[$key] = $value;
}
}
}
3 changes: 1 addition & 2 deletions src/Attributes/ArrayAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Parameter\Attributes;

use ArrayAccess;
use Attribute;
use Chevere\Parameter\ArrayParameter;
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
Expand Down Expand Up @@ -44,7 +43,7 @@ public function __construct(
}

// @phpstan-ignore-next-line
public function __invoke(array|ArrayAccess $array): array|ArrayAccess
public function __invoke(array $array): array
{
return $this->parameter->__invoke($array);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Parameter;

use ArrayAccess;
use Chevere\Parameter\Interfaces\CastInterface;

final class Cast implements CastInterface
Expand Down Expand Up @@ -50,9 +49,7 @@ public function string(): string
*/
public function array(): array
{
return $this->argument instanceof ArrayAccess
? (array) $this->argument
: $this->argument;
return $this->argument;
}

public function object(): object
Expand Down
4 changes: 1 addition & 3 deletions src/Interfaces/ArrayParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Chevere\Parameter\Interfaces;

use ArrayAccess;

/**
* Describes the component in charge of defining a parameter of type array.
*/
Expand All @@ -24,7 +22,7 @@ interface ArrayParameterInterface extends ArrayTypeParameterInterface, ArrayPara
* Asserts the given `$value` is valid.
* @phpstan-ignore-next-line
*/
public function __invoke(array|ArrayAccess $value): array|ArrayAccess;
public function __invoke(array $value): array;

/**
* Return an instance with the specified default value.
Expand Down
6 changes: 1 addition & 5 deletions src/Interfaces/ArrayStringParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Chevere\Parameter\Interfaces;

use ArrayAccess;

/**
* Describes the component in charge of defining a parameter of type array with string members.
*/
Expand All @@ -25,10 +23,8 @@ interface ArrayStringParameterInterface extends ArrayTypeParameterInterface, Arr
*
* @param array<string, mixed> $array
* @return array<string, mixed>
*
* @phpstan-ignore-next-line
*/
public function __invoke(array|ArrayAccess $array): array|ArrayAccess;
public function __invoke(array $array): array;

/**
* Return an instance with the specified default value.
Expand Down
3 changes: 1 addition & 2 deletions src/Traits/ArrayParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Parameter\Traits;

use ArrayAccess;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Parameter\Interfaces\TypeInterface;
use Iterator;
Expand All @@ -28,7 +27,7 @@ trait ArrayParameterTrait
private bool $isList = false;

// @phpstan-ignore-next-line
public function __invoke(array|ArrayAccess $array): array|ArrayAccess
public function __invoke(array $array): array
{
// @phpstan-ignore-next-line
return assertArray($this, $array);
Expand Down
5 changes: 2 additions & 3 deletions src/functions-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Parameter;

use ArrayAccess;
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\Interfaces\ArrayStringParameterInterface;
use Chevere\Parameter\Interfaces\ArrayTypeParameterInterface;
Expand Down Expand Up @@ -66,12 +65,12 @@ function file(
}

/**
* @param array<int|string, mixed>|ArrayAccess<int|string, mixed> $argument
* @param array<int|string, mixed> $argument
* @return array<int|string, mixed> Asserted array, with fixed optional values.
*/
function assertArray(
ArrayTypeParameterInterface $parameter,
array|ArrayAccess $argument,
array $argument,
): array {
if ($parameter->parameters()->count() === 0) {
return (array) $argument;
Expand Down
5 changes: 2 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Parameter;

use ArrayAccess;
use BadMethodCallException;
use Chevere\Parameter\Attributes\ReturnAttr;
use Chevere\Parameter\Exceptions\AttributeNotFoundException;
Expand Down Expand Up @@ -52,7 +51,7 @@
function cast(mixed $variable, string|int ...$key): CastInterface
{
if ($key !== []) {
if (! ($variable instanceof ArrayAccess || is_array($variable))) {
if (! is_array($variable)) {
throw new BadMethodCallException(
(string) message(
'Argument must be array-accessible, %type% provided',
Expand Down Expand Up @@ -137,7 +136,7 @@ function parameters(
*/
function arguments(
ParametersInterface|ParametersAccessInterface $parameters,
array|ArrayAccess $arguments
array $arguments
): ArgumentsInterface {
$parameters = getParameters($parameters);

Expand Down
56 changes: 0 additions & 56 deletions tests/ArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@

use ArgumentCountError;
use Chevere\Parameter\Arguments;
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\IntParameter;
use Chevere\Parameter\Parameters;
use Chevere\Regex\Regex;
use Chevere\Tests\src\ArrayAccessDynamic;
use Chevere\Tests\src\ArrayAccessMixed;
use Chevere\Tests\src\ArrayAccessScoped;
use InvalidArgumentException;
use OutOfBoundsException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
use TypeError;
use function Chevere\Parameter\arrayp;
use function Chevere\Parameter\bool;
use function Chevere\Parameter\int;
use function Chevere\Parameter\parameters;
Expand Down Expand Up @@ -456,57 +451,6 @@ public function testCastOptional(): void
$arguments->required($foo);
}

#[DataProvider('dataProviderArrayAccess')]
public function testArrayAccess(
ArrayParameterInterface $parameter,
\ArrayAccess $arrayAccess,
array $array
): void {
$arguments = new Arguments($parameter->parameters(), $arrayAccess);
$this->assertSame($array, $arguments->toArray());
}

public static function dataProviderArrayAccess(): array
{
$named = [
'string' => 'test',
'int' => 1,
'bool' => false,
];
$parameter = arrayp(
string: string(),
int: int(),
bool: bool()
);

return [
[
arrayp(),
new ArrayAccessDynamic([]),
[],
],
[
$parameter,
new ArrayAccessScoped(...$named),
$named,
],
[
$parameter,
new ArrayAccessDynamic($named),
$named,
],
[
$parameter->withRequired(
dynamic: string()
),
new ArrayAccessMixed(...$named),
$named + [
'dynamic' => '123abc',
],
],
];
}

public function testMinimumOptional(): void
{
$parameters = (new Parameters(foo: string()))
Expand Down
9 changes: 0 additions & 9 deletions tests/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Tests;

use ArrayObject;
use Chevere\Parameter\Cast;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -66,12 +65,4 @@ public function testCast($expected, string $method): void
$cast = new Cast($expected);
$this->assertSame($expected, $cast->{$method}());
}

public function testArrayAccess(): void
{
$input = ['foo'];
$value = new ArrayObject($input);
$cast = new Cast($value);
$this->assertSame($input, $cast->array());
}
}
37 changes: 0 additions & 37 deletions tests/src/ArrayAccess.php

This file was deleted.

25 changes: 0 additions & 25 deletions tests/src/ArrayAccessDynamic.php

This file was deleted.

Loading

0 comments on commit 837e421

Please sign in to comment.