Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Collection is Immutable & Operations Pure #122

Merged
merged 4 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
use const PHP_INT_MIN;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand All @@ -153,6 +155,8 @@ final class Collection implements CollectionInterface
private $source;

/**
* @psalm-external-mutation-free
*
* @param callable(mixed ...$parameters): iterable<TKey, T> $callable
* @param mixed ...$parameters
*/
Expand Down Expand Up @@ -400,6 +404,7 @@ public function frequency(): CollectionInterface
}

/**
* @pure
* @template NewTKey
* @template NewT
*
Expand All @@ -414,6 +419,8 @@ public static function fromCallable(callable $callable, ...$parameters): self
}

/**
* @pure
*
* @return self<int, string>
*/
public static function fromFile(string $filepath): self
Expand All @@ -425,6 +432,8 @@ public static function fromFile(string $filepath): self
}

/**
* @pure
*
* @template NewTKey
* @template NewT
*
Expand All @@ -441,6 +450,8 @@ public static function fromIterable(iterable $iterable): self
}

/**
* @pure
*
* @param resource $resource
*
* @return self<int, string>
Expand All @@ -457,6 +468,8 @@ public static function fromResource($resource): self
}

/**
* @pure
*
* @return self<int, string>
*/
public static function fromString(string $string, string $delimiter = ''): self
Expand Down
2 changes: 2 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
use loophp\collection\Contract\Operation\Zipable;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand Down
7 changes: 7 additions & 0 deletions src/Contract/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@

use Closure;

/** @immutable */
interface Operation
{
/**
* @pure
*/
public function __invoke(): Closure;

/**
* @pure
*/
public static function of(): Closure;
}
9 changes: 9 additions & 0 deletions src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
use Closure;
use loophp\collection\Contract\Operation;

/**
* @immutable
*/
abstract class AbstractOperation implements Operation
{
/**
* @pure
*/
final public function __construct()
{
}

/**
* @pure
*/
public static function of(): Closure
{
return (new static())->__invoke();
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use loophp\collection\Iterator\MultipleIterableIterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Append extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T...): Closure(Iterator<TKey, T>): Iterator<int|TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand All @@ -22,6 +24,8 @@
final class Apply extends AbstractOperation
{
/**
* @pure
*
* @return Closure(callable(T, TKey, Iterator<TKey, T>):bool ...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand All @@ -23,6 +25,8 @@
final class Associate extends AbstractOperation
{
/**
* @pure
*
* @return Closure(callable(TKey, TKey, T, Iterator<TKey, T>): (T|TKey) ...): Closure((callable(T, TKey, T, Iterator<TKey, T>): (T|TKey))...): Closure(Iterator<TKey, T>): Generator<TKey|T, T|TKey>
*/
public function __invoke(): Closure
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/AsyncMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
// phpcs:enable
/**
* Class AsyncMap.
* @immutable
*
* @template TKey
* @template T
Expand All @@ -37,6 +37,8 @@
final class AsyncMap extends AbstractOperation
{
/**
* @pure
*
* @return Closure(callable(T, TKey): T ...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Psr\Cache\CacheItemPoolInterface;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand All @@ -23,6 +25,8 @@
final class Cache extends AbstractOperation
{
/**
* @pure
*
* @return Closure(CacheItemPoolInterface): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
use function count;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Chunk extends AbstractOperation
{
/**
* @pure
*
* @return Closure(int...): Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Coalesce.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Coalesce extends AbstractOperation
{
/**
* @pure
*
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Collapse extends AbstractOperation
{
/**
* @pure
*
* @return Closure(Iterator<TKey, (T|iterable<TKey, T>)>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Column extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T): Closure(Iterator<TKey, T>): Generator<int, iterable<TKey, T>>
*/
public function __invoke(): Closure
Expand Down
5 changes: 5 additions & 0 deletions src/Operation/Combinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
use function count;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Combinate extends AbstractOperation
{
/**
* @pure
*/
public function __invoke(): Closure
{
return static function (?int $length = null): Closure {
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
use const E_USER_WARNING;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Combine extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T...): Closure(Iterator<TKey, T>): Generator<T, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use function in_array;

/**
* @immutable
*
* @template TKey
* @template T
*
Expand All @@ -24,6 +26,8 @@
final class Compact extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Contains extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T ...$values): Closure(Iterator<TKey, T>): Generator<TKey, bool>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Current extends AbstractOperation
{
/**
* @pure
*
* @return Closure(int $index): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Cycle extends AbstractOperation
{
/**
* @pure
*
* @return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
use function in_array;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class Diff extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand Down
Loading