Skip to content

Commit

Permalink
refactor: Update Frequency operation return type and minor optimiza…
Browse files Browse the repository at this point in the history
…tions here and there.
  • Loading branch information
drupol committed Aug 28, 2021
1 parent bef9ed1 commit a1964f4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Contract/Operation/Frequencyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Frequencyable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#frequency
*
* @return Collection<TKey, T>
* @return Collection<int, T>
*/
public function frequency(): Collection;
}
6 changes: 2 additions & 4 deletions src/Operation/Frequency.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ static function (array $storage, $value): array {
break;
}

if (false !== $added) {
return $storage;
if (true !== $added) {
$storage[] = [1, $value];
}

$storage[] = [1, $value];

return $storage;
};

Expand Down
15 changes: 6 additions & 9 deletions src/Operation/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace loophp\collection\Operation;

use Closure;
use EmptyIterator;
use Generator;
use Iterator;

Expand Down Expand Up @@ -44,15 +43,13 @@ static function (Iterator $iterator): Generator {
break;
}

if (true === $isEmpty) {
return new EmptyIterator();
if (false === $isEmpty) {
/**
* @var TKey $key
* @var T $current
*/
return yield $key => $current;
}

/**
* @var TKey $key
* @var T $current
*/
return yield $key => $current;
};
}
}
15 changes: 6 additions & 9 deletions src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace loophp\collection\Operation;

use Closure;
use EmptyIterator;
use Generator;
use Iterator;

Expand Down Expand Up @@ -42,15 +41,13 @@ static function (Iterator $iterator): Generator {
$isEmpty = false;
}

if (true === $isEmpty) {
return new EmptyIterator();
if (false === $isEmpty) {
/**
* @var TKey $key
* @var T $current
*/
return yield $key => $current;
}

/**
* @var TKey $key
* @var T $current
*/
return yield $key => $current;
};
}
}

0 comments on commit a1964f4

Please sign in to comment.