Skip to content

Commit

Permalink
Split StringsArray and ArrayValue (#41)
Browse files Browse the repository at this point in the history
* Split StringsArray and ArrayValue

* Split StringsArray and ArrayValue

* fix

* fix

* fix
  • Loading branch information
bronek89 authored Dec 9, 2021
1 parent fc467b1 commit 3f91740
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 118 deletions.
2 changes: 1 addition & 1 deletion spec/PlainArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ function it_implements_ArrayAccess()
$this->shouldImplement(\ArrayAccess::class);

Assert::assertTrue($this->offsetExists(0));
Assert::assertFalse($this->offsetExists(66));
Assert::assertFalse($this->offsetExists(33));
$this->offsetGet(0)->shouldReturn('item 1');
$this[0]->shouldBe('item 1');
}
Expand Down
10 changes: 1 addition & 9 deletions spec/PlainStringsArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ function it_joins_with_other_StringsArray()
$join->toNativeStrings()->shouldBeLike(['string 1', 'string 2', 'string 3', 'string 4']);
}

function it_joins_with_regular_ArrayValue_with_strings()
{
$this->beConstructedWithStrings('string 1', 'string 2');

$this->join(Wrap::array(['string 3', 'string 4']))
->toNativeStrings()->shouldBeLike(['string 1', 'string 2', 'string 3', 'string 4']);
}

function it_can_be_sliced()
{
$strings = ['string 1', 'string 2', 'string 3', 'string 4'];
Expand All @@ -73,7 +65,7 @@ function it_can_be_spliced()

$this->splice(1, 2)->toNativeStrings()->shouldBeLike(['string 1', 'string 4']);

$this->splice(1, 2, Wrap::array(['string x', 'string y']))
$this->splice(1, 2, Wrap::stringsArray(['string x', 'string y']))
->toNativeStrings()->shouldBeLike(['string 1', 'string x', 'string y', 'string 4']);

$this->splice(0, 3)->toNativeStrings()->shouldBeLike(['string 4']);
Expand Down
7 changes: 4 additions & 3 deletions src/ArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @template TValue
* @extends Collection<TValue>
* @extends Stack<TValue>
* @extends IteratorAggregate<int, TValue>
* @extends ArrayAccess<int, TValue>
*/
Expand All @@ -21,7 +22,7 @@ interface ArrayValue extends Value, Collection, Stack, IteratorAggregate, ArrayA
public function each(callable $callback): ArrayValue;

/**
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function unique(?callable $comparator = null): ArrayValue;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function groupBy(callable $reducer): AssocValue;
public function chunk(int $size): ArrayValue;

/**
* @param callable(TValue $valueA, TValue $valueB): int $comparator
* @param callable(TValue,TValue):int $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function sort(callable $comparator): ArrayValue;
Expand Down Expand Up @@ -105,7 +106,7 @@ public function shift(&$value = null): ArrayValue;
public function push($value): ArrayValue;

/**
* @phpstan-param TValue $value
* @phpstan-param TValue|null $value
* @phpstan-return ArrayValue<TValue>
*/
public function pop(&$value = null): ArrayValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Arrayable/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Cache implements Arrayable
/** @var Arrayable<TValue>|null */
private ?Arrayable $arrayable;
/** @var TValue[] */
private array $array;
private array $array = [];

/** @param Arrayable<TValue> $arrayable */
public function __construct(Arrayable $arrayable)
Expand Down
12 changes: 9 additions & 3 deletions src/AssocArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function filter(callable $filter): AssocArray
}

/**
* @param callable(TValue $valueA, TValue $valueB):int $comparator
* @param callable(TValue,TValue):int $comparator
* @phpstan-return AssocArray<TKey, TValue>
*/
public function sort(callable $comparator): AssocArray
Expand Down Expand Up @@ -137,7 +137,7 @@ public function each(callable $callback): AssocArray
}

/**
* @phpstan-param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return AssocArray<TKey, TValue>
*/
public function unique(?callable $comparator = null): AssocArray
Expand Down Expand Up @@ -280,18 +280,24 @@ public function hasElement($element): bool
return in_array($element, $this->items->toAssocArray(), true);
}

/**
* @param callable(TValue $value):bool $filter
*/
public function any(callable $filter): bool
{
return $this->values()->any($filter);
}

/**
* @param callable(TValue $value):bool $filter
*/
public function every(callable $filter): bool
{
return $this->values()->every($filter);
}

