From ad60162521882a5e2177c30de49af53040c17e17 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 15 Aug 2019 21:31:43 +0200 Subject: [PATCH] Update code style using latest version of drupol/drupal-conventions. --- spec/drupol/collection/CollectionSpec.php | 126 +++++++++++++--------- src/Collection.php | 14 +-- src/Contract/Collection.php | 2 +- src/Operation/Combine.php | 2 +- src/Operation/Forget.php | 2 +- src/Operation/Only.php | 2 +- src/Operation/Skip.php | 2 +- src/Operation/Walk.php | 2 +- 8 files changed, 91 insertions(+), 61 deletions(-) diff --git a/spec/drupol/collection/CollectionSpec.php b/spec/drupol/collection/CollectionSpec.php index 3750bb60c..2a3576ec1 100644 --- a/spec/drupol/collection/CollectionSpec.php +++ b/spec/drupol/collection/CollectionSpec.php @@ -34,7 +34,7 @@ public function it_can_append_items(): void public function it_can_apply(): void { $this - ->beConstructedThrough('with', [range(1, 10)]); + ->beConstructedThrough('with', [\range(1, 10)]); $this ->apply(static function ($item) { @@ -48,7 +48,9 @@ public function it_can_apply(): void }) ->shouldReturn($this); - $callback = static function (): void {throw new \Exception('foo'); }; + $callback = static function (): void { + throw new \Exception('foo'); + }; $this ->shouldThrow(\Exception::class) @@ -98,10 +100,12 @@ public function it_can_apply(): void return $item; }; - $this::withArray(range(1, 20)) + $this::withArray(\range(1, 20)) ->apply($applyCallback1) ->walk($walkCallback2) - ->filter(static function ($item) {return false !== $item; }) + ->filter(static function ($item) { + return false !== $item; + }) ->all() ->shouldReturn($context); } @@ -109,7 +113,7 @@ public function it_can_apply(): void public function it_can_be_constructed_from_array(): void { $this - ->beConstructedThrough('withArray', [range('A', 'E')]); + ->beConstructedThrough('withArray', [\range('A', 'E')]); $this ->all() @@ -129,7 +133,9 @@ public function it_can_be_constructed_from_empty(): void public function it_can_be_constructed_with_a_closure(): void { $this - ->beConstructedThrough('with', [static function () {yield from range(1, 3); }]); + ->beConstructedThrough('with', [static function () { + yield from \range(1, 3); + }]); $this->shouldImplement(CollectionInterface::class); } @@ -181,11 +187,13 @@ public function it_can_be_returned_as_an_array(): void public function it_can_chunk(): void { $this - ->beConstructedThrough('with', [range('A', 'F')]); + ->beConstructedThrough('with', [\range('A', 'F')]); $this ->chunk(2) - ->map(static function (CollectionInterface $item) {return implode('', iterator_to_array($item)); }) + ->map(static function (CollectionInterface $item) { + return \implode('', \iterator_to_array($item)); + }) ->all() ->shouldReturn(['AB', 'CD', 'EF']); @@ -196,7 +204,9 @@ public function it_can_chunk(): void $this ->chunk(1) - ->map(static function (CollectionInterface $item) {return implode('', iterator_to_array($item)); }) + ->map(static function (CollectionInterface $item) { + return \implode('', \iterator_to_array($item)); + }) ->all() ->shouldReturn(['A', 'B', 'C', 'D', 'E', 'F']); } @@ -204,7 +214,7 @@ public function it_can_chunk(): void public function it_can_collapse(): void { $this - ->beConstructedThrough('with', [range('A', 'J')]); + ->beConstructedThrough('with', [\range('A', 'J')]); $this ->chunk(2) @@ -216,14 +226,14 @@ public function it_can_collapse(): void public function it_can_combine(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this - ->combine(range('e', 'a')) + ->combine(\range('e', 'a')) ->shouldIterateAs(['e' => 'A', 'd' => 'B', 'c' => 'C', 'b' => 'D', 'a' => 'E']); $this - ->combine(range(1, 100)) + ->combine(\range(1, 100)) ->shouldThrow(\Exception::class) ->during('all'); } @@ -240,7 +250,7 @@ public function it_can_convert_use_a_string_as_parameter(): void public function it_can_count_its_items(): void { $this - ->beConstructedThrough('with', [range('A', 'C')]); + ->beConstructedThrough('with', [\range('A', 'C')]); $this ->count() @@ -249,12 +259,14 @@ public function it_can_count_its_items(): void public function it_can_filter_its_element(): void { - $input = array_merge([0, false], range(1, 10)); + $input = \array_merge([0, false], \range(1, 10)); $this ->beConstructedThrough('with', [$input]); - $callable = static function ($item) {return $item % 2; }; + $callable = static function ($item) { + return $item % 2; + }; $this ->filter($callable) @@ -287,7 +299,7 @@ public function it_can_flatten(): void $input[] = $items; } - $input = array_pad([], 5, $input); + $input = \array_pad([], 5, $input); $this ->beConstructedThrough('with', [$input]); @@ -295,7 +307,7 @@ public function it_can_flatten(): void $output = []; for ($i = 0; 5 > $i; ++$i) { - $output = array_merge($output, range(0, 9)); + $output = \array_merge($output, \range(0, 9)); } $this @@ -323,7 +335,7 @@ public function it_can_flatten(): void public function it_can_flip(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this ->flip() @@ -334,7 +346,7 @@ public function it_can_flip(): void public function it_can_forget(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this ->forget(0, 4) @@ -346,7 +358,7 @@ public function it_can_forget(): void public function it_can_get(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this ->get(4) @@ -360,7 +372,7 @@ public function it_can_get(): void public function it_can_get_items_with_only_specific_keys(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this ->only(0, 1, 3) @@ -376,7 +388,7 @@ public function it_can_get_items_with_only_specific_keys(): void public function it_can_get_its_first_value(): void { $this - ->beConstructedThrough('with', [range(1, 10)]); + ->beConstructedThrough('with', [\range(1, 10)]); $this ->first() @@ -407,17 +419,17 @@ static function ($value) { public function it_can_keys(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); $this ->keys() - ->shouldIterateAs(range(0, 4)); + ->shouldIterateAs(\range(0, 4)); } public function it_can_limit(): void { $this - ->beConstructedThrough('with', [range('A', 'F')]); + ->beConstructedThrough('with', [\range('A', 'F')]); $this ->limit(3) @@ -430,13 +442,15 @@ public function it_can_limit(): void public function it_can_map(): void { - $input = array_combine(range('A', 'E'), range('A', 'E')); + $input = \array_combine(\range('A', 'E'), \range('A', 'E')); $this ->beConstructedThrough('with', [$input]); $this - ->map(static function (string $item) {return $item . $item; }) + ->map(static function (string $item) { + return $item . $item; + }) ->all() ->shouldReturn([0 => 'AA', 1 => 'BB', 2 => 'CC', 3 => 'DD', 4 => 'EE']); } @@ -444,9 +458,11 @@ public function it_can_map(): void public function it_can_merge(): void { $this - ->beConstructedThrough('with', [range('A', 'E')]); + ->beConstructedThrough('with', [\range('A', 'E')]); - $collection = Collection::with(static function () {yield from range('F', 'J'); }); + $collection = Collection::with(static function () { + yield from \range('F', 'J'); + }); $this ->merge($collection->all()) @@ -458,7 +474,7 @@ public function it_can_merge(): void public function it_can_nth(): void { $this - ->beConstructedThrough('with', [range(0, 70)]); + ->beConstructedThrough('with', [\range(0, 70)]); $this ->nth(7) @@ -471,7 +487,7 @@ public function it_can_nth(): void public function it_can_pad(): void { - $input = array_combine(range('A', 'E'), range('A', 'E')); + $input = \array_combine(\range('A', 'E'), \range('A', 'E')); $this ->beConstructedThrough('with', [$input]); @@ -485,7 +501,7 @@ public function it_can_pad(): void public function it_can_prepend(): void { $this - ->beConstructedThrough('with', [range('D', 'F')]); + ->beConstructedThrough('with', [\range('D', 'F')]); $this ->prepend('A', 'B', 'C') @@ -497,10 +513,12 @@ public function it_can_prepend(): void public function it_can_reduce(): void { $this - ->beConstructedThrough('with', [range(1, 100)]); + ->beConstructedThrough('with', [\range(1, 100)]); $this - ->reduce(static function ($carry, $item) {return $carry + $item; }, 0) + ->reduce(static function ($carry, $item) { + return $carry + $item; + }, 0) ->shouldReturn(5050); } @@ -518,7 +536,7 @@ public function it_can_run_an_operation(Operation $operation): void public function it_can_skip(): void { $this - ->beConstructedThrough('with', [range('A', 'F')]); + ->beConstructedThrough('with', [\range('A', 'F')]); $this ->skip(3) @@ -534,7 +552,7 @@ public function it_can_skip(): void public function it_can_slice(): void { $this - ->beConstructedThrough('with', [range(0, 10)]); + ->beConstructedThrough('with', [\range(0, 10)]); $this ->slice(5) @@ -582,7 +600,9 @@ public function it_can_use_range_with_value_1(): void public function it_can_use_times_with_a_callback(): void { $this - ->beConstructedThrough('times', [2, static function () {return range(1, 5); }]); + ->beConstructedThrough('times', [2, static function () { + return \range(1, 5); + }]); $a = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]; @@ -598,7 +618,7 @@ public function it_can_use_times_without_a_callback(): void $this ->all() - ->shouldReturn(range(1, 10)); + ->shouldReturn(\range(1, 10)); $this::times(-5) ->all() @@ -611,30 +631,40 @@ public function it_can_use_times_without_a_callback(): void public function it_can_walk(): void { - $input = array_combine(range('A', 'E'), range('A', 'E')); + $input = \array_combine(\range('A', 'E'), \range('A', 'E')); $this ->beConstructedThrough('with', [$input]); $this - ->walk(static function (string $item) {return $item . $item; }) + ->walk(static function (string $item) { + return $item . $item; + }) ->all() ->shouldReturn(['A' => 'AA', 'B' => 'BB', 'C' => 'CC', 'D' => 'DD', 'E' => 'EE']); - $this::withArray(range(1, 10)) - ->walk(static function ($item) {return $item * 2; }, static function ($item) {return $item + 1; }) + $this::withArray(\range(1, 10)) + ->walk(static function ($item) { + return $item * 2; + }, static function ($item) { + return $item + 1; + }) ->all() - ->shouldReturn(range(3, 21, 2)); + ->shouldReturn(\range(3, 21, 2)); - $this::withArray(range(1, 10)) - ->walk(static function ($item) {return $item; }, static function ($item) {return $item; }) - ->shouldIterateAs(range(1, 10)); + $this::withArray(\range(1, 10)) + ->walk(static function ($item) { + return $item; + }, static function ($item) { + return $item; + }) + ->shouldIterateAs(\range(1, 10)); } public function it_can_zip(): void { $this - ->beConstructedThrough('with', [range('A', 'C')]); + ->beConstructedThrough('with', [\range('A', 'C')]); $this ->zip('D', 'E', 'F') diff --git a/src/Collection.php b/src/Collection.php index 046656797..ea3604e60 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -46,7 +46,7 @@ final class Collection implements CollectionInterface */ public function all(): array { - return iterator_to_array($this->getIterator()); + return \iterator_to_array($this->getIterator()); } /** @@ -102,7 +102,7 @@ public function combine($keys): CollectionInterface */ public function count(): int { - return iterator_count($this->getIterator()); + return \iterator_count($this->getIterator()); } /** @@ -149,7 +149,7 @@ public function first(callable $callback = null, $default = null) /** * {@inheritdoc} */ - public function flatten(int $depth = PHP_INT_MAX): CollectionInterface + public function flatten(int $depth = \PHP_INT_MAX): CollectionInterface { return $this->run(Flatten::with($depth)); } @@ -221,7 +221,7 @@ public function map(callable ...$callbacks): CollectionInterface */ public function merge(...$sources): CollectionInterface { - return $this->run(Merge::with(...array_map([$this, 'makeIterator'], $sources))); + return $this->run(Merge::with(...\array_map([$this, 'makeIterator'], $sources))); } /** @@ -273,7 +273,7 @@ public function prepend(...$items): CollectionInterface * * @return \drupol\collection\Contract\Collection */ - public static function range(int $start = 0, $end = INF, $step = 1): CollectionInterface + public static function range(int $start = 0, $end = \INF, $step = 1): CollectionInterface { return self::withArray([])->run(Range::with($start, $end, $step)); } @@ -297,7 +297,7 @@ public function reduce(callable $callback, $initial = null) */ public function run(Operation ...$operations): CollectionInterface { - return array_reduce($operations, [$this, 'doRun'], $this); + return \array_reduce($operations, [$this, 'doRun'], $this); } /** @@ -431,7 +431,7 @@ private static function getArrayableItems($items): array } if ($items instanceof Traversable) { - return iterator_to_array($items); + return \iterator_to_array($items); } return (array) $items; diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index e7aed9f5f..f34c56f2c 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -88,7 +88,7 @@ public function first(callable $callback = null, $default = null); * * @return \drupol\collection\Contract\Collection */ - public function flatten(int $depth = PHP_INT_MAX): self; + public function flatten(int $depth = \PHP_INT_MAX): self; /** * Flip the items in the collection. diff --git a/src/Operation/Combine.php b/src/Operation/Combine.php index 6b2d3d98e..fa4558737 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -32,7 +32,7 @@ static function () use ($keys, $collection) { if (($original->valid() && !$keysIterator->valid()) || (!$original->valid() && $keysIterator->valid()) ) { - trigger_error('Both keys and values must have the same amount of items.', E_USER_WARNING); + \trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING); } } ); diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index fa9619bd1..cf4041dff 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -21,7 +21,7 @@ public function run(CollectionInterface $collection): CollectionInterface return Collection::withClosure( static function () use ($keys, $collection) { - $keys = array_flip($keys); + $keys = \array_flip($keys); foreach ($collection->getIterator() as $key => $value) { if (!\array_key_exists($key, $keys)) { diff --git a/src/Operation/Only.php b/src/Operation/Only.php index 073f7bbc8..a3caabc2c 100644 --- a/src/Operation/Only.php +++ b/src/Operation/Only.php @@ -24,7 +24,7 @@ static function () use ($keys, $collection) { if ([] === $keys) { yield from $collection; } else { - $keys = array_flip($keys); + $keys = \array_flip($keys); foreach ($collection as $key => $value) { if (\array_key_exists($key, $keys)) { diff --git a/src/Operation/Skip.php b/src/Operation/Skip.php index 23babb1a4..9a0cf8d60 100644 --- a/src/Operation/Skip.php +++ b/src/Operation/Skip.php @@ -22,7 +22,7 @@ public function run(CollectionInterface $collection): CollectionInterface return Collection::withClosure( static function () use ($counts, $collection) { $iterator = $collection->getIterator(); - $counts = array_sum($counts); + $counts = \array_sum($counts); foreach ($iterator as $key => $item) { if (0 < $counts--) { diff --git a/src/Operation/Walk.php b/src/Operation/Walk.php index 4fbb9e509..27c3a01d8 100644 --- a/src/Operation/Walk.php +++ b/src/Operation/Walk.php @@ -26,7 +26,7 @@ static function () use ($callbacks, $collection) { }; foreach ($collection->getIterator() as $key => $value) { - yield $key => array_reduce($callbacks, $callback, $value); + yield $key => \array_reduce($callbacks, $callback, $value); } } );