Skip to content

Commit

Permalink
Split StringsArray and ArrayValue
Browse files Browse the repository at this point in the history
  • Loading branch information
bronek89 committed Dec 8, 2021
1 parent 2ad4775 commit ce016a7
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 81 deletions.
3 changes: 2 additions & 1 deletion spec/AssocArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GW\Value\Wrap;
use GW\Value\AssocArray;
use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\Assert;

final class AssocArraySpec extends ObjectBehavior
{
Expand Down Expand Up @@ -408,8 +409,8 @@ function it_implements_ArrayAccess()

$this->shouldImplement(\ArrayAccess::class);

$this->offsetExists('a')->shouldReturn(true);
$this->offsetGet('b')->shouldReturn('item 2');
Assert::assertTrue($this->offsetExists('a'));
$this['a']->shouldBe('item 1');
}

Expand Down
4 changes: 3 additions & 1 deletion spec/PlainArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GW\Value\Wrap;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\Assert;

final class PlainArraySpec extends ObjectBehavior
{
Expand Down Expand Up @@ -652,7 +653,8 @@ function it_implements_ArrayAccess()

$this->shouldImplement(\ArrayAccess::class);

$this->offsetExists(0)->shouldReturn(true);
Assert::assertTrue($this->offsetExists(0));
Assert::assertFalse($this->offsetExists(33));
$this->offsetGet(0)->shouldReturn('item 1');
$this[0]->shouldBe('item 1');
}
Expand Down
15 changes: 4 additions & 11 deletions spec/PlainStringsArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\ObjectBehavior;
use PhpSpec\Wrapper\Collaborator;
use PHPUnit\Framework\Assert;
use Prophecy\Argument;

final class PlainStringsArraySpec extends ObjectBehavior
Expand Down Expand Up @@ -45,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 @@ -72,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 Expand Up @@ -309,8 +302,8 @@ function it_is_like_array()
$this[0]->shouldBeLike(Wrap::string('first'));
$this->offsetGet(0)->shouldBeLike(Wrap::string('first'));

$this->offsetExists(0)->shouldReturn(true);
$this->offsetExists(2)->shouldReturn(false);
Assert::assertTrue($this->offsetExists(0));
Assert::assertFalse($this->offsetExists(2));
}

function it_is_like_array_but_immutable()
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
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/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
47 changes: 18 additions & 29 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
*/
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
*/
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 Down Expand Up @@ -96,25 +88,22 @@ public function flatMap(callable $transformer): PlainStringsArray
}

/**
* @param callable(StringValue $value):string $reducer
* @phpstan-return AssocValue<string, ArrayValue<StringValue>>
* @phpstan-ignore-next-line shrug
* @template TNewKey
* @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)
// @phpstan-ignore-next-line shrug
->map(
/** @return ArrayValue<StringValue> */
static fn(ArrayValue $value): ArrayValue => $value->toStringsArray()
/** @param ArrayValue<StringValue> $value */
static fn(ArrayValue $value): StringsArray => $value->toStringsArray()
);
}

/**
* @return ArrayValue<array<int, StringValue>>
* @phpstan-ignore-next-line shrug
*/
public function chunk(int $size): ArrayValue
{
Expand Down Expand Up @@ -266,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 @@ -499,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
15 changes: 9 additions & 6 deletions src/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

namespace GW\Value;

/**
* @template TValue
*/
interface Stack
{
/**
* @param mixed $value
* @return Stack
* @param TValue $value
* @return Stack<TValue>
*/
public function unshift($value): Stack;

/**
* @return Stack
* @return Stack<TValue>
*/
public function shift(): Stack;

/**
* @param mixed $value
* @return Stack
* @param TValue $value
* @return Stack<TValue>
*/
public function push($value): Stack;

/**
* @return Stack
* @return Stack<TValue>
*/
public function pop(): Stack;
}
2 changes: 1 addition & 1 deletion src/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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
Loading

0 comments on commit ce016a7

Please sign in to comment.