From b7cdda529f27fcd5b290a7b13fa08b2ac37b0d38 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 23 May 2024 23:05:24 +0200 Subject: [PATCH 1/4] Upgrading packages --- .github/workflows/dependabot-auto-merge.yml | 8 +- .github/workflows/pint.yml | 6 +- .github/workflows/run-tests-coverage.yml | 4 +- .github/workflows/run-tests.yml | 8 +- .gitignore | 2 +- composer.json | 6 +- examples/cheatsheet.php | 2 +- examples/sets.php | 4 +- phpunit.xml.dist | 58 +++---- rector.php | 2 +- src/Arr.php | 10 +- src/Classes/Operation.php | 4 +- src/Table.php | 49 +++--- tests/Arr/ArrFromTest.php | 8 +- tests/ArrTest.php | 180 ++++++++++---------- tests/ArrayAccessTest.php | 8 +- tests/CalcTest.php | 4 +- tests/IterateTest.php | 4 +- tests/TableOperationTest.php | 4 +- tests/TableTest.php | 50 +++--- 20 files changed, 199 insertions(+), 222 deletions(-) diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index ca2197d..2b9e9a5 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -10,20 +10,20 @@ jobs: runs-on: ubuntu-latest if: ${{ github.actor == 'dependabot[bot]' }} steps: - + - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v1.6.0 + uses: dependabot/fetch-metadata@v2.1.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" - + - name: Auto-merge Dependabot PRs for semver-minor updates if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} run: gh pr merge --auto --merge "$PR_URL" env: PR_URL: ${{github.event.pull_request.html_url}} GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - + - name: Auto-merge Dependabot PRs for semver-patch updates if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} run: gh pr merge --auto --merge "$PR_URL" diff --git a/.github/workflows/pint.yml b/.github/workflows/pint.yml index d460ad0..58e98ef 100644 --- a/.github/workflows/pint.yml +++ b/.github/workflows/pint.yml @@ -8,14 +8,14 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} - name: "Run Laravel Pint" - uses: aglipanci/laravel-pint-action@2.3.0 + uses: aglipanci/laravel-pint-action@2.4.0 - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: Fix styling diff --git a/.github/workflows/run-tests-coverage.yml b/.github/workflows/run-tests-coverage.yml index 05795b1..9141917 100644 --- a/.github/workflows/run-tests-coverage.yml +++ b/.github/workflows/run-tests-coverage.yml @@ -13,13 +13,13 @@ jobs: strategy: matrix: operating-system: [ubuntu-latest] - php-versions: [ '8.2' ] + php-versions: [ '8.3' ] dependency-stability: [ 'prefer-none' ] name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install PHP versions uses: shivammathur/setup-php@v2 with: diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 5fe499c..95cb062 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,18 +9,18 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest] - php: [8.0, 8.1, 8.2] + php: [8.1, 8.2, 8.3] exclude: - - os: windows-latest - php: 8.0 - os: windows-latest php: 8.1 + - os: windows-latest + php: 8.2 name: P${{ matrix.php }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.gitignore b/.gitignore index a816169..3b3fcd5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ phpunit.xml psalm.xml vendor .php-cs-fixer.cache - +/.phpunit.cache .DS_Store diff --git a/composer.json b/composer.json index 3380add..a8f9110 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ } ], "require": { - "php": "^8.0" + "php": "^8.1|^8.2|^8.3" }, "require-dev": { "laravel/pint": "^1.2", - "pestphp/pest": "^1.22", + "pestphp/pest": "^2", "phpstan/phpstan": "^1.6", - "rector/rector": "^0.15.0" + "rector/rector": "^1" }, "autoload": { "psr-4": { diff --git a/examples/cheatsheet.php b/examples/cheatsheet.php index 5eb47f5..56c3015 100644 --- a/examples/cheatsheet.php +++ b/examples/cheatsheet.php @@ -226,7 +226,7 @@ print_result($arr->find(fn ($element, $index): bool => $element > 1 && $index > 1)); // 3 $arr = Arr::make(['foo', 'bar', 'baz']); -print_result($arr->find(fn ($element): bool => str_contains($element, 'a'))); +print_result($arr->find(fn ($element): bool => str_contains((string) $element, 'a'))); // 'bar' // Returns the shadow copied part of the array to another location and keeps its length diff --git a/examples/sets.php b/examples/sets.php index a4e21c6..7e17c81 100644 --- a/examples/sets.php +++ b/examples/sets.php @@ -1,6 +1,6 @@ filter(fn ($x) => $arrB->includes($x)); +$intersection = $arrA->filter(fn ($x): bool => $arrB->includes($x)); echo $intersection->toString() . PHP_EOL; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 41f6a60..d67e5d2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,39 +1,23 @@ - - - - tests - - - - - ./src - - - - - - - - - - + + + + tests + + + + + + + + + + + + + + + ./src + + diff --git a/rector.php b/rector.php index a9d1fcc..756d2b9 100644 --- a/rector.php +++ b/rector.php @@ -19,7 +19,7 @@ // define sets of rules $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_80, + LevelSetList::UP_TO_PHP_81, SetList::DEAD_CODE, SetList::CODE_QUALITY, SetList::EARLY_RETURN, diff --git a/src/Arr.php b/src/Arr.php index 88fd74b..6477814 100755 --- a/src/Arr.php +++ b/src/Arr.php @@ -61,7 +61,7 @@ public static function from(iterable|string $arrayLike, ?Closure $mapFn = null): $arrayLike = iterator_to_array($arrayLike); } - $array = self::make((array) $arrayLike); + $array = self::make($arrayLike); if (! is_callable($mapFn)) { return $array; @@ -268,7 +268,6 @@ public function pop(): mixed /** * Add element to start of Arr and return new length * - * @param mixed $element the elements to add to the front of the array * @return int the new length of the array upon which the method was called. */ public function unshift(mixed ...$element): int @@ -371,7 +370,6 @@ public function slice(int $start, int $end = null): Arr /** * Returns index of first occurrence of element in arr * - * @param mixed $searchElement the element to search * @return string|int|bool the index of element found. If it is not found, false is returned */ public function indexOf(mixed $searchElement): string|int|bool @@ -382,7 +380,6 @@ public function indexOf(mixed $searchElement): string|int|bool /** * Returns index of last occurrence of element in Arr * - * @param mixed $searchElement the element to search * @return string|int|bool the index of element found. If it is not found, false is returned */ public function lastIndexOf(mixed $searchElement): string|int|bool @@ -481,7 +478,7 @@ public function flat(): self { $array = array_reduce( $this->arr, - fn ($result, $element) => array_merge($result, is_array($element) ? [...$element] : [$element]), + fn ($result, $element): array => array_merge($result, is_array($element) ? [...$element] : [$element]), [] ); @@ -518,7 +515,6 @@ public function flatMap(callable $callback): self /** * Changes all elements in range from $start for $count, to the specified value * - * @param mixed $value the value to fill the array with * @param int $start start index (from 0) * @param int|null $end end index (default, the end of array) */ @@ -587,7 +583,6 @@ public function sort(): Arr * * @param int $start the index at which to start changing the array * @param int|null $deleteCount an integer indicating the number of elements in the array to remove from $start - * @param mixed $newElements The elements to add to the array, beginning from $start * @return Arr an array containing the deleted elements */ public function splice(int $start, int $deleteCount = null, mixed $newElements = []): Arr @@ -606,7 +601,6 @@ public function toString(): string /** * Returns true if the input is an array * - * @param mixed $input the input value, to test if it is an array * @return bool true if $input is an array, false otherwise */ public static function isArray(mixed $input): bool diff --git a/src/Classes/Operation.php b/src/Classes/Operation.php index 294f86f..216c4fe 100644 --- a/src/Classes/Operation.php +++ b/src/Classes/Operation.php @@ -8,11 +8,11 @@ class Operation { public static function add(int|string $field, int|float $value): Closure { - return fn ($element) => $value + $element[$field]; + return fn ($element): int|float => $value + $element[$field]; } public static function double(int|string $field): Closure { - return fn ($element) => $element[$field] * 2; + return fn ($element): int|float => $element[$field] * 2; } } diff --git a/src/Table.php b/src/Table.php index 41bf9a1..c07eb1d 100644 --- a/src/Table.php +++ b/src/Table.php @@ -44,7 +44,7 @@ public function rows(): array public function toArray(): array { $result = []; - foreach ($this->rows() as $key => $row) { + foreach ($this->rows as $key => $row) { $result[$key] = $row->arr(); } return $result; @@ -89,9 +89,7 @@ public function select(int|string ...$columns): self $newRow = []; foreach ($columns as $column) { $value = $row->get($column); - if ($column !== null) { - $newRow[$column] = $value; - } + $newRow[$column] = $value; } $table->append(Arr::make($newRow)); } @@ -129,14 +127,14 @@ public function where(string|int $field, mixed $operator = null, mixed $value = } $function = match ($operator) { - '==' => fn ($element) => $element->get($field) == $value, - '>' => fn ($element) => $element->get($field) > $value, - '<' => fn ($element) => $element->get($field) < $value, - '>=' => fn ($element) => $element->get($field) >= $value, - '<=' => fn ($element) => $element->get($field) <= $value, - '!=' => fn ($element) => $element->get($field) != $value, - '!==' => fn ($element) => $element->get($field) !== $value, - default => fn ($element) => $element->get($field) === $value + '==' => fn ($element): bool => $element->get($field) == $value, + '>' => fn ($element): bool => $element->get($field) > $value, + '<' => fn ($element): bool => $element->get($field) < $value, + '>=' => fn ($element): bool => $element->get($field) >= $value, + '<=' => fn ($element): bool => $element->get($field) <= $value, + '!=' => fn ($element): bool => $element->get($field) != $value, + '!==' => fn ($element): bool => $element->get($field) !== $value, + default => fn ($element): bool => $element->get($field) === $value }; $filteredArray = array_filter($this->rows, $function); @@ -145,12 +143,12 @@ public function where(string|int $field, mixed $operator = null, mixed $value = public function orderBy(string|int $field, string $order = 'desc'): self { - $array = $this->rows(); + $array = $this->rows; if ($order !== 'asc') { - $closure = static fn (Arr $item1, Arr $item2) => $item2->get($field) <=> $item1->get($field); + $closure = static fn (Arr $item1, Arr $item2): int => $item2->get($field) <=> $item1->get($field); } else { - $closure = static fn ($item1, $item2) => $item1->get($field) <=> $item2->get($field); + $closure = static fn ($item1, $item2): int => $item1->get($field) <=> $item2->get($field); } usort($array, $closure); @@ -179,9 +177,10 @@ public function groupBy(string|int $field): Table foreach ($this->rows as $value) { $property = $value->get($field); $property = $this->castVariableForStrval($property); - - if (!$property - || array_key_exists(strval($property), $result)) { + if (!$property) { + continue; + } + if (array_key_exists(strval($property), $result)) { continue; } $result[$property] = $value; @@ -197,10 +196,10 @@ public function groupBy(string|int $field): Table private function castVariableForStrval(mixed $property): bool|float|int|string|null { return match(gettype($property)) { - 'boolean' => (bool) $property, - 'double' => (float) $property, - 'integer' => (int) $property, - 'string' => (string) $property, + 'boolean' => $property, + 'double' => $property, + 'integer' => $property, + 'string' => $property, default => null, }; } @@ -212,7 +211,7 @@ public function transform( string|int $field, callable $function, ): self { - $array = $this->rows(); + $array = $this->rows; foreach ($array as $row) { /** @var array $keys */ @@ -228,7 +227,7 @@ public function transform( public function count(): int { - return count($this->rows()); + return count($this->rows); } /** @@ -237,7 +236,7 @@ public function count(): int private function getArr(array|Arr $value): Arr { if (!$value instanceof Arr) { - $value = Arr::make($value); + return Arr::make($value); } return $value; } diff --git a/tests/Arr/ArrFromTest.php b/tests/Arr/ArrFromTest.php index 32a389d..03edf37 100644 --- a/tests/Arr/ArrFromTest.php +++ b/tests/Arr/ArrFromTest.php @@ -2,13 +2,13 @@ use HiFolks\DataType\Arr; -it('converts a string to an array', function () { +it('converts a string to an array', function (): void { expect(Arr::from('foo')->arr()) ->toBeArray() ->toBe(['f','o','o']); }); -it('converts a generator to an array', function () { +it('converts a generator to an array', function (): void { function generator(): Generator { yield 1; @@ -21,8 +21,8 @@ function generator(): Generator ->toBe([1, 8, 7]); }); -it('takes a function to map over the given array', function () { - expect(Arr::from([1, 2, 3], fn ($x) => $x + $x)->arr()) +it('takes a function to map over the given array', function (): void { + expect(Arr::from([1, 2, 3], fn ($x): float|int|array => $x + $x)->arr()) ->toBeArray() ->toBe([2, 4, 6]); }); diff --git a/tests/ArrTest.php b/tests/ArrTest.php index f26d131..1b3c090 100644 --- a/tests/ArrTest.php +++ b/tests/ArrTest.php @@ -2,43 +2,43 @@ use HiFolks\DataType\Arr; -it('is Array', function () { +it('is Array', function (): void { $arr = Arr::make(); expect($arr->arr())->toBeArray(); }); -it('creates Arr from function', function () { - $arr = Arr::fromFunction(fn () => random_int(0, 100), 500); +it('creates Arr from function', function (): void { + $arr = Arr::fromFunction(fn (): int => random_int(0, 100), 500); expect($arr->arr())->toBeArray(); expect($arr->length())->toEqual(500); - expect($arr->every(fn ($element) => $element >= 0))->toBeTrue(); - expect($arr->every(fn ($element) => $element <= 100))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element >= 0))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element <= 100))->toBeTrue(); $arr = Arr::fromFunction(fn ($i) => $i, 5000); expect($arr->arr())->toBeArray(); expect($arr->length())->toEqual(5000); //print_result($arr); - expect($arr->every(fn ($element) => $element >= 0))->toBeTrue(); - expect($arr->every(fn ($element) => $element <= 5000))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element >= 0))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element <= 5000))->toBeTrue(); }); -it('creates Arr from value', function () { +it('creates Arr from value', function (): void { $arr = Arr::fromValue(0, 5000); expect($arr->arr())->toBeArray(); expect($arr->length())->toEqual(5000); - expect($arr->every(fn ($element) => $element === 0))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element === 0))->toBeTrue(); }); -it('is Array empty', function () { +it('is Array empty', function (): void { $arr = Arr::make(); expect($arr->arr())->toBeArray(); expect(count($arr->arr()))->toEqual(0); expect($arr->count())->toEqual(0); expect($arr->length())->toEqual(0); }); -it('is Array length 1', function () { +it('is Array length 1', function (): void { $arr = Arr::make([99]); expect($arr->arr())->toBeArray(); expect(count($arr->arr()))->toEqual(1); @@ -46,7 +46,7 @@ expect($arr->length())->toEqual(1); }); -it('is Array forEach', function () { +it('is Array forEach', function (): void { $arr = Arr::make([99, 98]); expect($arr->arr())->toBeArray(); expect(count($arr->arr()))->toEqual(2); @@ -54,19 +54,19 @@ expect($arr->length())->toEqual(2); expect($arr->push(100))->toEqual(3); expect($arr->length())->toEqual(3); - $x = $arr->forEach(fn ($element, $key) => $key * $element.PHP_EOL); + $x = $arr->forEach(fn ($element, $key): string => $key * $element.PHP_EOL); expect($x->length())->toEqual(3); expect($x->get(2))->toEqual(200); }); -it('shift array', function () { +it('shift array', function (): void { $arr = Arr::make([99, 98, 97]); expect($arr->length())->toEqual(3); expect($arr->shift())->toEqual(99); expect($arr->length())->toEqual(2); }); -it('unshift array', function () { +it('unshift array', function (): void { $arr = Arr::make([99, 98, 97]); expect($arr->length())->toEqual(3); expect($arr->unshift(200))->toEqual(4); @@ -79,7 +79,7 @@ $arr->unshift(-2, -1); expect($arr->toString())->toEqual('-2,-1,0,1,2'); }); -it('append array', function () { +it('append array', function (): void { $arr = Arr::make([99, 98, 97])->append([1, 2, 3]); expect($arr->length())->toEqual(6); $arr->append([12], [13, 14]); @@ -91,19 +91,19 @@ expect($arr->length())->toEqual(0); expect($number)->toEqual(11); }); -it('joins arrays', function () { +it('joins arrays', function (): void { $arr = Arr::make([99, 98, 97])->append([1, 2, 3]); expect($arr->join())->toEqual('99,98,97,1,2,3'); }); -it('concats arrays', function () { +it('concats arrays', function (): void { $arr = Arr::make([99, 98, 97])->append([1, 2, 3]); $arr2 = $arr->concat([1000, 1001]); expect($arr2->arr())->toBeArray(); expect($arr2->length())->toEqual(8); expect($arr->length())->toEqual(6); }); -it('concats more types', function () { +it('concats more types', function (): void { $arr = Arr::make([99, 98, 97])->concat([1, 2, 3], [1000, 1001]); expect($arr->arr())->toBeArray(); expect($arr->length())->toEqual(8); @@ -112,7 +112,7 @@ $arr3 = $arr->concat($arr2); expect($arr3->length())->toEqual(18); }); -it('concats indexed/associative arrays', function () { +it('concats indexed/associative arrays', function (): void { $fruits = Arr::make([ 3 => '🥝', -1 => '🍓', @@ -127,7 +127,7 @@ expect($fruits2['mango'])->toEqual('🥭'); expect($fruits2[4])->toEqual('🍍'); }); -it('slices arrays', function () { +it('slices arrays', function (): void { $arr = Arr::make([99, 98, 97])->append([1, 2, 3]); $arr2 = $arr->slice(1, 2); expect($arr2->arr())->toBeArray(); @@ -193,7 +193,7 @@ expect($arr[0])->toBeNull(); }); -it('searches arrays', function () { +it('searches arrays', function (): void { $arr = Arr::make([99, 98, 97])->append([1, 2, 3]); $index = $arr->indexOf(1); expect($index)->toBeInt(); @@ -206,7 +206,7 @@ expect($arr->length())->toEqual(3); }); -it('searches last occurrence arrays', function () { +it('searches last occurrence arrays', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); $index = $arr->lastIndexOf(5); expect($index)->toBeInt(); @@ -219,39 +219,39 @@ expect($arr->length())->toEqual(5); }); -it('matches every element', function () { +it('matches every element', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - $bool = $arr->every(fn ($element) => $element > 0); + $bool = $arr->every(fn ($element): bool => $element > 0); expect($bool)->toEqual(true); - $bool = $arr->every(fn ($element) => $element > 1); + $bool = $arr->every(fn ($element): bool => $element > 1); expect($bool)->toEqual(false); - $bool = $arr->every(fn ($element) => $element > 10000); + $bool = $arr->every(fn ($element): bool => $element > 10000); expect($bool)->toEqual(false); }); -it('matches some element', function () { +it('matches some element', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - $bool = $arr->some(fn ($element) => $element > 0); + $bool = $arr->some(fn ($element): bool => $element > 0); expect($bool)->toEqual(true); - $bool = $arr->some(fn ($element) => $element > 1); + $bool = $arr->some(fn ($element): bool => $element > 1); expect($bool)->toEqual(true); - $bool = $arr->some(fn ($element) => $element > 10000); + $bool = $arr->some(fn ($element): bool => $element > 10000); expect($bool)->toEqual(false); $arr = Arr::make([1, 2, 3, 4, 5]); - $even = fn ($element) => $element % 2 === 0; + $even = fn ($element): bool => $element % 2 === 0; $bool = $arr->some($even); expect($bool)->toEqual(true); }); -it('filters some element', function () { +it('filters some element', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - $arr2 = $arr->filter(fn ($element) => $element > 3); + $arr2 = $arr->filter(fn ($element): bool => $element > 3); expect($arr->length())->toEqual(17); expect($arr2->length())->toEqual(11); - expect($arr->every(fn ($element) => $element > 0))->toBeTrue(); - expect($arr->every(fn ($element) => $element <= 3))->toBeFalse(); - expect($arr2->every(fn ($element) => $element > 3))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element > 0))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element <= 3))->toBeFalse(); + expect($arr2->every(fn ($element): bool => $element > 3))->toBeTrue(); }); it('finds the first element') @@ -261,48 +261,48 @@ 'strArr' => Arr::make(['begin','middle','end']) ], ]) - ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element) => $element > 0)) + ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element): bool => $element > 0)) ->toBeInt()->toEqual(1) - ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element) => $element < 5)) + ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element): bool => $element < 5)) ->toBeInt()->toEqual(1) - ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element) => $element > 5)) + ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element): bool => $element > 5)) ->toBeInt()->toEqual(6) - ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element, $index) => $element > 1 && $index > 1)) + ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element, $index): bool => $element > 1 && $index > 1)) ->toBeInt()->toEqual(3) - ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element) => $element > 50)) + ->expect(fn ($dataset) => $dataset->intArr->find(fn ($element): bool => $element > 50)) ->toBeNull() - ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element) => $element == 'end')) + ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element): bool => $element == 'end')) ->toBeString()->toBe('end') - ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element) => str_contains($element, 'e'))) + ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element): bool => str_contains((string) $element, 'e'))) ->toBeString()->toBe('begin') - ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element) => $element == 'prefix')) + ->expect(fn ($dataset) => $dataset->strArr->find(fn ($element): bool => $element == 'prefix')) ->toBeNull(); -it('finds the first index of some element', function () { +it('finds the first index of some element', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - $index = $arr->findIndex(fn ($element) => $element > 0); + $index = $arr->findIndex(fn ($element): bool => $element > 0); expect($index)->toBeInt(); expect($index)->toEqual(0); - $index = $arr->findIndex(fn ($element) => $element > 1); + $index = $arr->findIndex(fn ($element): bool => $element > 1); expect($index)->toEqual(1); - $index = $arr->findIndex(fn ($element) => $element > 10000); + $index = $arr->findIndex(fn ($element): bool => $element > 10000); expect($index)->toEqual(-1); - $index = $arr->findIndex(fn ($element, $index) => $element > 1 && $index > 1); + $index = $arr->findIndex(fn ($element, $index): bool => $element > 1 && $index > 1); expect($index)->toEqual(2); }); -it('maps elements', function () { +it('maps elements', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - $arr2 = $arr->map(fn ($element) => $element + 1); + $arr2 = $arr->map(fn ($element): int|float => $element + 1); expect($arr->length())->toEqual(17); expect($arr2->length())->toEqual(17); - expect($arr->every(fn ($element) => $element > 0))->toBeTrue(); - expect($arr->every(fn ($element) => $element > 1))->toBeFalse(); - expect($arr2->every(fn ($element) => $element > 1))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element > 0))->toBeTrue(); + expect($arr->every(fn ($element): bool => $element > 1))->toBeFalse(); + expect($arr2->every(fn ($element): bool => $element > 1))->toBeTrue(); }); -it('flats array', function () { +it('flats array', function (): void { $arr = Arr::make([1, [2, 3], 4, [5, 6, 7]]); $arr2 = $arr->flat(); @@ -310,10 +310,10 @@ expect($arr2->length())->toEqual(7); }); -it('flats and maps array', function () { +it('flats and maps array', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); - $arr2 = $arr->flatMap(fn ($element) => [$element, $element * 2]); + $arr2 = $arr->flatMap(fn ($element): array => [$element, $element * 2]); expect($arr->length())->toEqual(7); expect($arr2->length())->toEqual(14); expect($arr2->arr())->toEqual([1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14]); @@ -324,7 +324,7 @@ expect($arr2->arr())->toEqual([1, 2, 3, 4, 5, 6, 7]); }); -it('fills array', function () { +it('fills array', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); expect($arr->length())->toEqual(7); $arr->fill(0, 0, 3); @@ -336,30 +336,30 @@ expect($arr[4])->toEqual(5); expect($arr[5])->toEqual(6); expect($arr[6])->toEqual(7); - $arr2 = $arr->filter(fn ($element) => $element == 0); + $arr2 = $arr->filter(fn ($element): bool => $element == 0); expect($arr2->length())->toEqual(4); $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); $arr->fill(0); - $allAreZeros = $arr->every(fn ($element) => $element === 0); + $allAreZeros = $arr->every(fn ($element): bool => $element === 0); expect($arr->length())->toEqual(7); expect($allAreZeros)->toBeTrue(); }); -it('reduces Arr', function () { +it('reduces Arr', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); - $value = $arr->reduce(fn ($previousValue, $currentValue) => $previousValue + $currentValue); + $value = $arr->reduce(fn ($previousValue, $currentValue): float|int|array => $previousValue + $currentValue); expect($value)->toBeInt(); expect($value)->toEqual(28); }); -it('reduces Arr in reverse way', function () { +it('reduces Arr in reverse way', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); - $value = $arr->reduceRight(fn ($previousValue, $currentValue) => $previousValue + $currentValue); + $value = $arr->reduceRight(fn ($previousValue, $currentValue): float|int|array => $previousValue + $currentValue); expect($value)->toBeInt(); expect($value)->toEqual(28); }); -it('reverses Arr', function () { +it('reverses Arr', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7]); $arr2 = $arr->reverse(); expect($arr2->length())->toEqual(7); @@ -370,14 +370,14 @@ expect($arr->join())->toEqual('7,6,5,4,3,2,1,0'); }); -it('sorts Arr', function () { +it('sorts Arr', function (): void { $arr = Arr::make([6, 2, 4, 2, 1, 9, 7]); expect($arr->join())->toEqual('6,2,4,2,1,9,7'); $arr->sort(); expect($arr->length())->toEqual(7); expect($arr->join())->toEqual('1,2,2,4,6,7,9'); }); -it('sorts and change Arr', function () { +it('sorts and change Arr', function (): void { $months = Arr::make(['March', 'Jan', 'Feb', 'Dec']); $monthsSorted = $months->sort(); expect($months->length())->toEqual(4); @@ -390,7 +390,7 @@ expect($monthsSorted[3])->toBeNull(); }); -it('splices Arr', function () { +it('splices Arr', function (): void { $months = Arr::make(['Jan', 'March', 'April', 'June']); $months->splice(1, 0, 'Feb'); expect($months->join())->toEqual('Jan,Feb,March,April,June'); @@ -400,7 +400,7 @@ expect($months->join())->toEqual('Jan'); }); -it('stringifies an Arr', function () { +it('stringifies an Arr', function (): void { $months = Arr::make(['Jan', 'Feb', 'March', 'April', 'May']); expect($months->toString())->toEqual('Jan,Feb,March,April,May'); @@ -408,7 +408,7 @@ expect($arr->toString())->toEqual('1,2,a,1a'); }); -it('checks is array', function () { +it('checks is array', function (): void { $isArray = Arr::isArray(['Jan', 'Feb', 'March', 'April', 'May']); expect($isArray)->toEqual(true); $isArray = Arr::isArray(null); @@ -419,20 +419,20 @@ expect($isArray)->toEqual(false); }); -it('tests if array is empty', function () { +it('tests if array is empty', function (): void { $emptyArr = Arr::make([]); expect($emptyArr->isEmpty())->toEqual(true); $notEmptyArr = Arr::make([1]); expect($notEmptyArr->isEmpty())->toEqual(false); }); -it('chainable empty array', function () { +it('chainable empty array', function (): void { $arr = Arr::make([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - expect($arr->filter(fn ($element) => $element > 100)->isEmpty())->toBeTrue(); - expect($arr->filter(fn ($element) => $element < 100)->isEmpty())->toBeFalse(); + expect($arr->filter(fn ($element): bool => $element > 100)->isEmpty())->toBeTrue(); + expect($arr->filter(fn ($element): bool => $element < 100)->isEmpty())->toBeFalse(); }); -it('implements of() method', function () { +it('implements of() method', function (): void { $arr = Arr::of('Jan', 'Feb', 'March', 'April', 'May'); expect($arr->length())->toEqual(5); expect($arr[4])->toEqual('May'); @@ -442,7 +442,7 @@ expect($arr[0])->toEqual(7); }); -it('tests keys() method', function () { +it('tests keys() method', function (): void { $arr = Arr::make( [ '01' => 'Jan', '02' => 'Feb', '03' => 'March', '04' => 'April', @@ -466,7 +466,7 @@ } }); -it('test keys() method', function () { +it('test keys() method', function (): void { $arr = Arr::make( [5, 12, 8, 130, 44] ); @@ -476,7 +476,7 @@ expect($arr->at(100))->toBeNull(); }); -it('includes element', function () { +it('includes element', function (): void { $arr = Arr::make( [1, 2, 3] ); @@ -493,7 +493,7 @@ expect($arr->includes('at'))->toBeFalse(); }); -it('includes element from index', function () { +it('includes element from index', function (): void { $arr = Arr::make( [1, 2, 3] ); @@ -516,7 +516,7 @@ expect($arr->includes('a', -2))->toBeFalse(); }); -it(' extract values', function () { +it(' extract values', function (): void { $fruits = Arr::make([ 7 => '🥝', -1 => '🍓', @@ -544,7 +544,7 @@ expect($i)->toEqual(8); }); -it('creates entries', function () { +it('creates entries', function (): void { $fruits = Arr::make([ 7 => '🥝', -1 => '🍓', @@ -571,7 +571,7 @@ ])); }); -it('tests copyWithin() method with one parameter', function () { +it('tests copyWithin() method with one parameter', function (): void { $arr = Arr::make([1, 2, 3, 4, 5]); $result = $arr->copyWithin(-2); @@ -581,7 +581,7 @@ ->toEqual([1, 2, 3, 1, 2]); }); -it('tests copyWithin() method with two parameters', function () { +it('tests copyWithin() method with two parameters', function (): void { $arr = Arr::make([1, 2, 3, 4, 5]); $result = $arr->copyWithin(0, 3); @@ -591,7 +591,7 @@ ->toEqual([4, 5, 3, 4, 5]); }); -it('tests copyWithin() method with all the parameters', function () { +it('tests copyWithin() method with all the parameters', function (): void { $arr = Arr::make([1, 2, 3, 4, 5]); $result = $arr->copyWithin(0, 3, 4); @@ -601,7 +601,7 @@ ->toEqual([4, 2, 3, 4, 5]); }); -it('tests copyWithin() method with all the parameters negative', function () { +it('tests copyWithin() method with all the parameters negative', function (): void { $arr = Arr::make([1, 2, 3, 4, 5]); $result = $arr->copyWithin(-2, -3, -1); @@ -611,18 +611,18 @@ ->toEqual([1, 2, 3, 3, 4]); }); -it('tests flatMap can handle an array of arrays', function () { +it('tests flatMap can handle an array of arrays', function (): void { $arr = Arr::make([ [1, 2], [3, 4] ]); - $result = $arr->flatMap(fn ($element) => $element * 2); + $result = $arr->flatMap(fn ($element): int|float => $element * 2); expect($result->length())->toEqual(4); expect($result->arr())->toEqual([2, 4, 6, 8]); }); -it('can unset array elements by their keys', function () { +it('can unset array elements by their keys', function (): void { $arr = Arr::make([ 'mango' => '🥭', 'apple' => '🍎', @@ -648,7 +648,7 @@ ]); }); -it('can set array key', function () { +it('can set array key', function (): void { $arr = Arr::make([ 'mango' => '🥭', 'banana' => '🍌' diff --git a/tests/ArrayAccessTest.php b/tests/ArrayAccessTest.php index 545d33e..f27c1b6 100644 --- a/tests/ArrayAccessTest.php +++ b/tests/ArrayAccessTest.php @@ -2,14 +2,14 @@ use HiFolks\DataType\Arr; -it('access to element', function () { +it('access to element', function (): void { $arr = Arr::make([100, 101, 102]); expect($arr[0])->toEqual(100); expect($arr[1])->toEqual(101); expect($arr[2])->toEqual(102); }); -it('create elements', function () { +it('create elements', function (): void { $arr = Arr::make(); $arr['test01'] = 'Some'; $arr['test02'] = 'Thing'; @@ -26,7 +26,7 @@ expect($arr[2])->toBeNull(); expect($arr[1])->toEqual($arr->get(1)); }); -it('isset and empty', function () { +it('isset and empty', function (): void { $arr = Arr::make(); $arr['test01'] = 'Some'; $arr['test02'] = 'Thing'; @@ -37,7 +37,7 @@ expect(empty($arr['not exists']))->toBeTrue(); }); -it('unset', function () { +it('unset', function (): void { $arr = Arr::make(); $arr['test01'] = 'Some'; $arr['test02'] = 'Thing'; diff --git a/tests/CalcTest.php b/tests/CalcTest.php index 0fd19de..416b2fb 100644 --- a/tests/CalcTest.php +++ b/tests/CalcTest.php @@ -2,7 +2,7 @@ use HiFolks\DataType\Arr; -it('sum Array', function () { +it('sum Array', function (): void { $arr = Arr::make([1, 2, 3]); expect($arr->sum())->toEqual(6); @@ -12,7 +12,7 @@ expect($arr->sum())->toEqual(100); }); -it('avg Array', function () { +it('avg Array', function (): void { $arr = Arr::make([1, 2, 3]); expect($arr->avg())->toEqual(2); diff --git a/tests/IterateTest.php b/tests/IterateTest.php index 1de4492..55cf754 100644 --- a/tests/IterateTest.php +++ b/tests/IterateTest.php @@ -2,7 +2,7 @@ use HiFolks\DataType\Arr; -it('iterates', function () { +it('iterates', function (): void { $arr = Arr::make([100, 101, 102]); foreach ($arr as $element) { expect($element)->toBeGreaterThanOrEqual(100); @@ -10,7 +10,7 @@ } }); -it('iterates prev and next', function () { +it('iterates prev and next', function (): void { $arr = Arr::make([100, 101, 102]); $arr->next(); $arr->next(); diff --git a/tests/TableOperationTest.php b/tests/TableOperationTest.php index 2ed9254..bcfb785 100644 --- a/tests/TableOperationTest.php +++ b/tests/TableOperationTest.php @@ -11,7 +11,7 @@ ['product' => 'Door', 'price' => 100, 'active' => true], ]; -it('multiply by 2 a field', function () use ($dataTable) { +it('multiply by 2 a field', function () use ($dataTable): void { $table = Table::make($dataTable); $resultTable = $table->calc("price2", Operation::double("price")); expect($table->getFromFirst("price"))->toEqual(200); @@ -21,7 +21,7 @@ expect($table->first()->get("price2"))->toEqual(400); expect($resultTable->first()->get("price2"))->toEqual(400); }); -it('add value to a field', function () use ($dataTable) { +it('add value to a field', function () use ($dataTable): void { $table = Table::make($dataTable); $resultTable = $table->calc("price2", Operation::add("price", 50)); expect($table->getFromFirst("price"))->toEqual(200); diff --git a/tests/TableTest.php b/tests/TableTest.php index f7c0334..7f94c0e 100644 --- a/tests/TableTest.php +++ b/tests/TableTest.php @@ -11,7 +11,7 @@ ['product' => 'Door', 'price' => 100, 'active' => true], ]; -it('is Table', function () use ($dataTable) { +it('is Table', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows()) ->toBeArray() @@ -19,12 +19,12 @@ ->toBeInstanceOf(Arr::class); }); -it('is Table countable', function () use ($dataTable) { +it('is Table countable', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table)->toHaveCount(5); }); -it('is iterable', function () use ($dataTable) { +it('is iterable', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table)->toBeInstanceOf(Iterator::class); foreach ($table as $row) { @@ -45,7 +45,7 @@ ->toMatchArray(['product' => 'Chair', 'price' => 100, 'active' => true]); }); -it('can get first', function () use ($dataTable) { +it('can get first', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows())->toBeArray() ->and($table->first()) @@ -58,7 +58,7 @@ ->toMatchArray(['product', 'price', 'active']); }); -it('can get column from first', function () use ($dataTable) { +it('can get column from first', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows()) ->toBeArray() @@ -69,7 +69,7 @@ ->toBeNull(); }); -it('can get last', function () use ($dataTable) { +it('can get last', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows())->toBeArray() ->and($table->last())->toHaveCount(3) @@ -82,7 +82,7 @@ ->toMatchArray(['product', 'price', 'active']); }); -it('can get column from last', function () use ($dataTable) { +it('can get column from last', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows()) ->toBeArray() @@ -93,7 +93,7 @@ ->toBeNull(); }); -it('can select', function () use ($dataTable) { +it('can select', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows())->toBeArray() ->and( @@ -111,7 +111,7 @@ ->toMatchArray(['product' => 'Desk', 'active' => true]); }); -it('can except', function () use ($dataTable) { +it('can except', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows())->toBeArray() ->and($table->except('price')->last()) @@ -137,7 +137,7 @@ ->and($table->rows())->toHaveCount(5); }); -it('can filter', function () use ($dataTable) { +it('can filter', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table->rows())->toBeArray() ->and($table->select('product', 'price')->last()) @@ -177,7 +177,7 @@ )->toEqual(100); }); -it('can filter greater than', function () use ($dataTable) { +it('can filter greater than', function () use ($dataTable): void { $table = Table::make($dataTable); expect( $table->select('product', 'price') @@ -191,7 +191,7 @@ )->toHaveCount(5); }); -it('can filter true', function () use ($dataTable) { +it('can filter true', function () use ($dataTable): void { $table = Table::make($dataTable); expect( $table->where('active') @@ -199,7 +199,7 @@ )->toHaveCount(4); }); -it('can filter smaller', function () use ($dataTable) { +it('can filter smaller', function () use ($dataTable): void { $table = Table::make($dataTable); expect( @@ -214,7 +214,7 @@ )->toHaveCount(0); }); -it('can filter not equal', function () use ($dataTable) { +it('can filter not equal', function () use ($dataTable): void { $table = Table::make($dataTable); expect( @@ -236,13 +236,13 @@ )->toHaveCount(5); }); -it('can create calculated field', function () use ($dataTable) { +it('can create calculated field', function () use ($dataTable): void { $table = Table::make($dataTable); $calculatedTable = $table ->select('product', 'price') ->where('price', '>', 100) - ->calc('new_field', fn ($item) => $item['price'] * 2); + ->calc('new_field', fn ($item): int|float => $item['price'] * 2); expect($calculatedTable)->toHaveCount(3) ->and($calculatedTable->first()?->get('price'))->toEqual(200) @@ -251,7 +251,7 @@ ->and($calculatedTable->last()?->get('new_field'))->toEqual(300); }); -it('can group', function () use ($dataTable) { +it('can group', function () use ($dataTable): void { $table = Table::make($dataTable); $groupedTable = $table->groupBy('product'); @@ -263,14 +263,14 @@ ->toMatchArray(['product' => 'Bookcase', 'price' => 150, 'active' => true]); }); -it('can append Arr', function () use ($dataTable) { +it('can append Arr', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table)->toHaveCount(5); $table->append(Arr::make([])); expect($table)->toHaveCount(6); }); -it('can append array', function () use ($dataTable) { +it('can append array', function () use ($dataTable): void { $table = Table::make($dataTable); expect($table)->toHaveCount(5); $table->append([]); @@ -281,7 +281,7 @@ }); -it('orders by desc', function () use ($dataTable) { +it('orders by desc', function () use ($dataTable): void { $table = Table::make($dataTable); $orderedTable = $table->orderBy('price'); expect($orderedTable) @@ -292,7 +292,7 @@ ->toMatchArray(['product' => 'Door', 'price' => 100, 'active' => true]); }); -it('orders by asc', function () use ($dataTable) { +it('orders by asc', function () use ($dataTable): void { $table = Table::make($dataTable); $orderedTable = $table->orderBy('product', 'asc'); expect($orderedTable) @@ -303,7 +303,7 @@ ->toMatchArray(['product' => 'Door', 'price' => 100, 'active' => true]); }); -it('can get the cheapest of all products that are active', function () use ($dataTable) { +it('can get the cheapest of all products that are active', function () use ($dataTable): void { $table = Table::make($dataTable); $cheapestOfEachProduct = $table ->where('active', '=', true) @@ -318,9 +318,9 @@ ->toMatchArray(['product' => 'Desk', 'price' => 200, 'active' => true]); }); -it('can transform all of the elements in a specific column', function () use ($dataTable) { +it('can transform all of the elements in a specific column', function () use ($dataTable): void { $table = Table::make($dataTable); - $cheapestOfEachProduct = $table->transform('price', fn ($price) => number_format($price, 2)); + $cheapestOfEachProduct = $table->transform('price', fn ($price): string => number_format($price, 2)); expect($cheapestOfEachProduct) ->toHaveCount(5) @@ -330,7 +330,7 @@ ->toMatchArray(['product' => 'Door', 'price' => '100.00', 'active' => true]); }); -it('can transform to native array', function () use ($dataTable) { +it('can transform to native array', function () use ($dataTable): void { $table = Table::make($dataTable); $array = $table->toArray(); From 7b1a93a1d057696cc1b83c15532d0592d959158d Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 23 May 2024 23:07:06 +0200 Subject: [PATCH 2/4] Update pint.yml --- .github/workflows/pint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pint.yml b/.github/workflows/pint.yml index 58e98ef..e64161d 100644 --- a/.github/workflows/pint.yml +++ b/.github/workflows/pint.yml @@ -13,7 +13,7 @@ jobs: ref: ${{ github.head_ref }} - name: "Run Laravel Pint" - uses: aglipanci/laravel-pint-action@2.4.0 + uses: aglipanci/laravel-pint-action@2.0.0 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v5 From 466cb6989ac59590a710886c32fd97cb9507b5d7 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 23 May 2024 23:19:30 +0200 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04cac66..5fa8892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ # Changelog -## 0.1.2 - WIP +## 1.0.0 - 2024-05-23 - PHPStan Level 6, thanks to @RoadSigns - Updating Pint action for the GitHub Actions -- Adding Rector with UP_TO_PHP_80, DEAD_CODE, CODE_QUALITY, EARLY_RETURN, TYPE_DECLARATION, PRIVATIZATION options +- Upgrading dependencies (Rector 1, PHP 8.3, some GitHub Actions, ...) +- Adding Rector with UP_TO_PHP_81, DEAD_CODE, CODE_QUALITY, EARLY_RETURN, TYPE_DECLARATION, PRIVATIZATION options ## 0.1.1 - 2022-12-08 Welcome to PHP 8.2. From 423b59977ab972007f52c482e623ad23434844d8 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 23 May 2024 23:19:33 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 14534e5..2bbd32a 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,6 @@ You can install the package via composer: composer require hi-folks/array ``` -> Currently, this package is under development. It's **not** "production ready". It is to be considered "not production ready" until it is in version v0.0.x. When version 0.1.x will be released, it means that the package is considered stable. ## Usage