Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 5 additions & 10 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ jobs:
quality:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [ 8.2 ]

name: CS PHP ${{ matrix.php }}
name: Code style

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v5

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
php-version: 8.4

- name: Install
run: composer install --no-interaction
run: composer install --prefer-dist --no-interaction

- name: Code style checks for PHP
run: composer cs
run: composer cs
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
php: [ 8.2, 8.1 ]
php: [ 8.5, 8.4, 8.3, 8.2 ]

name: Tests ${{ matrix.os }} - PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v5

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Install
run: composer install --no-interaction
run: composer install --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 portavice GmbH
Copyright (c) 2023-2025 portavice GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Permutation
[![Latest Version on Packagist](https://img.shields.io/packagist/v/portavice/permutation.svg?style=flat-square)](https://packagist.org/packages/portavice/permutation)
![Test Status](https://img.shields.io/github/actions/workflow/status/portavice/Permutation/tests.yml?branch=main&label=Tests)
![Code Style Status](https://img.shields.io/github/actions/workflow/status/portavice/Permutation/code-style.yml?branch=main&label=Code%20Style)

[![MIT Licensed](https://img.shields.io/badge/License-MIT-brightgreen.svg)](LICENSE.md)
<a href="https://packagist.org/packages/portavice/permutation"><img src="https://img.shields.io/packagist/php-v/portavice/permutation.svg?style=flat-square" alt="PHP from Packagist"></a>
[![Tests](https://github.com/portavice/Permutation/actions/workflows/tests.yml/badge.svg)](https://github.com/portavice/bladestrap/actions/workflows/tests.yml)
[![Code style check](https://github.com/portavice/Permutation/actions/workflows/code-style.yml/badge.svg)](https://github.com/portavice/bladestrap/actions/workflows/code-style.yml)
[![Latest Version](https://img.shields.io/packagist/v/portavice/permutation.svg?style=flat-square)](https://packagist.org/packages/portavice/permutation)
[![Total Downloads](https://img.shields.io/packagist/dt/portavice/permutation.svg?style=flat-square)](https://packagist.org/packages/portavice/permutation)

This is a simple permutation library for PHP.
Expand Down Expand Up @@ -118,7 +120,7 @@ foreach ($permutations as $permutation) {
This library is licensed under the MIT license.

## Author
This library was written by [Shaun Lüdeke](https://github.com/shaunluedeke) for [Portavice GmbH](https://portavice.de/).
This library was written by [Shaun Lüdeke](https://github.com/shaunluedeke) for [portavice GmbH](https://portavice.de/).

## Development

Expand All @@ -130,4 +132,4 @@ This library was written by [Shaun Lüdeke](https://github.com/shaunluedeke) for
### Code Style
PHP code MUST follow [PSR-12 specification](https://www.php-fig.org/psr/psr-12/).

We use [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for the PHP code style check.
We use [Laravel Pint](https://laravel.com/docs/12.x/pint#configuring-pint) for the PHP code style check.
28 changes: 13 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "portavice/permutation",
"description": "Generating permutations of arrays normal and recursive",
"type": "library",
"minimum-stability": "dev",
"license": "MIT",
"homepage": "https://portavice.de",
"keywords": [
Expand All @@ -22,13 +21,11 @@
"issues": "https://github.com/portavice/Permutation/issues"
},
"require": {
"php": "^8.0"
"php": "^8.2"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
"phpcompatibility/php-compatibility": "^9.3.5",
"phpunit/phpunit": "^10.2.2",
"squizlabs/php_codesniffer": "^3.7.2"
"phpunit/phpunit": "^11.5",
"portavice/laravel-pint-config": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,19 +35,20 @@
"Portavice\\Permutation\\": "src"
}
},
"scripts": {
"test": "phpunit",
"cs": ["phpcs"],
"csfix": ["phpcbf"]
},
"autoload-dev": {
"psr-4": {
"Portavice\\Permutation\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
"scripts": {
"cs": [
"pint --config vendor/portavice/laravel-pint-config/pint.json --test"
],
"csfix": [
"pint --config vendor/portavice/laravel-pint-config/pint.json"
],
"test": [
"phpunit"
]
}
}
21 changes: 0 additions & 21 deletions phpcs.xml

This file was deleted.

9 changes: 3 additions & 6 deletions src/Permutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Permutation
private mixed $callback = null;
private bool $unsetAfterCall = false;
private ?array $callbackArgs;

public function __construct(array $input, int $offset = 0, int $limit = 0)
{
$this->input = array_map(
Expand Down Expand Up @@ -80,7 +81,7 @@ final public function permutate(): self
break;
}
for ($currentColumnIndex = 0; $currentColumnIndex < $columnCount; $currentColumnIndex++) {
$index = (int)($currentPermutation / $matrixInfo[$currentColumnIndex]['cumulativeCount'])
$index = (int) ($currentPermutation / $matrixInfo[$currentColumnIndex]['cumulativeCount'])
% $matrixInfo[$currentColumnIndex]['count'];
$this->result[$currentPermutation][$currentColumnIndex] = $matrix[$currentColumnIndex][$index];
}
Expand Down Expand Up @@ -133,8 +134,6 @@ final public function permutateRecursive(): self

/**
* Returns a generator for all permutations.
*
* @return Generator
*/
final public function generator(): Generator
{
Expand Down Expand Up @@ -186,8 +185,6 @@ final public function generator(): Generator
* Generates all permutations recursively.
*
* Note: Order of permutations is different from Permutation::permutateRecursive().
*
* @return Generator
*/
final public function recursiveGenerator(): Generator
{
Expand Down Expand Up @@ -248,7 +245,7 @@ private function getValues(array $input): array
final public function getResult(bool $withSort = false): array
{
if ($withSort) {
usort($this->result, static fn($a, $b) => count($a) <=> count($b));
usort($this->result, static fn ($a, $b) => count($a) <=> count($b));
}
return $this->result;
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ final public function testPermutate(): void
{
$this->assertEquals([
['a', 'b', 'c'],
['a', 'b', 'd']
['a', 'b', 'd'],
], Permutation::getPermutations([
'a', 'b', ['c', 'd']
'a', 'b', ['c', 'd'],
]));
}

Expand All @@ -26,48 +26,48 @@ final public function testPermutateRecursive(): void
[1 => 'b'],
['a', 'b'],
[2 => 'c'],
['a', 2 => 'c'],
['a', 2 => 'c'],
[1 => 'b', 2 => 'c'],
['a', 'b', 'c'],
[2 => 'd'],
['a', 2 => 'd'],
[1 => 'b', 2 => 'd'],
['a', 'b', 'd']
['a', 'b', 'd'],
], Permutation::getPermutationsRecursive([
'a', 'b', ['c', 'd']
'a', 'b', ['c', 'd'],
]));
}

final public function testPermutateWithCallback(): void
{
$this->callBackOutput = [];
Permutation::getPermutationsWithCallback([
'a', 'b', ['c', 'd']
'a', 'b', ['c', 'd'],
], [$this, 'permutationOutPutReturn'], true);
$this->assertEquals([
['a', 'b', 'c'],
['a', 'b', 'd']
['a', 'b', 'd'],
], $this->callBackOutput);
}

final public function testPermutateRecursiveWithCallback(): void
{
$this->callBackOutput = [];
Permutation::getPermutationsRecursiveWithCallback([
'a', 'b', ['c', 'd']
'a', 'b', ['c', 'd'],
], [$this, 'permutationOutPutReturn'], true);
$this->assertEquals([
['a'],
[1 => 'b'],
['a', 'b'],
[2 => 'c'],
['a', 2 => 'c'],
['a', 2 => 'c'],
[1 => 'b', 2 => 'c'],
['a', 'b', 'c'],
[2 => 'd'],
['a', 2 => 'd'],
[1 => 'b', 2 => 'd'],
['a', 'b', 'd']
['a', 'b', 'd'],
], $this->callBackOutput);
}

Expand Down