Skip to content

Commit

Permalink
refact: rewrite of the library
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
drupol committed Jan 2, 2025
1 parent c64ff50 commit 8ce8cb3
Show file tree
Hide file tree
Showing 41 changed files with 1,244 additions and 817 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ charset = utf-8
max_line_length = 80
indent_style = space
indent_size = 4
insert_final_newline = true
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake github:loophp/nix-shell#env-php82 --impure
26 changes: 18 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,35 @@
}
],
"require": {
"php": ">= 7.1.3"
"php": ">= 8.2"
},
"require-dev": {
"drupol/php-conventions": "^1.7.1",
"phpunit/php-code-coverage": "^4 || ^5",
"phpunit/phpunit": "^6 || ^7"
"ext-pcov": "*",
"phpunit/php-code-coverage": "^11",
"phpunit/phpunit": "^11",
"symfony/finder": "^7.2",
"symfony/yaml": "^7.2"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"ergebnis/composer-normalize": true,
"phpro/grumphp": true,
"phpstan/extension-installer": true
}
},
"autoload": {
"psr-4": {
"drupol\\phpermutations\\": "src/",
"drupol\\phpermutations\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"drupol\\phpermutations\\Tests\\": "tests/src/"
}
},
"scripts": {
"grumphp": "./vendor/bin/grumphp run",
"phpunit": "./vendor/bin/phpunit --coverage-clover build/logs/clover.xml -c tests/phpunit.xml tests"
"phpunit": "./vendor/bin/phpunit --coverage-clover build/logs/clover.xml -c phpunit.xml tests"
},
"support": {
"issues": "https://github.com/drupol/phpermutations/issues",
Expand Down
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
19 changes: 0 additions & 19 deletions src/Combinatorics.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ public function __construct(array $dataset = [], $length = null)
$this->setLength($length);
}

/**
* Count elements of an object.
*
* @return int
* The number of element
*/
public function count(): int
{
return count($this->toArray());
}

/**
* Get the dataset.
*
Expand Down Expand Up @@ -108,14 +97,6 @@ public function setLength($length = null): Combinatorics
return $this;
}

/**
* @return array<int, mixed>
*/
public function toArray(): array
{
return [];
}

/**
* Compute the factorial of an integer.
*
Expand Down
25 changes: 3 additions & 22 deletions src/Iterators.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace drupol\phpermutations;

use Countable;
use Iterator;

abstract class Iterators extends Combinatorics implements Countable, IteratorInterface
abstract class Iterators extends Combinatorics implements Iterator
{
/**
* A copy of the dataset at a give time.
Expand All @@ -15,10 +16,7 @@ abstract class Iterators extends Combinatorics implements Countable, IteratorInt
*/
protected $current;

/**
* @var int
*/
protected $key = 0;
protected int $key = 0;

/**
* {@inheritdoc}
Expand All @@ -45,21 +43,4 @@ public function rewind(): void
{
$this->key = 0;
}

/**
* Convert the iterator into an array.
*
* @return array<int, mixed>
* The elements
*/
public function toArray(): array
{
$data = [];

for ($this->rewind(); $this->valid(); $this->next()) {
$data[] = $this->current();
}

return $data;
}
}
68 changes: 8 additions & 60 deletions src/Iterators/Combinations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,24 @@

use drupol\phpermutations\Iterators;

class Combinations extends Iterators
final class Combinations extends Iterators
{
/**
* The values.
*
* @var array<int, mixed>
*/
protected $c = [];
private array $c = [];

/**
* Combinations constructor.
*
* @param array<int, mixed> $dataset
* The dataset
* @param int|null $length
* The length
*/
public function __construct(array $dataset = [], $length = null)
public function __construct(array $dataset = [], ?int $length = null)
{
parent::__construct(array_values($dataset), $length);
$this->rewind();
}

/**
* {@inheritdoc}
*/
public function count(): int
{
$i = 0;

for ($this->rewind(); $this->valid(); $this->next()) {
++$i;
}

return $i;
}

/**
* {@inheritdoc}
*/
public function current(): mixed
{
$r = [];

for ($i = 0; $i < $this->length; ++$i) {
$r[] = $this->dataset[$this->c[$i]];
}

return $r;
return array_map(
fn(int $index): mixed => $this->dataset[$this->c[$index]],
range(0, $this->length - 1)
);
}

/**
* {@inheritdoc}
*
* @return void
*/
public function next(): void
{
if ($this->nextHelper()) {
Expand All @@ -71,32 +33,18 @@ public function next(): void
}
}

/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->c = range(0, $this->length);
$this->key = 0;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function valid(): bool
{
return 0 <= $this->key;
}

/**
* Custom next() callback.
*
* @return bool
* Return true or false
*/
protected function nextHelper(): bool
private function nextHelper(): bool
{
$i = $this->length - 1;

Expand Down
2 changes: 1 addition & 1 deletion src/Iterators/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use function count;

class Cycle extends Iterators
final class Cycle extends Iterators
{
/**
* {@inheritdoc}
Expand Down
48 changes: 4 additions & 44 deletions src/Iterators/Fibonacci.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,31 @@

use const PHP_INT_MAX;

class Fibonacci extends Iterators
final class Fibonacci extends Iterators
{
/**
* @var int
*/
protected $current;

/**
* The maximum limit.
*
* @var int
*/
protected $max;
protected int $max;

/**
* The previous element.
*
* @var int
*/
private $previous = 1;
private int $previous = 1;

/**
* Fibonacci constructor.
*/
public function __construct()
{
$this->setMaxLimit(PHP_INT_MAX);
parent::__construct();
}

/**
* Get the maximum limit.
*
* @return int
* The limit
*/
public function getMaxLimit(): int
{
return (int) $this->max;
}

/**
* {@inheritdoc}
*
* @return void
*/
public function next(): void
{
[$this->current, $this->previous] = [$this->current + $this->previous, $this->current];
++$this->key;
}

/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->previous = 1;
Expand All @@ -72,22 +42,12 @@ public function rewind(): void

/**
* Set the maximum limit.
*
* @param int $max
* The limit
*
* @return void
*/
public function setMaxLimit($max): void
public function setMaxLimit(int $max): void
{
$this->max = $max;
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function valid(): bool
{
return $this->getMaxLimit() > $this->current;
Expand Down
4 changes: 1 addition & 3 deletions src/Iterators/FiniteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
use function count;

/**
* Class FiniteGroup.
*
* The finite group is an abelian finite cyclic group.
*/
class FiniteGroup extends Iterators
final class FiniteGroup extends Iterators
{
/**
* The group.
Expand Down
Loading

0 comments on commit 8ce8cb3

Please sign in to comment.