Skip to content

Commit

Permalink
feat: modernize code for php8+
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 2, 2022
1 parent 35850f3 commit 7841d8f
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 328 deletions.
18 changes: 6 additions & 12 deletions src/Arrayizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait Arrayizes
*
* @return array
*/
public function asArray($data, $cast = true)
public function asArray(mixed $data, bool $cast = true): array
{
if (\is_array($data)) {
return $data;
Expand All @@ -41,7 +41,7 @@ public function asArray($data, $cast = true)
return $data->jsonSerialize();
}

if (\method_exists($data, 'toArray')) {
if (\is_object($data) && \method_exists($data, 'toArray')) {
return $data->toArray();
}

Expand All @@ -50,17 +50,11 @@ public function asArray($data, $cast = true)

/**
* Convert the data items to array.
*
* @return array
*/
public function toArray()
public function toArray(): array
{
return \array_map(function ($value) {
if (\is_scalar($value)) {
return $value;
}

return $this->asArray($value, false);
}, $this->getData());
return \array_map(
fn ($value) => \is_scalar($value) ? $value : $this->asArray($value, false), $this->getData()
);
}
}
21 changes: 5 additions & 16 deletions src/HigherOrderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,17 @@
*/
class HigherOrderMessage
{
protected $underscore;
protected $method;

public function __construct(UnderscoreBase $underscore, $method)
public function __construct(protected UnderscoreBase $underscore, protected string $method)
{
$this->underscore = $underscore;
$this->method = $method;
}

public function __call($method, $args)
public function __call(string $method, array $args): mixed
{
return $this->underscore->{$this->method}(function ($item) use ($method, $args) {
return \call_user_func_array([$item, $method], $args);
});
return $this->underscore->{$this->method}(static fn ($item) => \call_user_func_array([$item, $method], $args));
}

public function __get($prop)
public function __get($prop): mixed
{
return $this->underscore->{$this->method}(function ($item) use ($prop) {
$props = \array_column([$item], $prop);

return empty($props) ? null : $props[0];
});
return $this->underscore->{$this->method}(static fn ($item) => \array_column([$item], $prop)[0] ?? null);
}
}
38 changes: 8 additions & 30 deletions src/Underscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Underscore extends UnderscoreFunction
*
* @param array|mixed $data
*/
public function __construct($data = [])
public function __construct(mixed $data = [])
{
parent::__construct($data);
}
Expand All @@ -30,44 +30,31 @@ public function __construct($data = [])
*
* @return self
*/
public static function _($data = null)
public static function _($data = null): self
{
return new static($data);
}

/**
* Generates a function that always returns a constant value.
*
* @param mixed $value
*
* @return callable
*/
public function constant($value)
public function constant(mixed $value): callable
{
return function () use ($value) {
return $value;
};
return fn () => $value;
}

/**
* No operation!
*
* @return void
*/
public function noop()
public function noop(): void
{
// ;)
}

/**
* Run callable n times and create new collection.
*
* @param int $n
* @param callable $fn
*
* @return self
*/
public function times($n, callable $fn)
public function times(int $n, callable $fn): self
{
$data = [];

Expand All @@ -80,25 +67,16 @@ public function times($n, callable $fn)

/**
* Return a random integer between min and max (inclusive).
*
* @param int $min
* @param int $max
*
* @return int
*/
public function random($min, $max)
public function random(int $min, int $max): int
{
return \mt_rand($min, $max);
}

/**
* Generate unique ID (unique for current go/session).
*
* @param string $prefix
*
* @return string
*/
public function uniqueId($prefix = '')
public function uniqueId(string $prefix = ''): string
{
static $id = 0;

Expand Down
94 changes: 16 additions & 78 deletions src/UnderscoreAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,190 +15,128 @@ trait UnderscoreAliases
{
/**
* Alias of first().
*
* @param int $n
*
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
*/
public function head($n = 1)
public function head(int $n = 1)
{
return $this->first($n);
}

/**
* Alias of first().
*
* @param int $n
*
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
*/
public function take($n = 1)
public function take(int $n = 1)
{
return $this->first($n);
}

/**
* Alias of last().
*
* @param int $n
*
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
*/
public function tail($n = 1)
public function tail(int $n = 1)
{
return $this->last($n);
}

/**
* Alias of last().
*
* @param int $n
*
* @return array|mixed With n = 1 (default), it gives one item, which may not be array.
*/
public function drop($n = 1)
public function drop(int $n = 1)
{
return $this->last($n);
}

/**
* Alias of unique().
*
* @param callable|string $fn The callback. String is resolved to value of that index.
*
* @return self
*/
public function uniq($fn = null)
public function uniq($fn = null): self
{
return $this->unique($fn);
}

/**
* Alias of difference().
*
* @param array|mixed $data Array or array like or array convertible.
*
* @return self
*/
public function without($data)
public function without(mixed $data): self
{
return $this->difference($data);
}

/**
* Alias of map().
*
* @param callable $fn The callback.
*
* @return self
*/
public function collect(callable $fn)
public function collect(callable $fn): self
{
return $this->map($fn);
}

/**
* Alias of reduce().
*
* @param callable $fn The callback.
* @param mixed $memo The initial value carried over to each iteration and returned finally.
*
* @return mixed
*/
public function foldl(callable $fn, $memo)
public function foldl(callable $fn, mixed $memo): mixed
{
return $this->reduce($fn, $memo);
}

/**
* Alias of reduce().
*
* @param callable $fn The callback.
* @param mixed $memo The initial value carried over to each iteration and returned finally.
*
* @return mixed
*/
public function inject(callable $fn, $memo)
public function inject(callable $fn, mixed $memo): mixed
{
return $this->reduce($fn, $memo);
}

/**
* Alias of reduceRight().
*
* @param callable $fn The callback.
* @param mixed $memo The initial value carried over to each iteration and returned finally.
*
* @return mixed
*/
public function foldr(callable $fn, $memo)
public function foldr(callable $fn, mixed $memo): mixed
{
return $this->reduceRight($fn, $memo);
}

/**
* Alias of find().
*
* @param callable $fn The truth test callback.
* @param bool $useValue Whether to return value or the index on match.
*
* @return mixed|null
*/
public function detect(callable $fn)
public function detect(callable $fn): mixed
{
return $this->find($fn);
}

/**
* Alias of filter().
*
* @param callable|string|null $fn The truth test callback.
*
* @return self
*/
public function select(callable $fn = null)
public function select(callable $fn = null): self
{
return $this->filter($fn);
}

/**
* Alias of every().
*
* @param callable $fn The truth test callback.
*
* @return bool
*/
public function all(callable $fn)
public function all(callable $fn): bool
{
return $this->every($fn);
}

/**
* Alias of some().
*
* @param callable $fn The truth test callback.
*
* @return bool
*/
public function any(callable $fn)
public function any(callable $fn): bool
{
return $this->some($fn);
}

/**
* Alias of contains().
*/
public function includes($item)
public function includes(mixed $item): bool
{
return $this->contains($item);
}

/**
* Alias of count().
*
* @return int
*/
public function size()
public function size(): int
{
return $this->count();
}
Expand Down
Loading

0 comments on commit 7841d8f

Please sign in to comment.