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

Split StringsArray and ArrayValue #41

Merged
merged 5 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
1 change: 1 addition & 0 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 Down
8 changes: 7 additions & 1 deletion src/AssocArray.php
Original file line number Diff line number Diff line change
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
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
bronek89 marked this conversation as resolved.
Show resolved Hide resolved
* @phpstan-param iterable<TKey, TValue> $iterable
* @phpstan-return iterable<TKey, TValue>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/PlainArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function unique(?callable $comparator = null): PlainArray
}

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

/**
* @phpstan-param PlainArray<TValue> $other
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-return PlainArray<TValue>
*/
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
64 changes: 28 additions & 36 deletions src/PlainStringsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use GW\Value\Stringable\ToStringValue;
use Traversable;
use InvalidArgumentException;
use function in_array;
use function is_scalar;

final class PlainStringsArray implements StringsArray
{
Expand All @@ -29,43 +27,37 @@ public static function fromArray(array $strings): self
return new self(Wrap::array($strings));
}

/**
* @param StringsArray $other
*/
public function join(ArrayValue $other): PlainStringsArray
public function join(StringsArray $other): PlainStringsArray
{
return new self($this->strings->join($this->mapStringValues($other)));
return new self($this->strings->join($other->toArrayValue()));
}

public function slice(int $offset, int $length): PlainStringsArray
{
return new self($this->strings->slice($offset, $length));
}

/**
* @param StringsArray $replacement
*/
public function splice(int $offset, int $length, ?ArrayValue $replacement = null): PlainStringsArray
public function splice(int $offset, int $length, ?StringsArray $replacement = null): PlainStringsArray
{
return new self($this->strings->splice($offset, $length, $replacement));
return new self(
$this->strings->splice($offset, $length, $replacement === null ? null : $replacement->toArrayValue())
);
}

/**
* @param StringsArray $other
* @param (callable(StringValue $valueA, StringValue $valueB):int)|null $comparator
* @param (callable(StringValue,StringValue):int)|null $comparator
*/
public function diff(ArrayValue $other, ?callable $comparator = null): PlainStringsArray
public function diff(StringsArray $other, ?callable $comparator = null): PlainStringsArray
{
return new self($this->strings->diff($this->mapStringValues($other), $comparator));
return new self($this->strings->diff($other->toArrayValue(), $comparator));
}

/**
* @param StringsArray $other
* @param callable(StringValue $valueA, StringValue $valueB):int|null $comparator
* @param (callable(StringValue,StringValue):int)|null $comparator
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): PlainStringsArray
public function intersect(StringsArray $other, ?callable $comparator = null): PlainStringsArray
{
return new self($this->strings->intersect($this->mapStringValues($other), $comparator));
return new self($this->strings->intersect($other->toArrayValue(), $comparator));
}

/**
Expand All @@ -80,33 +72,33 @@ public function reduce(callable $transformer, $start)
}

/**
* @param callable(StringValue $value):StringValue $transformer
* @param callable(StringValue):StringValue $transformer
*/
public function map(callable $transformer): PlainStringsArray
{
return new self($this->strings->map($transformer));
}

/**
* @param callable(StringValue $value):iterable<StringValue> $transformer
* @param callable(StringValue):iterable<StringValue> $transformer
*/
public function flatMap(callable $transformer): PlainStringsArray
{
return new self($this->strings->flatMap($transformer));
}

/**
* @param callable(StringValue $value):(string|int) $reducer
* @phpstan-return AssocValue<int|string, ArrayValue<StringValue>>
* @template TNewKey of int|string
* @param callable(StringValue $value):TNewKey $reducer
* @phpstan-return AssocValue<TNewKey, StringsArray>
*/
public function groupBy(callable $reducer): AssocValue
{
// @phpstan-ignore-next-line shrug
return $this->strings
->groupBy($reducer)
->map(
/** @return ArrayValue<StringValue> */
static fn(ArrayValue $value): ArrayValue => $value->toStringsArray()
/** @param ArrayValue<StringValue> $value */
static fn(ArrayValue $value): StringsArray => $value->toStringsArray()
);
}

Expand All @@ -119,7 +111,7 @@ public function chunk(int $size): ArrayValue
}

/**
* @param callable(StringValue $value): bool $filter
* @param callable(StringValue):bool $filter
*/
public function filter(callable $filter): PlainStringsArray
{
Expand All @@ -142,15 +134,15 @@ public function last(): ?StringValue
}

/**
* @param callable(StringValue $value): bool $filter
* @param callable(StringValue):bool $filter
*/
public function find(callable $filter): ?StringValue
{
return $this->strings->find($filter);
}

/**
* @param callable(StringValue $value): bool $filter
* @param callable(StringValue):bool $filter
*/
public function findLast(callable $filter): ?StringValue
{
Expand All @@ -166,23 +158,23 @@ public function hasElement($element): bool
}

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

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

/**
* @param callable(StringValue $value): void $callback
* @param callable(StringValue):void $callback
*/
public function each(callable $callback): PlainStringsArray
{
Expand Down Expand Up @@ -263,11 +255,11 @@ public function offsetGet($offset): StringValue

/**
* @param int $offset
* @param StringValue $value
* @param StringValue|string $value
*/
public function offsetSet($offset, $value): void
{
$this->strings->offsetSet($offset, $value);
$this->strings->offsetSet($offset, Wrap::string($value));
}

/**
Expand Down Expand Up @@ -496,7 +488,7 @@ public function positionLast($needle): ?int

/**
* @param string|StringValue $pattern
* @return ArrayValue<string[][]>
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue
{
Expand Down
19 changes: 14 additions & 5 deletions src/Safe.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ public static function filter(callable $callable): callable
return fn(...$args): bool => self::guard($callable(...$args), 'is_bool', 'Filter must return boolean value.');
}

/**
* @param callable(mixed ...$args):iterable<mixed> $callable
*/
public static function iterableTransformer(callable $callable): callable
{
return fn(...$args): iterable => self::guard(
$callable(...$args),
'is_iterable',
'Iterable transformer must return iterable value.'
);
return
/**
* @param mixed ...$args
* @return iterable<mixed>
*/
static fn(...$args): iterable => self::guard(
$callable(...$args),
'is_iterable',
'Iterable transformer must return iterable value.'
);
}

/**
* @template TValue
* @param TValue $value
* @param callable(TValue):bool $assertion
* @return TValue
*/
private static function guard($value, callable $assertion, string $message)
Expand Down
Loading