Skip to content

Commit 8ce8cb3

Browse files
committed
refact: rewrite of the library
- Remove Generators - It was not necessary to keep them - Switch to PHPUnit 11 - Remove temporarily drupol/php-conventions, deps issue with vimeo/psalm - Remove methods toArray and count - Remove custom interfaces
1 parent c64ff50 commit 8ce8cb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1244
-817
lines changed

.editorconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ charset = utf-8
66
max_line_length = 80
77
indent_style = space
88
indent_size = 4
9-
insert_final_newline = true

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake github:loophp/nix-shell#env-php82 --impure

composer.json

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,35 @@
2121
}
2222
],
2323
"require": {
24-
"php": ">= 7.1.3"
24+
"php": ">= 8.2"
2525
},
2626
"require-dev": {
27-
"drupol/php-conventions": "^1.7.1",
28-
"phpunit/php-code-coverage": "^4 || ^5",
29-
"phpunit/phpunit": "^6 || ^7"
27+
"ext-pcov": "*",
28+
"phpunit/php-code-coverage": "^11",
29+
"phpunit/phpunit": "^11",
30+
"symfony/finder": "^7.2",
31+
"symfony/yaml": "^7.2"
3032
},
3133
"config": {
32-
"sort-packages": true
34+
"sort-packages": true,
35+
"allow-plugins": {
36+
"ergebnis/composer-normalize": true,
37+
"phpro/grumphp": true,
38+
"phpstan/extension-installer": true
39+
}
3340
},
3441
"autoload": {
3542
"psr-4": {
36-
"drupol\\phpermutations\\": "src/",
43+
"drupol\\phpermutations\\": "src/"
44+
}
45+
},
46+
"autoload-dev": {
47+
"psr-4": {
3748
"drupol\\phpermutations\\Tests\\": "tests/src/"
3849
}
3950
},
4051
"scripts": {
41-
"grumphp": "./vendor/bin/grumphp run",
42-
"phpunit": "./vendor/bin/phpunit --coverage-clover build/logs/clover.xml -c tests/phpunit.xml tests"
52+
"phpunit": "./vendor/bin/phpunit --coverage-clover build/logs/clover.xml -c phpunit.xml tests"
4353
},
4454
"support": {
4555
"issues": "https://github.com/drupol/phpermutations/issues",

phpunit.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
shortenArraysForExportThreshold="10"
8+
requireCoverageMetadata="true"
9+
beStrictAboutCoverageMetadata="true"
10+
beStrictAboutOutputDuringTests="true"
11+
displayDetailsOnPhpunitDeprecations="true"
12+
failOnPhpunitDeprecation="true"
13+
failOnRisky="true"
14+
failOnWarning="true">
15+
<testsuites>
16+
<testsuite name="default">
17+
<directory>tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
22+
<include>
23+
<directory>src</directory>
24+
</include>
25+
</source>
26+
</phpunit>

src/Combinatorics.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ public function __construct(array $dataset = [], $length = null)
4444
$this->setLength($length);
4545
}
4646

47-
/**
48-
* Count elements of an object.
49-
*
50-
* @return int
51-
* The number of element
52-
*/
53-
public function count(): int
54-
{
55-
return count($this->toArray());
56-
}
57-
5847
/**
5948
* Get the dataset.
6049
*
@@ -108,14 +97,6 @@ public function setLength($length = null): Combinatorics
10897
return $this;
10998
}
11099

111-
/**
112-
* @return array<int, mixed>
113-
*/
114-
public function toArray(): array
115-
{
116-
return [];
117-
}
118-
119100
/**
120101
* Compute the factorial of an integer.
121102
*

src/Iterators.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
namespace drupol\phpermutations;
66

77
use Countable;
8+
use Iterator;
89

9-
abstract class Iterators extends Combinatorics implements Countable, IteratorInterface
10+
abstract class Iterators extends Combinatorics implements Iterator
1011
{
1112
/**
1213
* A copy of the dataset at a give time.
@@ -15,10 +16,7 @@ abstract class Iterators extends Combinatorics implements Countable, IteratorInt
1516
*/
1617
protected $current;
1718

18-
/**
19-
* @var int
20-
*/
21-
protected $key = 0;
19+
protected int $key = 0;
2220

2321
/**
2422
* {@inheritdoc}
@@ -45,21 +43,4 @@ public function rewind(): void
4543
{
4644
$this->key = 0;
4745
}
48-
49-
/**
50-
* Convert the iterator into an array.
51-
*
52-
* @return array<int, mixed>
53-
* The elements
54-
*/
55-
public function toArray(): array
56-
{
57-
$data = [];
58-
59-
for ($this->rewind(); $this->valid(); $this->next()) {
60-
$data[] = $this->current();
61-
}
62-
63-
return $data;
64-
}
6546
}

src/Iterators/Combinations.php

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,24 @@
66

77
use drupol\phpermutations\Iterators;
88

