diff --git a/bin/compile.php b/bin/compile.php new file mode 100644 index 0000000..0032440 --- /dev/null +++ b/bin/compile.php @@ -0,0 +1,44 @@ +startBuffering(); + + // Create the default stub from main.php entrypoint + $defaultStub = Phar::createDefaultStub('main.php'); + + // Add the rest of the apps files + $phar->buildFromDirectory(__DIR__ . '/app'); + + // Customize the stub to add the shebang + $stub = "#!/usr/bin/env php \n" . $defaultStub; + + // Add the stub + $phar->setStub($stub); + + $phar->stopBuffering(); + + // plus - compressing it into gzip + $phar->compressFiles(Phar::GZ); + + # Make the file executable + chmod(__DIR__ . '/app.phar', 0770); + + echo "$pharFile successfully created" . PHP_EOL; +} catch (\Throwable $e) { + echo $e->getMessage(); +} diff --git a/bin/compile.sh b/bin/compile.sh new file mode 100755 index 0000000..6c23bcb --- /dev/null +++ b/bin/compile.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e + +php --define phar.readonly=0 bin/compile.php diff --git a/composer.json b/composer.json index 89b9394..bcfa393 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require-dev": { "ergebnis/composer-normalize": "^2.40", - "lmc/coding-standard": "^3.3", + "lmc/coding-standard": "^4.1", "mockery/mockery": "^1.6.7", "php-parallel-lint/php-parallel-lint": "^1.3.2", "phpstan/extension-installer": "^1.3.1", @@ -54,13 +54,12 @@ ], "analyze": [ "@cs", - "vendor/bin/ecs check-markdown README.md --ansi", "@phpstan" ], - "cs": "vendor/bin/ecs check --ansi -- src/ tests/ ecs.php", + "cs": "vendor/bin/ecs check --ansi", "fix": [ "@composer normalize", - "vendor/bin/ecs check --ansi --clear-cache --fix -- src/ tests/ ecs.php" + "vendor/bin/ecs check --ansi --clear-cache --fix" ], "lint": [ "vendor/bin/parallel-lint -j 10 -e php ./src ./tests ecs.php", diff --git a/ecs.php b/ecs.php index 71957ee..650beeb 100644 --- a/ecs.php +++ b/ecs.php @@ -5,31 +5,29 @@ use Lmc\CodingStandard\Sniffs\Naming\InterfaceNameSniff; use PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer; use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symplify\EasyCodingStandard\ValueObject\Option; +use Symplify\EasyCodingStandard\Config\ECSConfig; -return static function (ContainerConfigurator $containerConfigurator): void { - $parameters = $containerConfigurator->parameters(); - - $parameters->set( - Option::SKIP, - [ - SelfAccessorFixer::class => null, - 'SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff.ReferencedGeneralException' => ['tests/Exception/*.php'], - 'PHP_CodeSniffer\Standards\Generic\Sniffs\Commenting\DocCommentSniff.TagsNotGrouped' => [ - 'src/Immutable/Generic/ISeq.php', // skip fixing the order of phpstan annotations - 'src/Immutable/Generic/Seq.php', // skip fixing the order of phpstan annotations - ], - InterfaceNameSniff::class => null, - 'SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff.MissingAnyTypeHint' => null, - 'SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff.MissingAnyTypeHint' => null, - 'SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSniff.MissingAnyTypeHint' => null, - PhpdocToParamTypeFixer::class => [ - 'src/Immutable/Tuple.php', - ], +return ECSConfig::configure() + ->withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withRootFiles() + ->withSets([ + __DIR__ . '/vendor/lmc/coding-standard/ecs.php', + ]) + ->withSkip([ + SelfAccessorFixer::class => null, + 'SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff.ReferencedGeneralException' => ['tests/Exception/*.php'], + 'PHP_CodeSniffer\Standards\Generic\Sniffs\Commenting\DocCommentSniff.TagsNotGrouped' => [ + 'src/Immutable/Generic/ISeq.php', // skip fixing the order of phpstan annotations + 'src/Immutable/Generic/Seq.php', // skip fixing the order of phpstan annotations ], - ); - - $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); - $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs-8.1.php'); -}; + InterfaceNameSniff::class => null, + 'SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff.MissingAnyTypeHint' => null, + 'SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff.MissingAnyTypeHint' => null, + 'SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSniff.MissingAnyTypeHint' => null, + PhpdocToParamTypeFixer::class => [ + 'src/Immutable/Tuple.php', + ], + ]); diff --git a/phpstan.neon b/phpstan.neon index ea35b9a..f731569 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -67,7 +67,7 @@ parameters: - message: '#Parameter \#3 \$index of method MF\\Collection\\Immutable\\Generic\\Seq::mapiValue\(\) expects int, mixed given.#' - count: 2 + count: 1 path: src/Immutable/Generic/Seq.php - @@ -87,7 +87,7 @@ parameters: - message: '#Generator expects key type int, mixed given.#' - count: 2 + count: 1 path: src/Immutable/Generic/Seq.php - diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php index 234920d..da224f4 100644 --- a/src/Exception/BadMethodCallException.php +++ b/src/Exception/BadMethodCallException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class BadMethodCallException extends \BadMethodCallException implements CollectionExceptionInterface -{ -} +class BadMethodCallException extends \BadMethodCallException implements CollectionExceptionInterface {} diff --git a/src/Exception/CollectionExceptionInterface.php b/src/Exception/CollectionExceptionInterface.php index 7f00064..91fd7f8 100644 --- a/src/Exception/CollectionExceptionInterface.php +++ b/src/Exception/CollectionExceptionInterface.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -interface CollectionExceptionInterface extends \Throwable -{ -} +interface CollectionExceptionInterface extends \Throwable {} diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index c3dfc40..fc803c7 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -6,6 +6,18 @@ class InvalidArgumentException extends \InvalidArgumentException implements CollectionExceptionInterface, AssertionFailedException { + /** @phpstan-param mixed[] $constraints */ + public function __construct( + string $message, + ?int $code = null, + private ?string $propertyPath = null, + private mixed $value = null, + private array $constraints = [], + ?\Throwable $previous = null, + ) { + parent::__construct($message, (int) $code, $previous); + } + public static function forFailedAssertion(AssertionFailedException $e): self { return new static( @@ -18,18 +30,6 @@ public static function forFailedAssertion(AssertionFailedException $e): self ); } - /** @phpstan-param mixed[] $constraints */ - public function __construct( - string $message, - int $code = null, - private ?string $propertyPath = null, - private mixed $value = null, - private array $constraints = [], - \Throwable $previous = null, - ) { - parent::__construct($message, (int) $code, $previous); - } - public function getPropertyPath(): ?string { return $this->propertyPath; diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php index 4312b29..78785aa 100644 --- a/src/Exception/LogicException.php +++ b/src/Exception/LogicException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class LogicException extends \LogicException implements CollectionExceptionInterface -{ -} +class LogicException extends \LogicException implements CollectionExceptionInterface {} diff --git a/src/Exception/OutOfBoundsException.php b/src/Exception/OutOfBoundsException.php index d55e12c..b9efe97 100644 --- a/src/Exception/OutOfBoundsException.php +++ b/src/Exception/OutOfBoundsException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class OutOfBoundsException extends \OutOfBoundsException implements CollectionExceptionInterface -{ -} +class OutOfBoundsException extends \OutOfBoundsException implements CollectionExceptionInterface {} diff --git a/src/Exception/OutOfRangeException.php b/src/Exception/OutOfRangeException.php index 0cf86eb..ecc6770 100644 --- a/src/Exception/OutOfRangeException.php +++ b/src/Exception/OutOfRangeException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class OutOfRangeException extends \OutOfRangeException implements CollectionExceptionInterface -{ -} +class OutOfRangeException extends \OutOfRangeException implements CollectionExceptionInterface {} diff --git a/src/Exception/TupleException.php b/src/Exception/TupleException.php index c6ad690..a7c3dd0 100644 --- a/src/Exception/TupleException.php +++ b/src/Exception/TupleException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class TupleException extends InvalidArgumentException implements TupleExceptionInterface -{ -} +class TupleException extends InvalidArgumentException implements TupleExceptionInterface {} diff --git a/src/Exception/TupleExceptionInterface.php b/src/Exception/TupleExceptionInterface.php index 3f747dc..ae0d193 100644 --- a/src/Exception/TupleExceptionInterface.php +++ b/src/Exception/TupleExceptionInterface.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -interface TupleExceptionInterface extends CollectionExceptionInterface -{ -} +interface TupleExceptionInterface extends CollectionExceptionInterface {} diff --git a/src/Exception/TupleParseException.php b/src/Exception/TupleParseException.php index 184fb23..52076a2 100644 --- a/src/Exception/TupleParseException.php +++ b/src/Exception/TupleParseException.php @@ -2,6 +2,4 @@ namespace MF\Collection\Exception; -class TupleParseException extends TupleException -{ -} +class TupleParseException extends TupleException {} diff --git a/src/Helper/Callback.php b/src/Helper/Callback.php index 4e9cfc7..3165bcf 100644 --- a/src/Helper/Callback.php +++ b/src/Helper/Callback.php @@ -23,7 +23,7 @@ public static function curry(callable $callback): callable $all = $ref->getNumberOfParameters(); $required = $ref->getNumberOfRequiredParameters(); - return fn (...$args) => $callback(...self::prepareArgs($all, $required, $args)); + return fn(...$args) => $callback(...self::prepareArgs($all, $required, $args)); } /** @see https://stackoverflow.com/questions/13071186/how-to-get-the-number-of-parameters-of-a-run-time-determined-callable */ diff --git a/src/Immutable/Generic/KVPair.php b/src/Immutable/Generic/KVPair.php index b51f1a2..684110a 100644 --- a/src/Immutable/Generic/KVPair.php +++ b/src/Immutable/Generic/KVPair.php @@ -11,6 +11,12 @@ */ readonly class KVPair { + /** + * @phpstan-param TKey $key + * @phpstan-param TValue $value + */ + public function __construct(private int|string $key, private mixed $value) {} + /** * @phpstan-param self $pair * @phpstan-return TKey @@ -34,14 +40,6 @@ public static function fromTuple(ITuple $tuple): static return new static($tuple->first(), $tuple->second()); } - /** - * @phpstan-param TKey $key - * @phpstan-param TValue $value - */ - public function __construct(private int|string $key, private mixed $value) - { - } - /** @phpstan-return TKey */ public function getKey(): int|string { @@ -54,7 +52,7 @@ public function getValue(): mixed return $this->value; } - public function asTuple(): ITUple + public function asTuple(): ITuple { return Tuple::of($this->key, $this->value); } diff --git a/src/Immutable/Generic/ListCollection.php b/src/Immutable/Generic/ListCollection.php index 6ca5c5b..00f4797 100644 --- a/src/Immutable/Generic/ListCollection.php +++ b/src/Immutable/Generic/ListCollection.php @@ -15,6 +15,9 @@ */ readonly class ListCollection implements IList { + /** @phpstan-param array $listArray */ + public function __construct(private array $listArray = []) {} + /** * @phpstan-template T * @phpstan-param IList> $list @@ -79,11 +82,6 @@ public static function create(iterable $source, callable $creator): IList return new static($listArray); } - /** @phpstan-param array $listArray */ - public function __construct(private array $listArray = []) - { - } - public function count(): int { return count($this->listArray); @@ -249,7 +247,7 @@ private function removeIndex(int $index): IList */ public function removeAll(mixed $value): IList { - return $this->filter(fn (mixed $val): bool => $value !== $val); + return $this->filter(fn(mixed $val): bool => $value !== $val); } /** @@ -374,7 +372,7 @@ public function sortBy(callable $callback): IList usort( $sorted, - fn (mixed $a, mixed $b): int => $callback($a) <=> $callback($b) + fn(mixed $a, mixed $b): int => $callback($a) <=> $callback($b), ); return static::from($sorted); @@ -391,7 +389,7 @@ public function sortByDescending(callable $callback): IList usort( $sorted, - fn (mixed $a, mixed $b): int => $callback($b) <=> $callback($a) + fn(mixed $a, mixed $b): int => $callback($b) <=> $callback($a), ); return static::from($sorted); @@ -455,7 +453,7 @@ public function sumBy(callable $callback): int|float $callback = Callback::curry($callback); return $this->reduce( - fn (int|float $sum, mixed $value, int $i): int|float => $sum + $callback($value, $i), + fn(int|float $sum, mixed $value, int $i): int|float => $sum + $callback($value, $i), 0, ); } @@ -574,7 +572,7 @@ public function collect(callable $callback): IList $callback = Callback::curry($callback); /** @phsptan-var IList> $collected */ - $collected = $this->map(fn (mixed $v): iterable => $callback($v)); + $collected = $this->map(fn(mixed $v): iterable => $callback($v)); /** @phpstan-var IList $concatenated */ $concatenated = $collected->concat(); diff --git a/src/Immutable/Generic/Map.php b/src/Immutable/Generic/Map.php index f2d675c..51f4eab 100644 --- a/src/Immutable/Generic/Map.php +++ b/src/Immutable/Generic/Map.php @@ -19,6 +19,9 @@ */ readonly class Map implements IMap { + /** @phpstan-param array $mapArray */ + public function __construct(private array $mapArray = []) {} + /** * @phpstan-param iterable $source * @phpstan-return IMap @@ -55,7 +58,7 @@ public static function fromPairs(iterable $pairs): IMap throw new InvalidArgumentException('Value is not a pair'); } - /** + /* * @phpstan-var TKey $key * @phpstan-var TValue $value */ @@ -84,11 +87,6 @@ public static function create(iterable $source, callable $creator): IMap return new static($mapArray); } - /** @phpstan-param array $mapArray */ - public function __construct(private array $mapArray = []) - { - } - public function count(): int { return count($this->mapArray); @@ -257,7 +255,7 @@ public function values(): IList public function pairs(): IList { /** @phpstan-var IList> $pairs */ - $pairs = ListCollection::create($this, fn ($value, $key) => new KVPair($key, $value)); + $pairs = ListCollection::create($this, fn($value, $key) => new KVPair($key, $value)); return $pairs; } @@ -330,7 +328,7 @@ public function asMutable(): \MF\Collection\Mutable\Generic\IMap public function toList(): IList { /** @phpstan-var IList $list */ - $list = ListCollection::create($this, fn ($value, $key) => Tuple::of($key, $value)); + $list = ListCollection::create($this, fn($value, $key) => Tuple::of($key, $value)); return $list; } diff --git a/src/Immutable/Generic/Seq.php b/src/Immutable/Generic/Seq.php index c0e55e5..a3144af 100644 --- a/src/Immutable/Generic/Seq.php +++ b/src/Immutable/Generic/Seq.php @@ -29,6 +29,12 @@ class Seq implements ISeq private bool $isInfinite = false; + /** @phpstan-param DataSource $iterable */ + public function __construct(private readonly iterable|\Closure $iterable) + { + $this->modifiers = []; + } + /** * @phpstan-template T * @phpstan-param ISeq> $seq @@ -259,12 +265,6 @@ function () use ($callable, $initialValue) { ); } - /** @phpstan-param DataSource $iterable */ - public function __construct(private readonly iterable|\Closure $iterable) - { - $this->modifiers = []; - } - /** @phpstan-return ISeq */ private function setIsInfinite(bool $isInfinite = true): ISeq { @@ -479,7 +479,7 @@ public function take(int $limit): ISeq private function clone(): static { - return clone ($this); + return clone $this; } private function addModifier(SeqModifier $type, mixed $modifier): static @@ -687,7 +687,7 @@ public function sortBy(callable $callback): ISeq usort( $items, - fn (mixed $a, mixed $b): int => $callback($a) <=> $callback($b) + fn(mixed $a, mixed $b): int => $callback($a) <=> $callback($b), ); yield from $items; @@ -712,7 +712,7 @@ public function sortByDescending(callable $callback): ISeq usort( $items, - fn (mixed $a, mixed $b): int => $callback($b) <=> $callback($a) + fn(mixed $a, mixed $b): int => $callback($b) <=> $callback($a), ); yield from $items; @@ -795,7 +795,7 @@ public function reverse(): ISeq public function sum(): int|float { return $this->reduce( - fn (int|float $sum, mixed $value) => $sum + $value, + fn(int|float $sum, mixed $value) => $sum + $value, 0, ); } @@ -806,7 +806,7 @@ public function sumBy(callable $callback): int|float $callback = Callback::curry($callback); return $this->reduce( - fn (int|float $sum, mixed $value) => $sum + $callback($value), + fn(int|float $sum, mixed $value) => $sum + $callback($value), 0, ); } diff --git a/src/Immutable/ITuple.php b/src/Immutable/ITuple.php index fdaa4d9..ef0b9f7 100644 --- a/src/Immutable/ITuple.php +++ b/src/Immutable/ITuple.php @@ -15,6 +15,11 @@ */ interface ITuple extends IEnumerable, \ArrayAccess, \Stringable { + /** + * @see Tuple::toString() + */ + public function __toString(): string; + public static function fst(ITuple $tuple): mixed; public static function snd(ITuple $tuple): mixed; @@ -34,7 +39,7 @@ public static function snd(ITuple $tuple): mixed; * * @throws TupleParseException */ - public static function parse(string $tuple, int $expectedItemsCount = null): self; + public static function parse(string $tuple, ?int $expectedItemsCount = null): self; /** * Parse "(x, y, ... z)" string into Tuple(x, y, z) and validates items types @@ -153,11 +158,6 @@ public static function merge(ITuple $base, mixed ...$additional): ITuple; */ public static function mergeMatch(array $types, ITuple $base, mixed ...$additional): ITuple; - /** - * @see Tuple::toString() - */ - public function __toString(): string; - /** * Transform tuple values into string (which is compatible with Tuple::parse() method) * @see Tuple::parse() diff --git a/src/Immutable/Tuple.php b/src/Immutable/Tuple.php index 02c1ffe..51e7683 100644 --- a/src/Immutable/Tuple.php +++ b/src/Immutable/Tuple.php @@ -14,6 +14,28 @@ { private const MINIMAL_TUPLE_ITEMS_COUNT = 2; + /** @phpstan-param mixed[] $values */ + private function __construct(private array $values) + { + try { + Assertion::greaterOrEqualThan( + count($values), + self::MINIMAL_TUPLE_ITEMS_COUNT, + 'Tuple must have at least two values.', + ); + } catch (AssertionFailedException $e) { + throw TupleException::forFailedAssertion($e); + } + } + + /** + * @see Tuple::toString() + */ + public function __toString(): string + { + return $this->toString(); + } + public static function fst(ITuple $tuple): mixed { return $tuple->first(); @@ -39,7 +61,7 @@ public static function snd(ITuple $tuple): mixed * * @throws TupleParseException */ - public static function parse(string $tuple, int $expectedItemsCount = null): ITuple + public static function parse(string $tuple, ?int $expectedItemsCount = null): ITuple { try { return self::parseTuple($tuple, $expectedItemsCount); @@ -65,7 +87,7 @@ private static function parseTuple(string $tuple, ?int $expectedItemsCount): sel $cache = []; /** @var string[] $parts */ - $parts = Seq::init(fn () => explode(',', trim($tuple, '()'))) + $parts = Seq::init(fn() => explode(',', trim($tuple, '()'))) ->reduce(function (array $matches, string $match) use (&$cache) { $trimmedMatch = ltrim($match); $isStart = (str_starts_with($trimmedMatch, '"') || str_starts_with($trimmedMatch, "'")) @@ -91,8 +113,8 @@ private static function parseTuple(string $tuple, ?int $expectedItemsCount): sel unset($cache); $values = Seq::from($parts) - ->map(fn (string $value) => trim($value)) - ->filter(fn ($match) => $match !== '') + ->map(fn(string $value) => trim($value)) + ->filter(fn($match) => $match !== '') ->map(self::mapParsedItem(...)) ->toArray(); @@ -295,20 +317,6 @@ public static function mergeMatch(array $types, ITuple $base, mixed ...$addition return $mergedTuple; } - /** @phpstan-param mixed[] $values */ - private function __construct(private array $values) - { - try { - Assertion::greaterOrEqualThan( - count($values), - self::MINIMAL_TUPLE_ITEMS_COUNT, - 'Tuple must have at least two values.', - ); - } catch (AssertionFailedException $e) { - throw TupleException::forFailedAssertion($e); - } - } - public function count(): int { return count($this->values); @@ -331,14 +339,6 @@ public function isEmpty(): bool return true; } - /** - * @see Tuple::toString() - */ - public function __toString(): string - { - return $this->toString(); - } - /** * Transform tuple values into string (which is compatible with Tuple::parse() method) * @see Tuple::parse() @@ -348,7 +348,7 @@ public function toString(): string return $this->formatToString( ', ', '; ', - fn (string $value): string => sprintf('"%s"', $value) + fn(string $value): string => sprintf('"%s"', $value), ); } @@ -367,9 +367,9 @@ public function toStringForUrl(): string return $this->formatToString( ',', ';', - fn (string $value): string => $this->isMatching('/^[a-zA-Z0-9.\-_ ]+$/', $value) + fn(string $value): string => $this->isMatching('/^[a-zA-Z0-9.\-_ ]+$/', $value) ? $value - : sprintf('"%s"', $value) + : sprintf('"%s"', $value), ); } @@ -547,7 +547,7 @@ private function getTypes(array $types): array /** @phpstan-return \Closure(string): string */ private function normalizeType(): \Closure { - return fn (string $type): string => Seq::create( + return fn(string $type): string => Seq::create( explode('|', $type), function (string $type): iterable { if (str_starts_with($type, '?')) { @@ -557,7 +557,7 @@ function (string $type): iterable { yield ltrim($type, '?'); }, ) - ->map(fn (string $type) => match ($type) { + ->map(fn(string $type) => match ($type) { 'integer' => 'int', 'boolean' => 'bool', 'double' => 'float', diff --git a/src/Mutable/Generic/ListCollection.php b/src/Mutable/Generic/ListCollection.php index abca4c9..0d7d302 100644 --- a/src/Mutable/Generic/ListCollection.php +++ b/src/Mutable/Generic/ListCollection.php @@ -18,6 +18,12 @@ */ class ListCollection implements IList { + /** @phpstan-param array $listArray */ + public function __construct(private array $listArray = []) + { + $this->listArray = array_values($this->listArray); + } + /** * @phpstan-param TValue $values * @phpstan-return IList @@ -61,12 +67,6 @@ public static function create(iterable $source, callable $creator): IList return $list; } - /** @phpstan-param array $listArray */ - public function __construct(private array $listArray = []) - { - $this->listArray = array_values($this->listArray); - } - public function count(): int { return count($this->listArray); @@ -234,7 +234,7 @@ private function normalize(): void /** @phpstan-param TValue $value */ public function removeAll(mixed $value): void { - $this->filter(fn (mixed $val): bool => $value !== $val); + $this->filter(fn(mixed $val): bool => $value !== $val); } /** @@ -324,7 +324,7 @@ public function sortBy(callable $callback): void usort( $this->listArray, - fn (mixed $a, mixed $b): int => $callback($a) <=> $callback($b) + fn(mixed $a, mixed $b): int => $callback($a) <=> $callback($b), ); } @@ -335,7 +335,7 @@ public function sortByDescending(callable $callback): void usort( $this->listArray, - fn (mixed $a, mixed $b): int => $callback($b) <=> $callback($a) + fn(mixed $a, mixed $b): int => $callback($b) <=> $callback($a), ); } @@ -388,7 +388,7 @@ public function sumBy(callable $callback): int|float $callback = Callback::curry($callback); return $this->reduce( - fn (int|float $sum, mixed $value, int $i): int|float => $sum + $callback($value, $i), + fn(int|float $sum, mixed $value, int $i): int|float => $sum + $callback($value, $i), 0, ); } diff --git a/src/Mutable/Generic/Map.php b/src/Mutable/Generic/Map.php index 4e35e39..e83fb8a 100644 --- a/src/Mutable/Generic/Map.php +++ b/src/Mutable/Generic/Map.php @@ -20,6 +20,9 @@ */ class Map implements IMap { + /** @phpstan-param array $mapArray */ + public function __construct(private array $mapArray = []) {} + /** * @phpstan-param iterable $source * @phpstan-return IMap @@ -86,11 +89,6 @@ public static function create(iterable $source, callable $creator): IMap return $map; } - /** @phpstan-param array $mapArray */ - public function __construct(private array $mapArray = []) - { - } - public function count(): int { return count($this->mapArray); @@ -250,7 +248,7 @@ public function values(): IList public function pairs(): IList { /** @phpstan-var IList> $pairs */ - $pairs = ListCollection::create($this, fn ($value, $key) => new KVPair($key, $value)); + $pairs = ListCollection::create($this, fn($value, $key) => new KVPair($key, $value)); return $pairs; } @@ -320,7 +318,7 @@ public function asImmutable(): \MF\Collection\Immutable\Generic\IMap public function toList(): IList { /** @phpstan-var IList $list */ - $list = ListCollection::create($this, fn ($value, $key) => Tuple::of($key, $value)); + $list = ListCollection::create($this, fn($value, $key) => Tuple::of($key, $value)); return $list; } diff --git a/src/Mutable/Generic/PrioritizedCollection.php b/src/Mutable/Generic/PrioritizedCollection.php index 7143df6..a5a0304 100644 --- a/src/Mutable/Generic/PrioritizedCollection.php +++ b/src/Mutable/Generic/PrioritizedCollection.php @@ -34,7 +34,7 @@ private function getItemsByPriority(): iterable $items = $this->items; usort( $items, - fn (Tuple $a, Tuple $b) => $b->second() <=> $a->second() + fn(Tuple $a, Tuple $b) => $b->second() <=> $a->second(), ); foreach ($items as [$item]) { diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index b7a4f6a..341b225 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -46,11 +46,11 @@ protected function forPHP(array $versionDifferences): mixed protected function findByKeyOrValue(mixed $needle): \Closure { - return fn ($key, $value) => $key === $needle || $value === $needle; + return fn($key, $value) => $key === $needle || $value === $needle; } protected function findByValue(mixed $needle): \Closure { - return fn ($value) => $value === $needle; + return fn($value) => $value === $needle; } } diff --git a/tests/Fixtures/ComplexEntity.php b/tests/Fixtures/ComplexEntity.php index f5bf3a3..6e55394 100644 --- a/tests/Fixtures/ComplexEntity.php +++ b/tests/Fixtures/ComplexEntity.php @@ -4,9 +4,7 @@ class ComplexEntity implements EntityInterface { - public function __construct(private SimpleEntity $simpleEntity) - { - } + public function __construct(private SimpleEntity $simpleEntity) {} public function getSimpleEntity(): SimpleEntity { diff --git a/tests/Fixtures/DummySeq.php b/tests/Fixtures/DummySeq.php index 5819f63..be9ce09 100644 --- a/tests/Fixtures/DummySeq.php +++ b/tests/Fixtures/DummySeq.php @@ -7,9 +7,7 @@ class DummySeq implements ISeq { - public function __construct(private readonly iterable $source) - { - } + public function __construct(private readonly iterable $source) {} public function forAll(callable $predicate): bool { diff --git a/tests/Fixtures/EntityInterface.php b/tests/Fixtures/EntityInterface.php index b70adf7..cb50aea 100644 --- a/tests/Fixtures/EntityInterface.php +++ b/tests/Fixtures/EntityInterface.php @@ -2,6 +2,4 @@ namespace MF\Collection\Fixtures; -interface EntityInterface -{ -} +interface EntityInterface {} diff --git a/tests/Fixtures/SimpleEntity.php b/tests/Fixtures/SimpleEntity.php index ecd6486..4155601 100644 --- a/tests/Fixtures/SimpleEntity.php +++ b/tests/Fixtures/SimpleEntity.php @@ -4,15 +4,13 @@ class SimpleEntity implements EntityInterface { + public function __construct(private int $id) {} + public static function create(int $id): self { return new self($id); } - public function __construct(private int $id) - { - } - public function getId(): int { return $this->id; diff --git a/tests/Helper/CallbackTest.php b/tests/Helper/CallbackTest.php index d194201..7d5b38a 100644 --- a/tests/Helper/CallbackTest.php +++ b/tests/Helper/CallbackTest.php @@ -72,9 +72,7 @@ function ($one, $two, $three = null, $four = null) { self::assertSame('three', $three); self::assertNull($four); }) { - public function __construct(private \Closure $assert) - { - } + public function __construct(private \Closure $assert) {} public function __invoke($one, $two, $three = null, $four = null) { diff --git a/tests/Immutable/Generic/ListTest.php b/tests/Immutable/Generic/ListTest.php index 988c2ed..90fb084 100644 --- a/tests/Immutable/Generic/ListTest.php +++ b/tests/Immutable/Generic/ListTest.php @@ -41,7 +41,7 @@ public static function provideChunkBySize(): array public function testShouldGroupList(): void { $groups = ListCollection::from([1, 2, 3, 4, 5, 6, 7]) - ->groupBy(fn (int $i) => $i % 2 === 0 ? 'even' : 'odd') + ->groupBy(fn(int $i) => $i % 2 === 0 ? 'even' : 'odd') ->toArray(); $expected = [ @@ -55,7 +55,7 @@ public function testShouldGroupList(): void public function testShouldGroupListAndUseOnlyEvenValues(): void { $eventValues = ListCollection::from([1, 2, 3, 4, 5, 6, 7]) - ->groupBy(fn (int $i) => $i % 2 === 0 ? 'even' : 'odd') + ->groupBy(fn(int $i) => $i % 2 === 0 ? 'even' : 'odd') ->map(KVPair::value(...)) ->last() ->toArray(); @@ -103,10 +103,10 @@ public function testShouldCreateListByCallback(): void { $list = ListCollection::create( explode(',', '1,2,3'), - fn ($value) => new SimpleEntity((int) $value) + fn($value) => new SimpleEntity((int) $value), ); - $list = $list->map(fn ($e) => $e->getId()); + $list = $list->map(fn($e) => $e->getId()); $this->assertSame([1, 2, 3], $list->toArray()); } @@ -152,7 +152,7 @@ public function testShouldGetFirstValueBy(): void public function testShouldGetFirstValueByArrowFunction(): void { - $findSecond = fn ($value) => $value === 'second'; + $findSecond = fn($value) => $value === 'second'; $this->assertNull($this->list->firstBy($findSecond)); @@ -175,7 +175,7 @@ public function testShouldContainsValueBy(): void $this->assertFalse($this->list->contains('value')); $this->list = $this->list->add('value'); - $this->assertTrue($this->list->containsBy(fn ($v) => $v === 'value')); + $this->assertTrue($this->list->containsBy(fn($v) => $v === 'value')); } public function testShouldRemoveFirstValue(): void @@ -228,7 +228,7 @@ public function testShouldMapToNewListWithSameGenericType(): void $this->list = $this->list->add('key2'); $this->list = $this->list->add('key3'); - $newList = $this->list->map(fn ($v) => $v . '_'); + $newList = $this->list->map(fn($v) => $v . '_'); $this->assertNotEquals($this->list, $newList); $this->assertEquals(['key_', 'key2_', 'key3_'], $newList->toArray()); @@ -240,7 +240,7 @@ public function testShouldFilterItemsToNewListByArrowFunction(): void $this->list = $this->list->add('key2'); $this->list = $this->list->add('key3'); - $newList = $this->list->filter(fn ($v, $i = null) => mb_strlen($v) > 3); + $newList = $this->list->filter(fn($v, $i = null) => mb_strlen($v) > 3); $this->assertNotEquals($this->list, $newList); $this->assertEquals(['key2', 'key3'], $newList->toArray()); @@ -252,8 +252,8 @@ public function testShouldCombineMapAndFilterToCreateNewMap(): void $this->list = $this->list->add('key2'); $newList = $this->list - ->filter(fn ($v, $i = null) => $v === 'key') - ->map(fn ($v) => $v . '_'); + ->filter(fn($v, $i = null) => $v === 'key') + ->map(fn($v) => $v . '_'); $this->assertNotEquals($this->list, $newList); $this->assertEquals(['key_'], $newList->toArray()); @@ -262,7 +262,7 @@ public function testShouldCombineMapAndFilterToCreateNewMap(): void /** @dataProvider provideValuesToChooseByItsIdentity */ public function testShouldChooseNotNullValues(array $values, array $expected): void { - $id = fn ($value) => $value; + $id = fn($value) => $value; $actual = ListCollection::from($values) ->choose($id) @@ -286,7 +286,7 @@ public static function provideValuesToChooseByItsIdentity(): array public function testShouldChooseValuesByCallback(): void { $evenValues = ListCollection::from([1, 2, 3, 4, 5]) - ->choose(fn (int $i) => $i % 2 === 0 ? $i : null) + ->choose(fn(int $i) => $i % 2 === 0 ? $i : null) ->toArray(); $this->assertSame([2, 4], $evenValues); @@ -298,7 +298,7 @@ public function testShouldNotMapListWithRequiredIndex(): void $this->list = $this->list->add('key'); - $this->list->map(fn ($v, $i) => $v . '_'); + $this->list->map(fn($v, $i) => $v . '_'); } public function testShouldIterateValues(): void @@ -319,7 +319,7 @@ public function testShouldReduceGenericList(): void $this->list = $this->list->add('key'); $this->list = $this->list->add('key2'); - $this->assertEquals('key|key2|', $this->list->reduce(fn ($t, $c) => $t . $c . '|')); + $this->assertEquals('key|key2|', $this->list->reduce(fn($t, $c) => $t . $c . '|')); } public function testShouldReduceGenericListOfListCounts(): void @@ -331,7 +331,7 @@ public function testShouldReduceGenericListOfListCounts(): void $list = $list->add($list1); $list = $list->add($list2); - $this->assertEquals(5, $list->reduce(fn ($t, $c) => $t + $c->count())); + $this->assertEquals(5, $list->reduce(fn($t, $c) => $t + $c->count())); } public function testShouldGetMutableGenericListAsImmutableGenericList(): void @@ -340,7 +340,7 @@ public function testShouldGetMutableGenericListAsImmutableGenericList(): void $mutable = $this->list->asMutable(); - $this->assertInstanceOf(\MF\Collection\Mutable\Generic\ListCollection::class, $mutable); + $this->assertInstanceOf(MutableListCollection::class, $mutable); $this->assertEquals($this->list->toArray(), $mutable->toArray()); } @@ -352,9 +352,9 @@ public function testShouldMapObjectsToDifferentList(): void $list = $list->add(new SimpleEntity(3)); $sumOfIdsGreaterThan1 = $list - ->filter(fn ($v, $i = null) => $v->getId() > 1) - ->map(fn ($v) => $v->getId()) - ->reduce(fn ($t, $v) => $t + $v); + ->filter(fn($v, $i = null) => $v->getId() > 1) + ->map(fn($v) => $v->getId()) + ->reduce(fn($t, $v) => $t + $v); $this->assertEquals(5, $sumOfIdsGreaterThan1); } @@ -367,9 +367,9 @@ public function testShouldMapObjectsToDifferentGenericList(): void $list = $list->add(new ComplexEntity(new SimpleEntity(3))); $sumOfIdsGreaterThan1 = $list - ->filter(fn ($v, $i = null) => $v->getSimpleEntity()->getId() > 1) - ->map(fn ($v) => $v->getSimpleEntity()) - ->reduce(fn ($t, $v) => $t + $v->getId()); + ->filter(fn($v, $i = null) => $v->getSimpleEntity()->getId() > 1) + ->map(fn($v) => $v->getSimpleEntity()) + ->reduce(fn($t, $v) => $t + $v->getId()); $this->assertEquals(5, $sumOfIdsGreaterThan1); } @@ -381,7 +381,7 @@ public function testShouldReduceListWithInitialValue(): void $list = $list->add(2); $list = $list->add(3); - $this->assertEquals(10 + 1 + 2 + 3, $list->reduce(fn ($t, $v) => $t + $v, 10)); + $this->assertEquals(10 + 1 + 2 + 3, $list->reduce(fn($t, $v) => $t + $v, 10)); } public function testShouldReduceListWithInitialValueToOtherType(): void @@ -391,7 +391,7 @@ public function testShouldReduceListWithInitialValueToOtherType(): void $list = $list->add(2); $list = $list->add(3); - $this->assertEquals('123', $list->reduce(fn ($t, $v) => $t . $v, '')); + $this->assertEquals('123', $list->reduce(fn($t, $v) => $t . $v, '')); } public function testShouldClearCollection(): void @@ -566,7 +566,7 @@ public function testShouldKeepOnlyUniqueValuesByCallback(): void new SimpleEntity(5), new SimpleEntity(1), ]); - $unique = $list->uniqueBy(fn (SimpleEntity $e) => $e->getId()); + $unique = $list->uniqueBy(fn(SimpleEntity $e) => $e->getId()); $this->assertNotEquals($list, $unique); $this->assertEquals( @@ -590,8 +590,8 @@ public function testShouldMapObjectsToDifferentListAndSumValues(): void ]); $list = $list - ->filter(fn (SimpleEntity $v, $i = null) => $v->getId() > 1) - ->map(fn (SimpleEntity $v) => $v->getId()); + ->filter(fn(SimpleEntity $v, $i = null) => $v->getId() > 1) + ->map(fn(SimpleEntity $v) => $v->getId()); $sum = $list->sum(); $this->assertSame(5, $sum); @@ -632,7 +632,7 @@ public function testShouldCountValuesByCallback(array $values, ?int $expectedOdd { $list = ListCollection::from($values); - $counts = $list->countBy(fn ($v) => $v % 2 === 0 ? 'even' : 'odd'); + $counts = $list->countBy(fn($v) => $v % 2 === 0 ? 'even' : 'odd'); $this->assertEquals( array_filter([ @@ -682,7 +682,7 @@ public function testShouldFindMinInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(1), $list->minBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(1), $list->minBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldFindMaxInListByCallback(): void @@ -693,7 +693,7 @@ public function testShouldFindMaxInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(3), $list->maxBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(3), $list->maxBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldAppendLists(): void @@ -713,9 +713,7 @@ public function testShouldAppendLists(): void public function testShouldCollectIntList(): void { $entity = new class([1, 2, 3]) { - public function __construct(private readonly array $data) - { - } + public function __construct(private readonly array $data) {} public function toArray(): array { @@ -724,7 +722,7 @@ public function toArray(): array }; $result = ListCollection::of($entity) - ->collect((fn ($s) => $s->toArray())) + ->collect(fn($s) => $s->toArray()) ->toArray(); $this->assertSame([1, 2, 3], $result); @@ -740,9 +738,9 @@ public function testShouldCollectList(): void ]; $word = ListCollection::from($data) - ->collect(fn (int $item) => $subData[$item]) + ->collect(fn(int $item) => $subData[$item]) ->reduce( - fn (string $word, string $subItem) => $word . $subItem, + fn(string $word, string $subItem) => $word . $subItem, 'Word: ', ); @@ -759,12 +757,12 @@ public function testShouldMapListCollectAndMapAgain(): void ]; $word = ListCollection::from($data) - ->map(fn (string $i) => (int) $i) - ->collect(fn (int $item) => $subData[$item]) - ->filter(fn ($l) => $l < 'f') - ->map(fn ($l) => $l . ' ') + ->map(fn(string $i) => (int) $i) + ->collect(fn(int $item) => $subData[$item]) + ->filter(fn($l) => $l < 'f') + ->map(fn($l) => $l . ' ') ->reduce( - fn (string $word, string $subItem) => $word . $subItem, + fn(string $word, string $subItem) => $word . $subItem, 'Word: ', ); @@ -790,10 +788,10 @@ public function testShouldConcatList(): void ]; $word = ListCollection::from($data) - ->map(fn (int $item) => $subData[$item]) + ->map(fn(int $item) => $subData[$item]) ->concat() ->reduce( - fn (string $word, string $subItem) => $word . $subItem, + fn(string $word, string $subItem) => $word . $subItem, 'Word: ', ); @@ -810,13 +808,13 @@ public function testShouldMapListConcatAndMapAgain(): void ]; $word = ListCollection::from($data) - ->map(fn ($i) => (int) $i) - ->map(fn (int $item) => $subData[$item]) + ->map(fn($i) => (int) $i) + ->map(fn(int $item) => $subData[$item]) ->concat() - ->filter(fn ($l) => $l < 'f') - ->map(fn ($l) => $l . ' ') + ->filter(fn($l) => $l < 'f') + ->map(fn($l) => $l . ' ') ->reduce( - fn (string $word, string $subItem) => $word . $subItem, + fn(string $word, string $subItem) => $word . $subItem, 'Word: ', ); @@ -861,7 +859,7 @@ public function testShouldMapListAndUseIndexInMapping(): void { $list = ListCollection::from([1, 2, 3, 4, 5]); - $list = $list->mapi(fn ($v, $i) => $i * $v); + $list = $list->mapi(fn($v, $i) => $i * $v); $this->assertSame([0, 2, 6, 12, 20], $list->toArray()); } @@ -880,20 +878,20 @@ public static function provideListForPartition(): array // list, predicate, expected 'empty' => [ new ListCollection(), - fn (int $i) => $i % 2 === 0, + fn(int $i) => $i % 2 === 0, [new ListCollection(), new ListCollection()], ], 'even/odd' => [ Seq::range('0..10')->toList(), - fn (int $i) => $i % 2 === 0, + fn(int $i) => $i % 2 === 0, [ ListCollection::from([0, 2, 4, 6, 8, 10]), ListCollection::from([1, 3, 5, 7, 9]), ], ], 'even/odd (no odds)' => [ - Seq::range('0..10')->toList()->filter(fn (int $i) => $i % 2 === 0), - fn (int $i) => $i % 2 === 0, + Seq::range('0..10')->toList()->filter(fn(int $i) => $i % 2 === 0), + fn(int $i) => $i % 2 === 0, [ ListCollection::from([0, 2, 4, 6, 8, 10]), ListCollection::from([]), @@ -901,7 +899,7 @@ public static function provideListForPartition(): array ], 'objects' => [ Seq::range('0..10')->toList()->map(SimpleEntity::create(...)), - fn (SimpleEntity $entity) => $entity->getId() > 5, + fn(SimpleEntity $entity) => $entity->getId() > 5, [ Seq::range('6..10')->toList()->map(SimpleEntity::create(...)), Seq::range('0..5')->toList()->map(SimpleEntity::create(...)), diff --git a/tests/Immutable/Generic/MapTest.php b/tests/Immutable/Generic/MapTest.php index 59aa735..5e28388 100644 --- a/tests/Immutable/Generic/MapTest.php +++ b/tests/Immutable/Generic/MapTest.php @@ -63,10 +63,10 @@ public function testShouldCreateMapByCallback(): void { $map = Map::create( explode(',', '1,2,3'), - fn ($value) => new SimpleEntity((int) $value) + fn($value) => new SimpleEntity((int) $value), ); - $map = $map->map(fn ($e, $k) => $e->getId()); + $map = $map->map(fn($e, $k) => $e->getId()); $this->assertSame([1, 2, 3], $map->toArray()); } @@ -123,8 +123,8 @@ public function testShouldContainsValueBy(): void $this->map = $this->map->set('key', $valueExists); - $this->assertTrue($this->map->containsBy(fn ($v, $k = null) => $v === $valueExists)); - $this->assertFalse($this->map->containsBy(fn ($v, $k = null) => $v === $valueDoesNotExist)); + $this->assertTrue($this->map->containsBy(fn($v, $k = null) => $v === $valueExists)); + $this->assertFalse($this->map->containsBy(fn($v, $k = null) => $v === $valueDoesNotExist)); } public function testShouldRemoveValueFromMap(): void @@ -145,7 +145,7 @@ public function testShouldMapToNewMapWithSameGenericType(): void $this->map = $this->map->set('key2', 2); $this->map = $this->map->set('key3', 3); - $newMap = $this->map->map(fn ($v, $k) => $v + 1); + $newMap = $this->map->map(fn($v, $k) => $v + 1); $this->assertNotEquals($this->map, $newMap); $this->assertEquals(['key' => 2, 'key2' => 3, 'key3' => 4], $newMap->toArray()); @@ -157,11 +157,11 @@ public function testShouldMapToNewMap(): void $map = $map->set('one', new SimpleEntity(1)); $map = $map->set('two', new SimpleEntity(2)); - $newMap = $map->map(fn ($v, $k) => $v->getId()); + $newMap = $map->map(fn($v, $k) => $v->getId()); $this->assertNotSame($map, $newMap); - $this->assertInstanceOf(\MF\Collection\Immutable\Generic\Map::class, $newMap); + $this->assertInstanceOf(Map::class, $newMap); $this->assertEquals(['one' => 1, 'two' => 2], $newMap->toArray()); } @@ -171,7 +171,7 @@ public function testShouldMapToNewGenericMap(): void $map = $map->set('one', new SimpleEntity(1)); $map = $map->set('two', new SimpleEntity(2)); - $newMap = $map->map(fn ($v, $k) => $v->getId()); + $newMap = $map->map(fn($v, $k) => $v->getId()); $this->assertNotSame($map, $newMap); @@ -185,7 +185,7 @@ public function testShouldFilterItemsToNewMapByArrowFunction(): void $this->map = $this->map->set('key2', 2); $this->map = $this->map->set('key3', 3); - $newMap = $this->map->filter(fn ($v, $k) => $v > 1); + $newMap = $this->map->filter(fn($v, $k) => $v > 1); $this->assertNotEquals($this->map, $newMap); $this->assertEquals(['key2' => 2, 'key3' => 3], $newMap->toArray()); @@ -197,8 +197,8 @@ public function testShouldCombineMapAndFilterToCreateNewMap(): void $this->map = $this->map->set('key2', 2); $newMap = $this->map - ->filter(fn ($v, $k) => $v > 1) - ->map(fn ($v, $k) => $v * 3); + ->filter(fn($v, $k) => $v > 1) + ->map(fn($v, $k) => $v * 3); $this->assertNotEquals($this->map, $newMap); $this->assertEquals(['key2' => 6], $newMap->toArray()); @@ -230,7 +230,7 @@ public function testShouldReduceMap(): void $this->map = $this->map->set('key2', 2); $this->map = $this->map->set('key3', 3); - $this->assertEquals(6, $this->map->reduce(fn ($t, $c) => $t + $c)); + $this->assertEquals(6, $this->map->reduce(fn($t, $c) => $t + $c)); } public function testShouldGetImmutableGenericMapAsMutableGenericMap(): void @@ -252,7 +252,7 @@ public function testShouldReduceMapWithInitialValue(): void $map = $map->set('two', 2); $map = $map->set('three', 3); - $this->assertEquals(10 + 1 + 2 + 3, $map->reduce(fn ($t, $v) => $t + $v, 10)); + $this->assertEquals(10 + 1 + 2 + 3, $map->reduce(fn($t, $v) => $t + $v, 10)); } public function testShouldReduceListWithInitialValueToOtherType(): void @@ -262,7 +262,7 @@ public function testShouldReduceListWithInitialValueToOtherType(): void $map = $map->set('two', 2); $map = $map->set('three', 3); - $this->assertEquals('123', $map->reduce(fn ($t, $v) => $t . $v, '')); + $this->assertEquals('123', $map->reduce(fn($t, $v) => $t . $v, '')); } public function testShouldClearCollection(): void diff --git a/tests/Immutable/Generic/SeqTest.php b/tests/Immutable/Generic/SeqTest.php index 8f8924b..8b4da0d 100644 --- a/tests/Immutable/Generic/SeqTest.php +++ b/tests/Immutable/Generic/SeqTest.php @@ -72,7 +72,7 @@ public function testShouldMakeSeqForRangeAndDoSquareOnInfiniteSeq(): void { $expectedValues = [1, 4, 9]; - $seq = Seq::forDo('1..Inf', fn ($i) => $i * $i) + $seq = Seq::forDo('1..Inf', fn($i) => $i * $i) ->take(3); $values = []; @@ -85,7 +85,7 @@ public function testShouldMakeSeqForRangeAndDoSquareOnInfiniteSeq(): void public function testShouldMakeSeqForRangeAndDoSquareOnInfiniteSeqSkippingFirstItems(): void { - $inf = Seq::forDo('1..7', fn ($i) => $i * $i); // 1, 4, 9, ... + $inf = Seq::forDo('1..7', fn($i) => $i * $i); // 1, 4, 9, ... $skip2 = $inf->skip(2); $take3 = $skip2->take(3); @@ -118,7 +118,7 @@ public function testShouldSkipMultipleTimesAndThenTakeSome(): void $array = Seq::range('1..Inf') ->skip(5) ->skip(5) - ->skipWhile(fn ($i) => $i % 2 === 0) + ->skipWhile(fn($i) => $i % 2 === 0) ->take(5) ->toArray(); @@ -143,7 +143,7 @@ public function testShouldGenerateSeqForRangeAndDoSquare(): void public function testShouldGenerateSeqFor(): void { - $result = Seq::forDo('0..10', fn () => yield 1)->take(5)->toArray(); + $result = Seq::forDo('0..10', fn() => yield 1)->take(5)->toArray(); $this->assertSame([1, 1, 1, 1, 1], $result); } @@ -161,7 +161,7 @@ public function testShouldGenerateSeqForEvenNumbers(): void public function testShouldGenerateSeqForGrid(): void { - /** + /* * F#: * let (height, width) = (10, 10) * seq { for row in 0 .. width - 1 do @@ -272,9 +272,9 @@ public function testShouldGenerateSeqForCube(): void public function testShouldSquareInfiniteWhile(): void { $result = Seq::infinite() - ->filter(fn ($i) => $i % 2 === 0) - ->map(fn ($i) => $i * $i) - ->takeWhile(fn ($i) => $i < 25) + ->filter(fn($i) => $i % 2 === 0) + ->map(fn($i) => $i * $i) + ->takeWhile(fn($i) => $i < 25) ->toArray(); $this->assertSame([4, 16], $result); @@ -283,10 +283,10 @@ public function testShouldSquareInfiniteWhile(): void public function testShouldSquareInfiniteWhileSkippingWhile(): void { $result = Seq::infinite() // 1, 2, 3, ... - ->filter(fn ($i) => $i % 2 === 0) // 2, 4, 6, ... - ->skipWhile(fn ($i) => $i < 10) // 10, 12, 14, ... - ->map(fn ($i) => $i * $i) // 100, 144, 169, ... - ->takeWhile(fn ($i) => $i < 150) // 100, 144 + ->filter(fn($i) => $i % 2 === 0) // 2, 4, 6, ... + ->skipWhile(fn($i) => $i < 10) // 10, 12, 14, ... + ->map(fn($i) => $i * $i) // 100, 144, 169, ... + ->takeWhile(fn($i) => $i < 150) // 100, 144 ->toArray(); $this->assertSame([100, 144], $result); @@ -295,12 +295,12 @@ public function testShouldSquareInfiniteWhileSkippingWhile(): void public function testShouldSquareInfiniteWhileAndThenMapIt(): void { $result = Seq::infinite() - ->filter(fn ($i) => $i % 2 === 0) // 2, 4, 6, ... Inf - ->map(fn ($i) => $i * $i) // 4, 16, 36 ... Inf - ->takeWhile(fn ($i) => $i < 25) // 4, 16 - ->map(fn ($i) => sqrt($i)) // 2.0, 4.0 - ->map(fn ($i) => (int) $i) // 2, 4 - ->filter(fn ($i) => $i > 2) // 4 + ->filter(fn($i) => $i % 2 === 0) // 2, 4, 6, ... Inf + ->map(fn($i) => $i * $i) // 4, 16, 36 ... Inf + ->takeWhile(fn($i) => $i < 25) // 4, 16 + ->map(fn($i) => sqrt($i)) // 2.0, 4.0 + ->map(fn($i) => (int) $i) // 2, 4 + ->filter(fn($i) => $i > 2) // 4 ->toArray(); $this->assertSame([4], $result); @@ -330,7 +330,7 @@ public function testShouldGenerateSeqForRange(): void { $expectedValues = [1, 2, 3]; - $seq = Seq::forDo([1, 3], fn ($i) => $i); + $seq = Seq::forDo([1, 3], fn($i) => $i); $values = []; foreach ($seq as $i) { @@ -344,7 +344,7 @@ public function testShouldCreateSeq(): void { $expectedValues = [1, 2, 3]; - $seq = Seq::create([1, 2, 3], fn ($i) => $i); + $seq = Seq::create([1, 2, 3], fn($i) => $i); $values = []; foreach ($seq as $i) { @@ -457,7 +457,7 @@ function (array $state) { public function testShouldGenerateSeqWithLimit(): void { $values = Seq::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) - ->takeWhile(fn ($i) => $i < 5) + ->takeWhile(fn($i) => $i < 5) ->toArray(); $this->assertSame([1, 2, 3, 4], $values); @@ -500,10 +500,10 @@ public function testShouldMapAndFilterRange(): void { $result = Seq::range('1..100') ->take(10) - ->filter(fn ($i) => $i % 2 === 0)// 2, 4, 6, 8, 10 - ->map(fn ($i) => $i * $i)// 4, 16, 36, 64, 100 - ->map(fn ($i) => $i . '_item')// '4_item', '16_item', '36_item', '64_item', '100_item' - ->filter(fn ($i) => $i[0] !== '1')// '4_item', '36_item', '64_item' + ->filter(fn($i) => $i % 2 === 0)// 2, 4, 6, 8, 10 + ->map(fn($i) => $i * $i)// 4, 16, 36, 64, 100 + ->map(fn($i) => $i . '_item')// '4_item', '16_item', '36_item', '64_item', '100_item' + ->filter(fn($i) => $i[0] !== '1')// '4_item', '36_item', '64_item' ->take(2)// '4_item', '36_item' ->toArray(); @@ -517,7 +517,7 @@ public function testShouldGenerateLimitedNumbers(): void yield 2; yield 3; }) - ->map(fn ($i) => 'item_' . $i) + ->map(fn($i) => 'item_' . $i) ->take(2) ->toArray(); @@ -601,7 +601,7 @@ public function testShouldGenerateInfiniteRangeUntilLimit(): void public function testShouldGenerateInfiniteRangeByStringDefinitionUntilLimit(): void { $result = Seq::range('1..Inf') - ->takeWhile(fn ($i) => $i < 100) + ->takeWhile(fn($i) => $i < 100) ->toArray(); $this->assertCount(99, $result); @@ -629,7 +629,7 @@ public static function provideIsEmpty(): array 'empty' => [Seq::createEmpty(), true], 'empty from' => [Seq::from([]), true], 'not empty infinite' => [Seq::infinite(), false], - 'not empty init' => [Seq::init(fn () => yield 1), false], + 'not empty init' => [Seq::init(fn() => yield 1), false], 'not empty init with generator' => [ Seq::init(function () { foreach ([1, 2, 3] as $i) { @@ -650,8 +650,8 @@ public static function provideIsEmpty(): array 'empty after clear' => [Seq::of(1, 2)->clear(), true], 'empty after take 0' => [Seq::range('1..Inf')->take(0), true], 'empty after takeUpTo 0' => [Seq::from([1, 2])->takeUpTo(0), true], - 'empty after takeWhile' => [Seq::infinite()->takeWhile(fn () => false), true], - 'empty after filterAll' => [Seq::range('1..10..100')->filter(fn () => false), true], + 'empty after takeWhile' => [Seq::infinite()->takeWhile(fn() => false), true], + 'empty after filterAll' => [Seq::range('1..10..100')->filter(fn() => false), true], ]; } @@ -668,10 +668,10 @@ public static function provideCount(): array return [ // seq, expectedCount 'count' => [Seq::createEmpty(), 0], - 'count create' => [Seq::create([], fn ($i) => $i), 0], + 'count create' => [Seq::create([], fn($i) => $i), 0], 'count from' => [Seq::from([]), 0], 'count from 2' => [Seq::from([1, 2]), 2], - 'count init' => [Seq::init(fn () => yield 1), 1], + 'count init' => [Seq::init(fn() => yield 1), 1], 'count init with generator' => [ Seq::init(function () { foreach ([1, 2, 3] as $i) { @@ -691,13 +691,13 @@ public static function provideCount(): array 'count after clear' => [Seq::of(1, 2)->clear(), 0], 'count after take 0' => [Seq::range('1..Inf')->take(0), 0], 'count after takeUpTo 0' => [Seq::from([1, 2])->takeUpTo(0), 0], - 'count after takeWhile' => [Seq::infinite()->takeWhile(fn () => false), 0], + 'count after takeWhile' => [Seq::infinite()->takeWhile(fn() => false), 0], 'count forDo on infinite while' => [ - Seq::forDo('0..10..Inf', fn ($i) => yield $i => 'item_' . $i)->takeWhile(fn ($i, $k = null) => $k < 100), + Seq::forDo('0..10..Inf', fn($i) => yield $i => 'item_' . $i)->takeWhile(fn($i, $k = null) => $k < 100), 10, ], 'count forDo on infinite' => [ - Seq::forDo('0..10..Inf', fn ($i) => yield $i)->take(10), + Seq::forDo('0..10..Inf', fn($i) => yield $i)->take(10), 10, ], 'large - count range' => [ @@ -709,15 +709,15 @@ public static function provideCount(): array 1000, ], 'large - count forDo on infinite while' => [ - Seq::forDo('0..10..Inf', fn ($i) => yield $i => 'item_' . $i)->takeWhile(fn ($i, $k = null) => $k < 10000), + Seq::forDo('0..10..Inf', fn($i) => yield $i => 'item_' . $i)->takeWhile(fn($i, $k = null) => $k < 10000), 1000, ], 'large - count forDo on infinite' => [ - Seq::forDo('0..10..Inf', fn ($i) => yield 'item_' . $i)->take(1000), + Seq::forDo('0..10..Inf', fn($i) => yield 'item_' . $i)->take(1000), 1000, ], 'large - count create' => [ - Seq::create(range(1, 1000), fn ($i) => yield 'item_' . $i), + Seq::create(range(1, 1000), fn($i) => yield 'item_' . $i), 1000, ], 'seq of seq' => [ @@ -738,9 +738,9 @@ public function testShouldThrowExceptionOnCountingInfiniteSeq(): void public function testShouldReduceSeq(): void { $sumOfSquaredOddNumbersFrom1to10 = Seq::range('1..10') - ->filter(fn ($i) => $i % 2 === 1)// 1, 3, 5, 7, 9 - ->map(fn ($i) => $i * $i)// 1, 9, 25, 49, 81 - ->reduce(fn ($t, $i) => $t + $i, 0); + ->filter(fn($i) => $i % 2 === 1)// 1, 3, 5, 7, 9 + ->map(fn($i) => $i * $i)// 1, 9, 25, 49, 81 + ->reduce(fn($t, $i) => $t + $i, 0); $this->assertSame(165, $sumOfSquaredOddNumbersFrom1to10); } @@ -756,12 +756,12 @@ public function testShouldContainsValue(ISeq $seq, mixed $value, mixed $expected public static function provideContains(): array { return [ - /// seq, value, expected + // / seq, value, expected 'empty' => [Seq::createEmpty(), true, false], 'in range' => [Seq::range('1..10'), 5, true], 'not in range' => [Seq::range('1..10'), 11, false], 'in infinite' => [Seq::range('0..10..Inf'), 100, true], - 'not in filtered range' => [Seq::range('0..10..100')->filter(fn ($i) => $i !== 100), 100, false], + 'not in filtered range' => [Seq::range('0..10..100')->filter(fn($i) => $i !== 100), 100, false], 'not in limited range' => [Seq::range('0..10..100')->take(2), 30, false], 'in static' => [Seq::from(['hello']), 'hello', true], ]; @@ -777,18 +777,18 @@ public function testShouldContainsValueBy(ISeq $seq, mixed $value, mixed $expect public function testShouldContainsValueByArrowFunction(): void { - $seq = Seq::from(['1', '2']); - $contains2NonStrict = $seq->containsBy(fn ($v) => $v == 2); - $contains3NonStrict = $seq->containsBy(fn ($v) => $v == 3); + $seq = Seq::from([1, 2]); + $contains2 = $seq->containsBy(fn($v) => $v === 2); + $contains3 = $seq->containsBy(fn($v) => $v === 3); - $this->assertTrue($contains2NonStrict); - $this->assertFalse($contains3NonStrict); + $this->assertTrue($contains2); + $this->assertFalse($contains3); } public function testShouldCheckIfNumberFromRangeIsOdd(): void { $result = Seq::range('3..2..Inf') - ->filter(fn ($i) => $i % 2 === 1) + ->filter(fn($i) => $i % 2 === 1) ->take(5) ->toArray(); @@ -798,9 +798,7 @@ public function testShouldCheckIfNumberFromRangeIsOdd(): void public function testShouldCollectIntSequence(): void { $entity = new class([1, 2, 3]) { - public function __construct(private readonly array $data) - { - } + public function __construct(private readonly array $data) {} public function toArray(): array { @@ -809,7 +807,7 @@ public function toArray(): array }; $result = Seq::of($entity) - ->collect((fn ($s) => $s->toArray())) + ->collect(fn($s) => $s->toArray()) ->toArray(); $this->assertSame([1, 2, 3], $result); @@ -849,12 +847,12 @@ public function testShouldMapSequenceCollectAndMapAgain(): void $word = Seq::init(function () use ($data): iterable { yield from $data; }) - ->map(fn ($i) => (int) $i) + ->map(fn($i) => (int) $i) ->collect(function (int $item) use ($subData): iterable { return $subData[$item]; }) - ->filter(fn ($l) => $l < 'f') - ->map(fn ($l) => $l . ' ') + ->filter(fn($l) => $l < 'f') + ->map(fn($l) => $l . ' ') ->reduce(function (string $word, string $subItem): string { return $word . $subItem; }, 'Word: '); @@ -883,9 +881,9 @@ public function testShouldConcatSequence(): void $word = Seq::init(function () use ($data): iterable { yield from $data; }) - ->map(fn (int $item): iterable => $subData[$item]) + ->map(fn(int $item): iterable => $subData[$item]) ->concat() - ->reduce(fn (string $word, string $subItem): string => $word . $subItem, 'Word: '); + ->reduce(fn(string $word, string $subItem): string => $word . $subItem, 'Word: '); $this->assertSame('Word: abcdefg', $word); } @@ -902,12 +900,12 @@ public function testShouldMapSequenceConcatAndMapAgain(): void $word = Seq::init(function () use ($data): iterable { yield from $data; }) - ->map(fn ($i) => (int) $i) - ->map(fn (int $item): iterable => $subData[$item]) + ->map(fn($i) => (int) $i) + ->map(fn(int $item): iterable => $subData[$item]) ->concat() - ->filter(fn ($l) => $l < 'f') - ->map(fn ($l) => $l . ' ') - ->reduce(fn (string $word, string $subItem): string => $word . $subItem, 'Word: '); + ->filter(fn($l) => $l < 'f') + ->map(fn($l) => $l . ' ') + ->reduce(fn(string $word, string $subItem): string => $word . $subItem, 'Word: '); $this->assertSame('Word: a b c d e ', $word); } @@ -939,7 +937,7 @@ public function testShouldImplodeSeq(): void public function testShouldCreateSequenceByArrowFunctionWithGenerator(): void { - $result = Seq::init(fn () => yield from range(1, 5))->toArray(); + $result = Seq::init(fn() => yield from range(1, 5))->toArray(); $this->assertSame([1, 2, 3, 4, 5], $result); } @@ -958,8 +956,8 @@ public function testShouldAccessEachItemInInfiniteSeqWhileConditionIsMet(): void $data = []; Seq::range('0 .. Inf') - ->map(fn ($i) => $i * 2) - ->takeWhile(fn ($i) => $i < 10) + ->map(fn($i) => $i * 2) + ->takeWhile(fn($i) => $i < 10) ->each(function ($i) use (&$data): void { $data[] = $i; }); @@ -972,8 +970,8 @@ public function testShouldIterateTheSeqMoreThanOnce(): void $data = []; $infiniteRange = Seq::range('0 .. Inf'); - $infiniteRangeDoubled = $infiniteRange->map(fn ($i) => $i * 2); - $infiniteRangeTripled = $infiniteRange->map(fn ($i) => $i * 3); + $infiniteRangeDoubled = $infiniteRange->map(fn($i) => $i * 2); + $infiniteRangeTripled = $infiniteRange->map(fn($i) => $i * 3); foreach ($infiniteRangeDoubled->take(5) as $i) { $this->ignore($i); @@ -983,13 +981,13 @@ public function testShouldIterateTheSeqMoreThanOnce(): void } $infiniteRangeDoubled - ->takeWhile(fn ($i) => $i < 10) + ->takeWhile(fn($i) => $i < 10) ->each(function ($i) use (&$data): void { $data[] = $i; }); $infiniteRangeTripled - ->takeWhile(fn ($i) => $i < 10) + ->takeWhile(fn($i) => $i < 10) ->each(function ($i) use (&$data): void { $data[] = $i; }); @@ -999,13 +997,13 @@ public function testShouldIterateTheSeqMoreThanOnce(): void public function testShouldTransformSeqToList(): void { - $list = Seq::forDo('1..5', fn ($i) => $i)->toList(); + $list = Seq::forDo('1..5', fn($i) => $i)->toList(); $this->assertSame([1, 2, 3, 4, 5], $list->toArray()); } public function testShouldTransformInfiniteSeqToList(): void { - $list = Seq::forDo('1..Inf', fn ($i) => $i)->take(5)->toList(); + $list = Seq::forDo('1..Inf', fn($i) => $i)->take(5)->toList(); $this->assertSame([1, 2, 3, 4, 5], $list->toArray()); } @@ -1013,7 +1011,7 @@ public function testShouldNotTransformInfiniteSeqToList(): void { $this->expectException(OutOfBoundsException::class); - Seq::forDo('1..Inf', fn ($i) => $i)->toList(); + Seq::forDo('1..Inf', fn($i) => $i)->toList(); } public function testShouldCheckPredicateForAllItems(): void @@ -1079,8 +1077,8 @@ public function testShouldNotSortDescendingInfiniteSeq(): void public function testShouldReverseGeneratedSequence(): void { - $seq = Seq::forDo('1..2..Inf', fn ($i) => $i) - ->takeWhile(fn ($i) => $i < 10) + $seq = Seq::forDo('1..2..Inf', fn($i) => $i) + ->takeWhile(fn($i) => $i < 10) ->reverse(); $this->assertSame([9, 7, 5, 3, 1], $seq->toArray()); @@ -1210,7 +1208,7 @@ public function testShouldKeepOnlyUniqueValuesByCallback(): void new SimpleEntity(5), new SimpleEntity(1), ]); - $unique = $seq->uniqueBy(fn (SimpleEntity $e) => $e->getId()); + $unique = $seq->uniqueBy(fn(SimpleEntity $e) => $e->getId()); $this->assertNotEquals($seq, $unique); $this->assertEquals( @@ -1234,8 +1232,8 @@ public function testShouldMapObjectsToDifferentSequenceAndSumValues(): void ]); $seq = $seq - ->filter(fn (SimpleEntity $v) => $v->getId() > 1) - ->map(fn (SimpleEntity $v) => $v->getId()); + ->filter(fn(SimpleEntity $v) => $v->getId() > 1) + ->map(fn(SimpleEntity $v) => $v->getId()); $sum = $seq->sum(); $this->assertSame(5, $sum); @@ -1279,7 +1277,7 @@ public function testShouldCountValuesByCallback(): void { $seq = Seq::from([1, 3, 5, 7, 2, 3, 5, 4, 8, 2]); - $counts = $seq->countBy(fn ($v) => $v % 2 === 0 ? 'even' : 'odd'); + $counts = $seq->countBy(fn($v) => $v % 2 === 0 ? 'even' : 'odd'); $this->assertEquals( [ @@ -1293,7 +1291,7 @@ public function testShouldCountValuesByCallback(): void public function testShouldGroupList(): void { $groupsSeq = Seq::from([1, 2, 3, 4, 5, 6, 7]) - ->groupBy(fn (int $i) => $i % 2 === 0 ? 'even' : 'odd') + ->groupBy(fn(int $i) => $i % 2 === 0 ? 'even' : 'odd') ->toArray(); $expected = [ @@ -1336,7 +1334,7 @@ public function testShouldFindMinInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(1), $seq->minBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(1), $seq->minBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldFindMaxInListByCallback(): void @@ -1347,7 +1345,7 @@ public function testShouldFindMaxInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(3), $seq->maxBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(3), $seq->maxBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldGenerateSequenceMultipleTimes(): void @@ -1377,8 +1375,8 @@ public function testShouldGenerateSequenceMultipleTimes(): void $isGenerated = false; $seq2 = $seq - ->map(fn ($i) => $i) - ->filter(fn ($i) => $i % 2 === 0) + ->map(fn($i) => $i) + ->filter(fn($i) => $i % 2 === 0) ->append(Seq::from([4, 6])); $this->assertFalse($isGenerated); $this->assertSame([2, 4, 6], $seq2->toArray()); @@ -1389,7 +1387,7 @@ public function testShouldMapSeqAndUseIndexInMapping(): void { $seq = Seq::from([1, 2, 3, 4, 5]); - $seq = $seq->mapi(fn ($v, $i) => $i * $v); + $seq = $seq->mapi(fn($v, $i) => $i * $v); $this->assertSame([0, 2, 6, 12, 20], $seq->toArray()); } @@ -1402,7 +1400,7 @@ public function testShouldMapGeneratedSeqAndUseIndexInMapping(): void } }); - $seq = $seq->mapi(fn ($v, $i) => $i * $v); + $seq = $seq->mapi(fn($v, $i) => $i * $v); $this->assertSame([0, 2, 6, 12, 20], $seq->toArray()); } @@ -1410,10 +1408,10 @@ public function testShouldMapGeneratedSeqAndUseIndexInMapping(): void public function testShouldChangeInfiniteSequenceExampleFromReadme(): void { $result = Seq::infinite() // 1, 2, ... - ->filter(fn ($i) => $i % 2 === 0) // 2, 4, ... + ->filter(fn($i) => $i % 2 === 0) // 2, 4, ... ->skip(2) // 6, 8, ... - ->map(fn ($i) => $i * $i) // 36, 64, ... - ->takeWhile(fn ($i) => $i < 100) // 36, 64 + ->map(fn($i) => $i * $i) // 36, 64, ... + ->takeWhile(fn($i) => $i < 100) // 36, 64 ->reverse() // 64, 36 ->take(1); // 64 diff --git a/tests/Immutable/TupleTest.php b/tests/Immutable/TupleTest.php index 6280571..69a73bb 100644 --- a/tests/Immutable/TupleTest.php +++ b/tests/Immutable/TupleTest.php @@ -658,14 +658,14 @@ public function testShouldUnpackTuple(): void public function testShouldUnpackTuples(): void { - $format = fn (string $title, string $value) => sprintf('%s: %s', $title, $value); + $format = fn(string $title, string $value) => sprintf('%s: %s', $title, $value); $values = [ Tuple::of('title', 'value'), Tuple::of('type', 'great'), ]; - $result = array_map(fn (Tuple $tuple) => $format(...$tuple), $values); + $result = array_map(fn(Tuple $tuple) => $format(...$tuple), $values); $this->assertSame(['title: value', 'type: great'], $result); } diff --git a/tests/Mutable/Generic/ListTest.php b/tests/Mutable/Generic/ListTest.php index 6fdb176..7667be3 100644 --- a/tests/Mutable/Generic/ListTest.php +++ b/tests/Mutable/Generic/ListTest.php @@ -83,10 +83,10 @@ public function testShouldCreateListByCallback(): void { $list = ListCollection::create( explode(',', '1,2,3'), - fn ($value) => new SimpleEntity((int) $value) + fn($value) => new SimpleEntity((int) $value), ); - $list->map(fn (SimpleEntity $e) => $e->getId()); + $list->map(fn(SimpleEntity $e) => $e->getId()); $this->assertSame([1, 2, 3], $list->toArray()); } @@ -117,7 +117,7 @@ public function testShouldGetFirstValue(): void public function testShouldGetFirstValueBy(): void { - $findSecond = fn ($value) => $value === 'second'; + $findSecond = fn($value) => $value === 'second'; $this->assertNull($this->list->firstBy($findSecond)); @@ -129,7 +129,7 @@ public function testShouldGetFirstValueBy(): void public function testShouldGetFirstValueByArrowFunction(): void { - $findSecond = fn ($value) => $value === 'second'; + $findSecond = fn($value) => $value === 'second'; $this->assertNull($this->list->firstBy($findSecond)); @@ -152,7 +152,7 @@ public function testShouldContainsValueBy(): void $this->assertFalse($this->list->contains('value')); $this->list->add('value'); - $this->assertTrue($this->list->containsBy(fn ($v) => $v === 'value')); + $this->assertTrue($this->list->containsBy(fn($v) => $v === 'value')); } public function testShouldNotContainsValueBy(): void @@ -160,7 +160,7 @@ public function testShouldNotContainsValueBy(): void $this->assertFalse($this->list->contains('value')); $this->list->add('value'); - $this->assertFalse($this->list->containsBy(fn ($v) => $v === 'not-there')); + $this->assertFalse($this->list->containsBy(fn($v) => $v === 'not-there')); } public function testShouldRemoveFirstValue(): void @@ -198,7 +198,7 @@ public function testShouldMapToNewListWithSameGenericType(): void $this->list->add('key2'); $this->list->add('key3'); - $this->list->map(fn ($v) => $v . '_'); + $this->list->map(fn($v) => $v . '_'); $this->assertEquals(['key_', 'key2_', 'key3_'], $this->list->toArray()); } @@ -209,7 +209,7 @@ public function testShouldFilterItemsToNewListByArrowFunction(): void $this->list->add('key2'); $this->list->add('key3'); - $this->list->filter(fn ($v, $i = null) => mb_strlen($v) > 3); + $this->list->filter(fn($v, $i = null) => mb_strlen($v) > 3); $this->assertEquals(['key2', 'key3'], $this->list->toArray()); } @@ -219,8 +219,8 @@ public function testShouldCombineMapAndFilterToCreateNewMap(): void $this->list->add('key'); $this->list->add('key2'); - $this->list->filter(fn ($v, $i = null) => $v === 'key'); - $this->list->map(fn ($v) => $v . '_'); + $this->list->filter(fn($v, $i = null) => $v === 'key'); + $this->list->map(fn($v) => $v . '_'); $this->assertEquals(['key_'], $this->list->toArray()); } @@ -231,7 +231,7 @@ public function testShouldNotMapListWithRequiredIndex(): void $this->list->add('key'); - $this->list->map(fn ($v, $i) => $v . '_'); + $this->list->map(fn($v, $i) => $v . '_'); } public function testShouldIterateValues(): void @@ -252,7 +252,7 @@ public function testShouldReduceGenericList(): void $this->list->add('key'); $this->list->add('key2'); - $this->assertEquals('key|key2|', $this->list->reduce(fn ($acc, $v) => $acc . $v . '|')); + $this->assertEquals('key|key2|', $this->list->reduce(fn($acc, $v) => $acc . $v . '|')); } public function testShouldReduceGenericListOfListCounts(): void @@ -264,7 +264,7 @@ public function testShouldReduceGenericListOfListCounts(): void $list->add($list1); $list->add($list2); - $this->assertEquals(5, $list->reduce(fn ($t, $c) => $t + $c->count())); + $this->assertEquals(5, $list->reduce(fn($t, $c) => $t + $c->count())); } public function testShouldSumGenericListOfListCountsByCallback(): void @@ -298,8 +298,8 @@ public function testShouldMapObjectsToDifferentListAndSumValues(): void $list->add(new SimpleEntity(2)); $list->add(new SimpleEntity(3)); - $list->filter(fn (SimpleEntity $v, $i = null) => $v->getId() > 1); - $list->map(fn (SimpleEntity $v) => $v->getId()); + $list->filter(fn(SimpleEntity $v, $i = null) => $v->getId() > 1); + $list->map(fn(SimpleEntity $v) => $v->getId()); $sum = $list->sum(); $this->assertSame(5, $sum); @@ -313,9 +313,9 @@ public function testShouldMapObjectsToDifferentGenericListAndSumValuesByCallback $list->add(new ComplexEntity(new SimpleEntity(2))); $list->add(new ComplexEntity(new SimpleEntity(3))); - $list->filter(fn (ComplexEntity $v, $i = null) => $v->getSimpleEntity()->getId() > 1); - $list->map(fn (ComplexEntity $v) => $v->getSimpleEntity()); - $sum = $list->sumBy(fn (SimpleEntity $e) => $e->getId()); + $list->filter(fn(ComplexEntity $v, $i = null) => $v->getSimpleEntity()->getId() > 1); + $list->map(fn(ComplexEntity $v) => $v->getSimpleEntity()); + $sum = $list->sumBy(fn(SimpleEntity $e) => $e->getId()); $this->assertEquals(5, $sum); } @@ -327,7 +327,7 @@ public function testShouldReduceListWithInitialValue(): void $list->add(2); $list->add(3); - $this->assertEquals(10 + 1 + 2 + 3, $list->reduce(fn ($t, $v) => $t + $v, 10)); + $this->assertEquals(10 + 1 + 2 + 3, $list->reduce(fn($t, $v) => $t + $v, 10)); } public function testShouldReduceListWithInitialValueToOtherType(): void @@ -337,7 +337,7 @@ public function testShouldReduceListWithInitialValueToOtherType(): void $list->add(2); $list->add(3); - $this->assertEquals('123', $list->reduce(fn ($t, $v) => $t . $v, '')); + $this->assertEquals('123', $list->reduce(fn($t, $v) => $t . $v, '')); } public function testShouldClearCollection(): void @@ -362,11 +362,11 @@ public function testShouldMapAndFilterCollectionToNewListCollectionByArrowFuncti { $this->list = ListCollection::from([1, 2, 3]); - $this->list->map(fn ($v) => $v + 1); // 2, 3, 4 - $this->list->map(fn ($v) => $v * 2); // 4, 6, 8 - $this->list->filter(fn ($v, $i = null) => $v % 3 === 0); // 6 - $this->list->map(fn ($v) => $v - 1); // 5 - $this->list->map(fn ($v) => (string) $v); // '5' + $this->list->map(fn($v) => $v + 1); // 2, 3, 4 + $this->list->map(fn($v) => $v * 2); // 4, 6, 8 + $this->list->filter(fn($v, $i = null) => $v % 3 === 0); // 6 + $this->list->map(fn($v) => $v - 1); // 5 + $this->list->map(fn($v) => (string) $v); // '5' $this->list->add('6'); // '5', '6' @@ -533,7 +533,7 @@ public function testShouldKeepOnlyUniqueValuesByCallback(): void new SimpleEntity(5), new SimpleEntity(1), ]); - $list->uniqueBy(fn (SimpleEntity $e) => $e->getId()); + $list->uniqueBy(fn(SimpleEntity $e) => $e->getId()); $this->assertEquals( [ @@ -567,7 +567,7 @@ public function testShouldCountValuesByCallback(): void { $list = ListCollection::from([1, 3, 5, 7, 2, 3, 5, 4, 8, 2]); - $counts = $list->countBy(fn ($v) => $v % 2 === 0 ? 'even' : 'odd'); + $counts = $list->countBy(fn($v) => $v % 2 === 0 ? 'even' : 'odd'); $this->assertEquals( [ @@ -600,7 +600,7 @@ public function testShouldFindMinInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(1), $list->minBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(1), $list->minBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldFindMaxInListByCallback(): void @@ -611,14 +611,14 @@ public function testShouldFindMaxInListByCallback(): void new SimpleEntity(2), ]); - $this->assertEquals(new SimpleEntity(3), $list->maxBy(fn (SimpleEntity $e) => $e->getId())); + $this->assertEquals(new SimpleEntity(3), $list->maxBy(fn(SimpleEntity $e) => $e->getId())); } public function testShouldMapListAndUseIndexInMapping(): void { $list = ListCollection::from([1, 2, 3, 4, 5]); - $list->mapi(fn ($v, $i) => $i * $v); + $list->mapi(fn($v, $i) => $i * $v); $this->assertSame([0, 2, 6, 12, 20], $list->toArray()); } diff --git a/tests/Mutable/Generic/MapTest.php b/tests/Mutable/Generic/MapTest.php index 78b3608..69269b7 100644 --- a/tests/Mutable/Generic/MapTest.php +++ b/tests/Mutable/Generic/MapTest.php @@ -98,10 +98,10 @@ public function testShouldCreateMapByCallback(): void { $map = Map::create( explode(',', '1,2,3'), - fn ($value) => new SimpleEntity((int) $value) + fn($value) => new SimpleEntity((int) $value), ); - $map->map(fn (SimpleEntity $e) => $e->getId()); + $map->map(fn(SimpleEntity $e) => $e->getId()); $this->assertSame([1, 2, 3], $map->toArray()); } @@ -176,8 +176,8 @@ public function testShouldContainsValueBy(): void $this->map->set('key', $valueExists); - $this->assertTrue($this->map->containsBy(fn ($v) => $v === $valueExists)); - $this->assertFalse($this->map->containsBy(fn ($v) => $v === $valueDoesNotExist)); + $this->assertTrue($this->map->containsBy(fn($v) => $v === $valueExists)); + $this->assertFalse($this->map->containsBy(fn($v) => $v === $valueDoesNotExist)); } public function testShouldRemoveValueFromMap(): void @@ -198,7 +198,7 @@ public function testShouldMapToNewMapWithSameGenericType(): void $this->map->set('key2', 2); $this->map->set('key3', 3); - $this->map->map(fn ($v, $k) => $v + 1); + $this->map->map(fn($v, $k) => $v + 1); $this->assertEquals(['key' => 2, 'key2' => 3, 'key3' => 4], $this->map->toArray()); } @@ -209,9 +209,9 @@ public function testShouldMapToNewMap(): void $map->set('one', new SimpleEntity(1)); $map->set('two', new SimpleEntity(2)); - $map->map(fn ($v) => $v->getId()); + $map->map(fn($v) => $v->getId()); - $this->assertInstanceOf(\MF\Collection\Mutable\Generic\Map::class, $map); + $this->assertInstanceOf(Map::class, $map); $this->assertEquals(['one' => 1, 'two' => 2], $map->toArray()); } @@ -221,7 +221,7 @@ public function testShouldMapToNewGenericMap(): void $map->set('one', new SimpleEntity(1)); $map->set('two', new SimpleEntity(2)); - $map->map(fn ($v) => $v->getId()); + $map->map(fn($v) => $v->getId()); $this->assertInstanceOf(Map::class, $map); $this->assertEquals(['one' => 1, 'two' => 2], $map->toArray()); @@ -233,7 +233,7 @@ public function testShouldFilterItemsToNewMapByArrowFunction(): void $this->map->set('key2', 2); $this->map->set('key3', 3); - $this->map->filter(fn ($v) => $v > 1); + $this->map->filter(fn($v) => $v > 1); $this->assertEquals(['key2' => 2, 'key3' => 3], $this->map->toArray()); } @@ -243,8 +243,8 @@ public function testShouldCombineMapAndFilterToCreateNewMap(): void $this->map->set('key', 1); $this->map->set('key2', 2); - $this->map->filter(fn ($v) => $v > 1); - $this->map->map(fn ($v) => $v * 3); + $this->map->filter(fn($v) => $v > 1); + $this->map->map(fn($v) => $v * 3); $this->assertEquals(['key2' => 6], $this->map->toArray()); } @@ -275,7 +275,7 @@ public function testShouldReduceMap(): void $this->map->set('key2', 2); $this->map->set('key3', 3); - $this->assertEquals(6, $this->map->reduce(fn ($t, $c) => $t + $c)); + $this->assertEquals(6, $this->map->reduce(fn($t, $c) => $t + $c)); } public function testShouldGetMutableGenericMapAsImmutableGenericMap(): void @@ -297,7 +297,7 @@ public function testShouldReduceMapWithInitialValue(): void $map->set('two', 2); $map->set('three', 3); - $this->assertEquals(10 + 1 + 2 + 3, $map->reduce(fn ($t, $v) => $t + $v, 10)); + $this->assertEquals(10 + 1 + 2 + 3, $map->reduce(fn($t, $v) => $t + $v, 10)); } public function testShouldReduceListWithInitialValueToOtherType(): void @@ -307,7 +307,7 @@ public function testShouldReduceListWithInitialValueToOtherType(): void $map->set('two', 2); $map->set('three', 3); - $this->assertEquals('123', $map->reduce(fn ($t, $v) => $t . $v, '')); + $this->assertEquals('123', $map->reduce(fn($t, $v) => $t . $v, '')); } public function testShouldClearCollection(): void @@ -442,7 +442,7 @@ public function testShouldForeachItemInMap(): void { $map = Map::from([1 => 'one', 2 => 'two', 'three' => 3]); - $map->each(function (mixed $value, int|string $key = null): void { + $map->each(function (mixed $value, int|string|null $key = null): void { if ($key === 1) { $this->assertEquals('one', $value); } elseif ($key === 2) {