Skip to content

Commit

Permalink
refactor: Minor optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 30, 2022
1 parent 19616a6 commit 11bee15
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Operation/AsyncMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use function function_exists;

// phpcs:disable
if (false === function_exists('Amp\ParallelFunctions\parallel')) {
if (!function_exists('Amp\ParallelFunctions\parallel')) {
throw new Exception('You need amphp/parallel-functions to get this operation working.');
}
// phpcs:enable
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/AsyncMapN.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use function function_exists;

// phpcs:disable
if (false === function_exists('Amp\ParallelFunctions\parallel')) {
if (!function_exists('Amp\ParallelFunctions\parallel')) {
throw new Exception('You need amphp/parallel-functions to get this operation working.');
}
// phpcs:enable
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static function (...$values): Closure {
/**
* @param T $value
*/
static fn ($value): bool => false === in_array($value, $values, true);
static fn ($value): bool => !in_array($value, $values, true);

$filter = (new Filter())()($filterCallbackFactory($values));

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (...$keys): Closure {
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => false === in_array($key, $keys, true);
static fn ($value, $key): bool => !in_array($key, $keys, true);

$filter = (new Filter())()($filterCallbackFactory($keys));

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static function ($value, $key) use ($comparatorCallback, $accessorCallback, $sta

$matchFalse = (new MatchOne())()($matchWhenNot)($matcher)($stack);

if (true === $matchFalse->current()) {
if ($matchFalse->current()) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static function ($value, $key) use ($comparatorCallback, $accessorCallback, $sta

$matchFalse = (new MatchOne())()($matchWhenNot)($matcher)($stack);

if (true === $matchFalse->current()) {
if ($matchFalse->current()) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static function (callable ...$callbacks) use ($matchers): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback($callbackReducer($callbacks))($callbackReducer($matchers))),
DropWhile::of()(static fn (bool $value): bool => true === $value),
DropWhile::of()(static fn (bool $value): bool => $value),
Append::of()(true),
Head::of(),
);
Expand Down
14 changes: 10 additions & 4 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ public function __invoke(): Closure
$matchWhenNot = static fn (): bool => true;
$matcher =
/**
* @param T $value
* @param bool $value
*/
static fn ($value): bool => (bool) $value;
static fn (bool $value): bool => $value;

/** @var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
MatchOne::of()($matchWhenNot)($matcher),
Map::of()(
/**
* @param T $value
*/
static fn ($value): bool => !(bool) $value
static fn ($value): bool => (bool) $value
),
MatchOne::of()($matchWhenNot)($matcher),
Map::of()(
/**
* @param bool $value
*/
static fn (bool $value): bool => !$value
),
);

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke(): Closure
*/
static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
if (!is_iterable($value)) {
yield $key => $value;

continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (...$keys): Closure {
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => false === in_array($key, $keys, true);
static fn ($value, $key): bool => !in_array($key, $keys, true);

$filter = (new Filter())()($filterCallbackFactory($keys));

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static function (Iterator $iterator): Generator {
break;
}

if (false === $isEmpty) {
if (!$isEmpty) {
yield $key => $current;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static function (Iterator $iterator): Generator {
$isEmpty = false;
}

if (false === $isEmpty) {
if (!$isEmpty) {
yield $key => $current;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static function (callable ...$callbacks) use ($matchers): Closure {
*/
static fn ($value, $key, Iterator $iterator): bool => CallbacksArrayReducer::or()($callbacks, $value, $key, $iterator) === CallbacksArrayReducer::or()($matchers, $value, $key, $iterator)
),
DropWhile::of()(static fn (bool $value): bool => false === $value),
DropWhile::of()(static fn (bool $value): bool => !$value),
Append::of()(false),
Head::of()
);
Expand Down
14 changes: 10 additions & 4 deletions src/Operation/Nullsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ public function __invoke(): Closure
$matchWhenNot = static fn (): bool => false;
$matcher =
/**
* @param T $value
* @param bool $value
*/
static fn ($value): bool => in_array($value, self::VALUES, true);
static fn (bool $value): bool => in_array($value, self::VALUES, true);

/** @var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
MatchOne::of()($matchWhenNot)($matcher),
Map::of()(
/**
* @param T $value
*/
static fn ($value): bool => !$value
static fn ($value): bool => (bool) $value
),
MatchOne::of()($matchWhenNot)($matcher),
Map::of()(
/**
* @param bool $value
*/
static fn (bool $value): bool => !$value
),
);

Expand Down
10 changes: 5 additions & 5 deletions src/Operation/Pluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static function (Iterator $iterator) use ($key, $default): Generator {
static function (Iterator $iterator, $target, array $key, $default = null) use (&$pick) {
while (null !== $segment = array_shift($key)) {
if ('*' === $segment) {
if (false === is_iterable($target)) {
if (!is_iterable($target)) {
return $default;
}

Expand All @@ -87,16 +87,16 @@ static function (Iterator $iterator, $target, array $key, $default = null) use (
return in_array('*', $key, true) ? $collapse : $result;
}

if ((true === is_array($target)) && (true === array_key_exists($segment, $target))) {
if ((is_array($target)) && (array_key_exists($segment, $target))) {
/** @var T $target */
$target = $target[$segment];
} elseif (($target instanceof ArrayAccess) && (true === $target->offsetExists($segment))) {
} elseif (($target instanceof ArrayAccess) && ($target->offsetExists($segment))) {
/** @var T $target */
$target = $target[$segment];
} elseif ($target instanceof Collection) {
/** @var T $target */
$target = (Get::of()($segment)($default)($target->getIterator()))->current();
} elseif ((true === is_object($target)) && (true === property_exists($target, $segment))) {
} elseif ((is_object($target)) && (property_exists($target, $segment))) {
/** @var T $target */
$target = (new ReflectionClass($target))->getProperty($segment)->getValue($target);
} else {
Expand All @@ -107,7 +107,7 @@ static function (Iterator $iterator, $target, array $key, $default = null) use (
return $target;
};

$key = true === is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key;
$key = is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key;

foreach ($iterator as $value) {
yield $pick($iterator, $value, $key, $default);
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ static function (Iterator $iterator) use ($type, $callbacks): Generator {
$carry[] = $current;
}

if (Splitable::REMOVE === $type && true === $callbackReturn) {
if ($callbackReturn && (Splitable::REMOVE === $type)) {
yield $carry;

$carry = [];

continue;
}

if (true === $callbackReturn && [] !== $carry) {
if ($callbackReturn && ([] !== $carry)) {
yield $carry;

$carry = [];
Expand Down
10 changes: 8 additions & 2 deletions src/Operation/Truthy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ public function __invoke(): Closure
$matchWhenNot = static fn (): bool => true;
$matcher =
/**
* @param T $value
* @param bool $value
*/
static fn ($value): bool => !(bool) $value;
static fn (bool $value): bool => !$value;

/** @var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Map::of()(
/**
* @param T $value
*/
static fn ($value): bool => (bool) $value
),
MatchOne::of()($matchWhenNot)($matcher),
Map::of()($matcher),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/When.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public function __invoke(): Closure
*
* @return Generator<TKey, T>
*/
static fn (Iterator $iterator): Generator => yield from (true === $predicate($iterator)) ? $whenTrue($iterator) : $whenFalse($iterator);
static fn (Iterator $iterator): Generator => yield from ($predicate($iterator) ? $whenTrue($iterator) : $whenFalse($iterator));
}
}

0 comments on commit 11bee15

Please sign in to comment.