9-
class Combinations extends Iterators
9+
final class Combinations extends Iterators
1010
{
11-
/**
12-
* The values.
13-
*
14-
* @var array<int, mixed>
15-
*/
16-
protected $c = [];
11+
private array $c = [];
1712

18-
/**
19-
* Combinations constructor.
20-
*
21-
* @param array<int, mixed> $dataset
22-
* The dataset
23-
* @param int|null $length
24-
* The length
25-
*/
26-
public function __construct(array $dataset = [], $length = null)
13+
public function __construct(array $dataset = [], ?int $length = null)
2714
{
2815
parent::__construct(array_values($dataset), $length);
2916
$this->rewind();
3017
}
3118

32-
/**
33-
* {@inheritdoc}
34-
*/
35-
public function count(): int
36-
{
37-
$i = 0;
38-
39-
for ($this->rewind(); $this->valid(); $this->next()) {
40-
++$i;
41-
}
42-
43-
return $i;
44-
}
45-
46-
/**
47-
* {@inheritdoc}
48-
*/
4919
public function current(): mixed
5020
{
51-
$r = [];
52-
53-
for ($i = 0; $i < $this->length; ++$i) {
54-
$r[] = $this->dataset[$this->c[$i]];
55-
}
56-
57-
return $r;
21+
return array_map(
22+
fn(int $index): mixed => $this->dataset[$this->c[$index]],
23+
range(0, $this->length - 1)
24+
);
5825
}
5926

60-
/**
61-
* {@inheritdoc}
62-
*
63-
* @return void
64-
*/
6527
public function next(): void
6628
{
6729
if ($this->nextHelper()) {
@@ -71,32 +33,18 @@ public function next(): void
7133
}
7234
}
7335

74-
/**
75-
* {@inheritdoc}
76-
*/
7736
public function rewind(): void
7837
{
7938
$this->c = range(0, $this->length);
8039
$this->key = 0;
8140
}
8241

83-
/**
84-
* {@inheritdoc}
85-
*
86-
* @return bool
87-
*/
8842
public function valid(): bool
8943
{
9044
return 0 <= $this->key;
9145
}
9246

93-
/**
94-
* Custom next() callback.
95-
*
96-
* @return bool
97-
* Return true or false
98-
*/
99-
protected function nextHelper(): bool
47+
private function nextHelper(): bool
10048
{
10149
$i = $this->length - 1;
10250

src/Iterators/Cycle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use function count;
1010

11-
class Cycle extends Iterators
11+
final class Cycle extends Iterators
1212
{
1313
/**
1414
* {@inheritdoc}

src/Iterators/Fibonacci.php

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,31 @@
88

99
use const PHP_INT_MAX;
1010

11-
class Fibonacci extends Iterators
11+
final class Fibonacci extends Iterators
1212
{
13-
/**
14-
* @var int
15-
*/
1613
protected $current;
1714

18-
/**
19-
* The maximum limit.
20-
*
21-
* @var int
22-
*/
23-
protected $max;
15+
protected int $max;
2416

25-
/**
26-
* The previous element.
27-
*
28-
* @var int
29-
*/
30-
private $previous = 1;
17+
private int $previous = 1;
3118

32-
/**
33-
* Fibonacci constructor.
34-
*/
3519
public function __construct()
3620
{
3721
$this->setMaxLimit(PHP_INT_MAX);
3822
parent::__construct();
3923
}
4024

41-
/**
42-
* Get the maximum limit.
43-
*
44-
* @return int
45-
* The limit
46-
*/
4725
public function getMaxLimit(): int
4826
{
4927
return (int) $this->max;
5028
}
5129

52-
/**
53-
* {@inheritdoc}
54-
*
55-
* @return void
56-
*/
5730
public function next(): void
5831
{
5932
[$this->current, $this->previous] = [$this->current + $this->previous, $this->current];
6033
++$this->key;
6134
}
6235

63-
/**
64-
* {@inheritdoc}
65-
*/
6636
public function rewind(): void
6737
{
6838
$this->previous = 1;
@@ -72,22 +42,12 @@ public function rewind(): void
7242

7343
/**
7444
* Set the maximum limit.
75-
*
76-
* @param int $max
77-
* The limit
78-
*
79-
* @return void
8045
*/
81-
public function setMaxLimit($max): void
46+
public function setMaxLimit(int $max): void
8247
{
8348
$this->max = $max;
8449
}
8550

86-
/**
87-
* {@inheritdoc}
88-
*
89-
* @return bool
90-
*/
9151
public function valid(): bool
9252
{
9353
return $this->getMaxLimit() > $this->current;

src/Iterators/FiniteGroup.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
use function count;
1010

1111
/**
12-
* Class FiniteGroup.
13-
*
1412
* The finite group is an abelian finite cyclic group.
1513
*/
16-
class FiniteGroup extends Iterators
14+
final class FiniteGroup extends Iterators
1715
{
1816
/**
1917
* The group.

0 commit comments

Comments
 (0)