Skip to content

Commit

Permalink
Typehint functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersai committed Jan 21, 2024
1 parent 91c2a05 commit b0b2316
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 158 deletions.
5 changes: 5 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
->setRules([
'@PSR12' => true,
'@PHP80Migration' => true,
'yoda_style' => true,
'no_unused_imports' => true,
'ordered_imports' => true,
'ordered_class_elements' => true,
'ordered_types' => true,
])
->setFinder((new PhpCsFixer\Finder())->in(__DIR__ . '/src'));
6 changes: 3 additions & 3 deletions src/Attributes/Paginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
final class Paginate
{
public function __construct(
public string|array|Decision $next,
public null|string|array|Decision $previous = null,
public null|string|array $callback = null
public array|Decision|string $next,
public null|array|Decision|string $previous = null,
public null|array|string $callback = null
) {
}
}
4 changes: 2 additions & 2 deletions src/Attributes/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ final class Transition
{
public function __construct(
public string $to,
public string|array|Decision $match,
public null|string|array $callback = null
public array|Decision|string $match,
public null|array|string $callback = null
) {
}
}
2 changes: 1 addition & 1 deletion src/Attributes/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Truncate
public function __construct(
public int $limit,
public string $end,
public string|array|Decision $more
public array|Decision|string $more
) {
}
}
4 changes: 2 additions & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function __construct(
private string $gid,
private string $input
) {
if (strlen($uid) === 0) {
if (0 === strlen($uid)) {
throw new InvalidArgumentException("Unique Identifier (uid) can not be empty");
}

if (strlen($gid) === 0) {
if (0 === strlen($gid)) {
throw new InvalidArgumentException("Global Identifier (gid) can not be empty");
}

Expand Down
1 change: 0 additions & 1 deletion src/Contracts/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface Action
{
//
}
1 change: 0 additions & 1 deletion src/Contracts/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface State
{
//
}
4 changes: 2 additions & 2 deletions src/Decisions/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class Between implements Decision
{
public function __construct(
private string|int|float $start,
private string|int|float $end
private float|int|string $start,
private float|int|string $end
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/Equal.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Equal implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class GreaterThan implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/GreaterThanOrEqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class GreaterThanOrEqualTo implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class In implements Decision
{
private array $values;

public function __construct(string|int|float ...$values)
public function __construct(float|int|string ...$values)
{
$this->values = $values;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class LessThan implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/LessThanOrEqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class LessThanOrEqualTo implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/Decisions/NotBetween.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class NotBetween implements Decision
{
public function __construct(
private string|int|float $start,
private string|int|float $end
private float|int|string $start,
private float|int|string $end
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/NotEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class NotEqual implements Decision
{
public function __construct(
private string|int|float $expected
private float|int|string $expected
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decisions/NotIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class NotIn implements Decision
{
private array $values;

public function __construct(string|int|float ...$values)
public function __construct(float|int|string ...$values)
{
$this->values = $values;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ public function __construct(
) {
}

public function __toString(): string
{
return $this->content;
}

public static function build(): static
{
return new static('');
}

public function append(self|callable $menu): static
public function append(callable|self $menu): static
{
if (is_callable($menu)) {
$menu($append = new static(''));
Expand All @@ -34,7 +39,7 @@ public function append(self|callable $menu): static
return $this;
}

public function prepend(self|callable $menu): static
public function prepend(callable|self $menu): static
{
if (is_callable($menu)) {
$menu($append = new static(''));
Expand All @@ -46,9 +51,4 @@ public function prepend(self|callable $menu): static

return $this;
}

public function __toString(): string
{
return $this->content;
}
}
66 changes: 33 additions & 33 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,35 @@ public function __construct(
$this->repository = Cache::store($store);
}

private function id(string $key, bool $public): string
public function __set(string $name, mixed $value): void
{
return 'ussd:'.($public ? $this->gid : $this->uid).":{$key}";
$this->set($name, $value);
}

private function ids(array $keys, bool $public): array
public function __get(string $name): mixed
{
return array_map(fn ($key) => $this->id($key, $public), $keys);
return $this->get($name);
}

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

public function __unset(string $name): void
{
$this->forget($name);
}

public function __invoke(array|string $argument): mixed
{
if (is_string($argument)) {
return $this->get($argument);
}

if (is_array($argument)) {
return $this->setMany($argument);
}
}

public function has(string $key, bool $public = false): bool
Expand All @@ -37,15 +58,15 @@ public function has(string $key, bool $public = false): bool
public function set(
string $key,
mixed $value,
null|int|DateInterval|DateTimeInterface $ttl = null,
null|DateInterval|DateTimeInterface|int $ttl = null,
bool $public = false
): bool {
return $this->repository->set($this->id($key, $public), $value, $ttl);
}

public function setMany(
array $values,
null|int|DateInterval|DateTimeInterface $ttl = null,
null|DateInterval|DateTimeInterface|int $ttl = null,
bool $public = false
): bool {
$newValues = [];
Expand All @@ -69,12 +90,12 @@ public function getMany(array $keys, mixed $default = null, bool $public = false
);
}

public function increment(string $key, mixed $value = 1, bool $public = false): int|bool
public function increment(string $key, mixed $value = 1, bool $public = false): bool|int
{
return $this->repository->increment($this->id($key, $public), $value);
}

public function decrement(string $key, mixed $value = 1, bool $public = false): int|bool
public function decrement(string $key, mixed $value = 1, bool $public = false): bool|int
{
return $this->repository->decrement($this->id($key, $public), $value);
}
Expand All @@ -89,34 +110,13 @@ public function forgetMany(array $keys, bool $public = false): bool
return $this->repository->deleteMultiple($this->ids($keys, $public));
}

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

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

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

public function __unset(string $name): void
private function id(string $key, bool $public): string
{
$this->forget($name);
return 'ussd:'.($public ? $this->gid : $this->uid).":{$key}";
}

public function __invoke(string|array $argument): mixed
private function ids(array $keys, bool $public): array
{
if (is_string($argument)) {
return $this->get($argument);
}

if (is_array($argument)) {
return $this->setMany($argument);
}
return array_map(fn ($key) => $this->id($key, $public), $keys);
}
}
21 changes: 11 additions & 10 deletions src/Tests/PendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use DateTimeInterface;
use Illuminate\Support\Str;
use Sparors\Ussd\ContinuingMode;
use Sparors\Ussd\Contracts\Response;
use Sparors\Ussd\Contracts\Configurator;
use Sparors\Ussd\Contracts\InitialState;
use Sparors\Ussd\Contracts\ContinueState;
use Sparors\Ussd\Contracts\ExceptionHandler;
use Sparors\Ussd\Contracts\InitialAction;
use Sparors\Ussd\Contracts\InitialState;
use Sparors\Ussd\Contracts\Response;

class PendingTest
{
Expand All @@ -22,10 +22,10 @@ class PendingTest
private ?string $storeName;

public function __construct(
private string|InitialState|InitialAction $initialState,
private InitialAction|InitialState|string $initialState,
private int $continuingMode = ContinuingMode::START,
private null|int|DateInterval|DateTimeInterface $continuingTtl = null,
private null|string|ContinueState $continuingState = null
private null|DateInterval|DateTimeInterface|int $continuingTtl = null,
private null|ContinueState|string $continuingState = null
) {
$this->uses = [];
$this->additional = [];
Expand All @@ -40,28 +40,28 @@ public function additional(array $additional): static
return $this;
}

public function use(string|Configurator|Response|ExceptionHandler|Closure $use)
public function use(Closure|Configurator|ExceptionHandler|Response|string $use): static
{
$this->uses[] = $use;

return $this;
}

public function useStore(string $storeName)
public function useStore(string $storeName): static
{
$this->storeName = $storeName;

return $this;
}

public function actingAs(string $key)
public function actingAs(string $key): static
{
$this->actor = $key;

return $this;
}

public function start()
public function start(string $input = ''): Testing
{
return new Testing(
$this->initialState,
Expand All @@ -71,7 +71,8 @@ public function start()
$this->storeName,
$this->additional,
$this->uses,
$this->actor
$this->actor,
$input
);
}
}
Loading

0 comments on commit b0b2316

Please sign in to comment.