Skip to content

Commit

Permalink
fix(QueryParameter): Remove unused magic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 22, 2024
1 parent 9f9ea8e commit 957c9c4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ parameters:
- src

reportUnmatchedIgnoredErrors: true
universalObjectCratesClasses:
- Npldevfr\Liquipedia\Query\QueryParameter
28 changes: 5 additions & 23 deletions src/Query/QueryParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@

use stdClass;

final readonly class QueryParameter implements QueryParametersInterface
final class QueryParameter implements QueryParametersInterface
{
private stdClass $params;
private readonly stdClass $params;

public function __construct(array $params = [])
{

$this->params = new stdClass();

foreach ($params as $key => $value) {
$this->params->{$key} = $value;
$this->generateMethod($key);
}
}

private function generateMethod(string $key): void
{
$methodName = 'get'.ucfirst($key);
$this->{$methodName} = fn () => $this->params->{$key};
}

public function toArray(): array
{
return (array) $this->params;
Expand All @@ -34,25 +28,13 @@ public function __toString(): string
return json_encode($this->params, JSON_THROW_ON_ERROR);
}

public function __get(string $name): mixed
{
return $this->params->{$name};
}

public function __set(string $name, mixed $value): void
{
$this->params->{$name} = $value;
$this->generateMethod($name);
}

public function __isset(string $name): bool
{
return isset($this->params->{$name});
}

public function __unset(string $name): void
public function __get(string $name): mixed
{
unset($this->params->{$name});
unset($this->{'get'.ucfirst($name)});
return $this->params->{$name};
}
}
16 changes: 3 additions & 13 deletions src/Query/QueryParametersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,12 @@ public function toArray(): array;
public function __toString(): string;

/**
* Returns the value of the parameter
*/
public function __get(string $name): mixed;

/**
* Sets the value of the parameter
* Sets the parameter
*/
public function __set(string $name, mixed $value): void;

/**
* Checks if the parameter is set
* Gets the parameter
*/
public function __isset(string $name): bool;

/**
* Unsets the parameter
*/
public function __unset(string $name): void;
public function __get(string $name): mixed;
}
52 changes: 52 additions & 0 deletions tests/QueryParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,55 @@

expect($queryParameter)->toBeInstanceOf(QueryParameter::class);
});

it('it can get by magic method', function () {
$queryParameter = new QueryParameter([
'limit' => 10,
]);

expect($queryParameter->limit)->toBe(10);
});

it('it can set by magic method', function () {
$queryParameter = new QueryParameter();

$queryParameter->wiki = 'foo';
expect($queryParameter->wiki)->toBe('foo');
});

it('it can be converted to array', function () {
$queryParameter = new QueryParameter([
'wiki' => 'foo',
'limit' => 10,
]);

expect($queryParameter->toArray())->toBe([
'wiki' => 'foo',
'limit' => 10,
])->and($queryParameter->wiki)->toBe('foo')
->and($queryParameter->limit)->toBe(10);

});

it('it can be converted to string', function () {
$queryParameter = new QueryParameter([
'wiki' => 'foo',
'limit' => 10,
]);

expect((string) $queryParameter)->toBe('{"wiki":"foo","limit":10}');
});

it('it can be set', function () {
$queryParameter = new QueryParameter([
'wiki' => 'foo',
'limit' => 10,
]);

$queryParameter->wiki = 'bar';

expect($queryParameter->toArray())->toBe([
'wiki' => 'bar',
'limit' => 10,
]);
});

0 comments on commit 957c9c4

Please sign in to comment.