Skip to content

Commit

Permalink
refactor: Remove unused code and optimize QueryBuilder class
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 22, 2024
1 parent 08ee988 commit 65ed1ce
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 40 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"laravel/pint": "^1.13.7",
"pestphp/pest": "^2.28.1",
"phpstan/phpstan": "^1.10.50",
"rector/rector": "^0.19.2",
"symfony/var-dumper": "^6.4.0|^7.0.0"
},
"autoload": {
Expand Down Expand Up @@ -44,7 +45,7 @@
"test:refacto": "rector --dry-run",
"test:lint": "pint --test",
"test:types": "phpstan analyse --ansi",
"test:unit": "pest --colors=always",
"test:unit": "pest",
"test": [
"@test:refacto",
"@test:lint",
Expand Down
16 changes: 0 additions & 16 deletions src/Example.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use GuzzleHttp\Exception\GuzzleException;
use KCNetwork\Liquipedia\Query\QueryBuilder;

class Query extends QueryBuilder
final class Query extends QueryBuilder
{
protected Client $client;
private readonly Client $client;

protected string $endpoint;
private string $endpoint;

/**
* @param array<string, mixed> $params
Expand Down Expand Up @@ -68,7 +68,7 @@ public function removeWiki(string $wiki): self
str_replace(
$wiki,
'',
$this->params['wiki']
(string) $this->params['wiki']
)
);

Expand Down
14 changes: 6 additions & 8 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

abstract class QueryBuilder implements QueryBuilderInterface
{
/**
* @var array<string, mixed>
*/
protected array $params;

public function __construct(array $params = [])
{
$this->params = $params;
public function __construct(
/**
* @var array<string, mixed>
*/
protected array $params = []
) {
}

public function build(): array
Expand Down
57 changes: 57 additions & 0 deletions src/Query/QueryParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace KCNetwork\Liquipedia\Query;

use stdClass;

class QueryParameter implements QueryParametersInterface
{
private stdClass $params;

public function __construct(array $params = [])
{
$this->params = (object) $params;

foreach ($this->params as $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;
}

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
{
unset($this->params->{$name});
unset($this->{'get'.ucfirst($name)});
}
}
42 changes: 42 additions & 0 deletions src/Query/QueryParametersInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace KCNetwork\Liquipedia\Query;

use JsonException;

interface QueryParametersInterface
{
/**
* Returns the parameters as an array
*
* @return array<string, mixed>
*/
public function toArray(): array;

/**
* Returns the parameters as a json string
*
* @throws JsonException
*/
public function __toString(): string;

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

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

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

/**
* Unsets the parameter
*/
public function __unset(string $name): void;
}
11 changes: 0 additions & 11 deletions tests/Feature.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/QueryParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use KCNetwork\Liquipedia\Query\QueryParameter;

it('it can be instantiated', function () {
$queryParameter = new QueryParameter();

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

0 comments on commit 65ed1ce

Please sign in to comment.