/**
* @return TValue[]
* @return array<int, TValue>
*/
public function toArray(): array
{
Expand Down
8 changes: 4 additions & 4 deletions src/AssocValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
interface AssocValue extends Value, Collection, IteratorAggregate, ArrayAccess, Associable
{
/**
* @phpstan-param callable(TValue $value, TKey $key=):void $callback
* @phpstan-param callable(TValue, TKey $key=):void $callback
* @phpstan-return AssocValue<TKey, TValue>
*/
public function each(callable $callback): AssocValue;

/**
* @phpstan-param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return AssocValue<TKey, TValue>
*/
public function unique(?callable $comparator = null): AssocValue;

/**
* @phpstan-param callable(TValue $value):bool $filter
* @phpstan-param callable(TValue):bool $filter
* @phpstan-return AssocValue<TKey, TValue>
*/
public function filter(callable $filter): AssocValue;
Expand All @@ -47,7 +47,7 @@ public function filterEmpty(): AssocValue;
public function map(callable $transformer): AssocValue;

/**
* @param callable(TValue $valueA, TValue $valueB):int $comparator
* @param callable(TValue,TValue):int $comparator
* @phpstan-return AssocValue<TKey, TValue>
*/
public function sort(callable $comparator): AssocValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Associable/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Cache implements Associable
/** @var Associable<TKey,TValue>|null */
private ?Associable $arrayable;
/** @var array<TKey,TValue> */
private array $array;
private array $array = [];

/** @param Associable<TKey,TValue> $arrayable */
public function __construct(Associable $arrayable)
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function every(callable $filter): bool;
public function each(callable $callback): Collection;

/**
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return Collection<TValue>
*/
public function unique(?callable $comparator = null): Collection;
Expand Down Expand Up @@ -84,7 +84,7 @@ public function filterEmpty(): Collection;
public function map(callable $transformer): Collection;

/**
* @param callable(TValue $valueA, TValue $valueB):int $comparator
* @param callable(TValue, TValue $valueB):int $comparator
* @phpstan-return Collection<TValue>
*/
public function sort(callable $comparator): Collection;
Expand Down
4 changes: 2 additions & 2 deletions src/InfiniteIterableValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function each(callable $callback): InfiniteIterableValue
}

/**
* @param callable(TValue $valueA, TValue $valueB):int | null $comparator
* @param (callable(TValue $lueA,TValue):int) | null $comparator
* @phpstan-return InfiniteIterableValue<TKey, TValue>
*/
public function unique(?callable $comparator = null): InfiniteIterableValue
Expand Down Expand Up @@ -248,7 +248,7 @@ static function (iterable $iterable) use ($offset, $length): iterable {

/**
* @phpstan-param ArrayValue<TValue> $other
* @param callable(TValue $valueA, TValue $valueB):int | null $comparator
* @param (callable(TValue,TValue):int) | null $comparator
* @phpstan-return InfiniteIterableValue<TKey, TValue>
*/
public function diff(ArrayValue $other, ?callable $comparator = null): InfiniteIterableValue
Expand Down
6 changes: 3 additions & 3 deletions src/IterableValueStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class IterableValueStack
/** @phpstan-var IterableValueIterator<TKey, TValue> */
private IterableValueIterator $iterable;

/** @phpstan-var array<int, (callable(iterable<TKey, TValue> $value): iterable<mixed, mixed>)> */
/** @phpstan-var array<int, (callable(iterable<TKey,TValue>):iterable<mixed,mixed>)> */
private array $modifiers = [];

/**
Expand All @@ -26,7 +26,7 @@ public function __construct(IterableValueIterator $iterable)
/**
* @template TNewKey
* @template TNewValue
* @param callable(iterable<TKey, TValue> $value): iterable<TNewKey, TNewValue> $modifier
* @param callable(iterable<TKey,TValue>):iterable<TNewKey,TNewValue> $modifier
* @phpstan-return IterableValueStack<TNewKey, TNewValue>
*/
public function push(callable $modifier): self
Expand All @@ -38,7 +38,7 @@ public function push(callable $modifier): self
}

/**
* @phpstan-param array<int, (callable(iterable<TKey, TValue> $value): iterable<TKey, TValue>)> $modifiers
* @phpstan-param array<int, (callable(iterable<TKey,TValue>):iterable<TKey,TValue>)> $modifiers
* @phpstan-param iterable<TKey, TValue> $iterable
* @phpstan-return iterable<TKey, TValue>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Mappers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class Mappers
/**
* @deprecated use method() instead
* @param array<int, mixed> $args
* @return callable(object $item): mixed
* @return callable(object):mixed
*/
public static function callMethod(string $method, ...$args): callable
{
Expand All @@ -16,15 +16,15 @@ public static function callMethod(string $method, ...$args): callable

/**
* @param array<int, mixed> $args
* @return callable(object $item): mixed
* @return callable(object):mixed
*/
public static function method(string $method, ...$args): callable
{
return /** @phpstan-return mixed */static fn(object $item) => $item->$method(...$args);
}

/**
* @return callable(object $item): mixed
* @return callable(object):mixed
*/
public static function property(string $propertyName): callable
{
Expand Down
14 changes: 7 additions & 7 deletions src/PlainArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function splice(int $offset, int $length, ?ArrayValue $replacement = null
}

/**
* @param callable(TValue $valueA, TValue $valueB):int | null $comparator
* @param (callable(TValue,TValue):int) | null $comparator
* @phpstan-return PlainArray<TValue>
*/
public function unique(?callable $comparator = null): PlainArray
Expand All @@ -183,8 +183,8 @@ public function unique(?callable $comparator = null): PlainArray
}

/**
* @phpstan-param PlainArray<TValue> $other
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return PlainArray<TValue>
*/
public function diff(ArrayValue $other, ?callable $comparator = null): PlainArray
Expand All @@ -197,8 +197,8 @@ public function diff(ArrayValue $other, ?callable $comparator = null): PlainArra
}

/**
* @phpstan-param PlainArray<TValue> $other
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return PlainArray<TValue>
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): PlainArray
Expand Down Expand Up @@ -257,7 +257,7 @@ public function push($value): PlainArray
}

/**
* @phpstan-param TValue $value
* @phpstan-param TValue|null $value
* @phpstan-return PlainArray<TValue>
*/
public function pop(&$value = null): PlainArray
Expand Down Expand Up @@ -368,7 +368,7 @@ public function count(): int
}

/**
* @phpstan-return TValue[]
* @phpstan-return array<int, TValue>
*/
public function toArray(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/PlainString.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function endsWith($pattern): bool

/**
* @param string|StringValue $pattern
* @return ArrayValue<string[][]>
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue
{
Expand Down
Loading

0 comments on commit 3f91740

Please sign in to comment.