Skip to content

Commit 2d2cd72

Browse files
author
Six
committed
Added php-cs-fixer
1 parent 5e1641b commit 2d2cd72

File tree

8 files changed

+79
-52
lines changed

8 files changed

+79
-52
lines changed

.php-cs-fixer.dist.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$finder = PhpCsFixer\Finder::create()
6+
->in(__DIR__.'/src')
7+
->in(__DIR__.'/tests')
8+
->exclude('vendor');
9+
10+
return (new PhpCsFixer\Config())
11+
->setRules([
12+
'@Symfony' => true,
13+
'@Symfony:risky' => true,
14+
'protected_to_private' => false,
15+
'phpdoc_to_comment' => false,
16+
])
17+
->setFinder($finder)
18+
->setRiskyAllowed(true);

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"ext-gmp": "*"
2020
},
2121
"require-dev": {
22-
"phpstan/phpstan": "^2.1.22",
22+
"friendsofphp/php-cs-fixer": "^3.87",
2323
"phpstan/extension-installer": "^1.4.3",
24+
"phpstan/phpstan": "^2.1.22",
2425
"phpstan/phpstan-deprecation-rules": "^2.0.3",
2526
"phpstan/phpstan-phpunit": "^2.0.7",
2627
"phpstan/phpstan-strict-rules": "^2.0.6",
@@ -50,6 +51,8 @@
5051
"test": "phpunit",
5152
"stan": "phpstan analyse",
5253
"coverage": "XDEBUG_MODE=coverage phpunit --coverage-html coverage-html --coverage-text",
53-
"coverage-text": "XDEBUG_MODE=coverage phpunit --coverage-text"
54+
"coverage-text": "XDEBUG_MODE=coverage phpunit --coverage-text",
55+
"cs-fix": "php-cs-fixer fix",
56+
"cs-check": "php-cs-fixer fix --dry-run --diff"
5457
}
5558
}

src/AbstractStrategy.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics;
56

6-
use Countable;
7-
use InvalidArgumentException;
8-
use IteratorAggregate;
9-
use Traversable;
10-
use function count;
11-
127
/**
138
* @template T
14-
* @implements IteratorAggregate<int, list<T>>
9+
*
10+
* @implements \IteratorAggregate<int, list<T>>
1511
*/
16-
abstract readonly class AbstractStrategy implements Countable, IteratorAggregate
12+
abstract readonly class AbstractStrategy implements \Countable, \IteratorAggregate
1713
{
1814
/** @var list<T> */
1915
protected array $elements;
@@ -26,39 +22,42 @@
2622

2723
/**
2824
* @param array<array-key, T> $elements
29-
* @param int<1, max> $k
30-
* @throws InvalidArgumentException
25+
* @param int<1, max> $k
26+
*
27+
* @throws \InvalidArgumentException
3128
*/
3229
final public function __construct(array $elements, int $k)
3330
{
34-
/** @noinspection PhpConditionAlreadyCheckedInspection */
31+
/* @noinspection PhpConditionAlreadyCheckedInspection */
3532
if ($k < 1) {
36-
throw new InvalidArgumentException("\$k must be greater than zero, got: $k");
33+
throw new \InvalidArgumentException("\$k must be greater than zero, got: $k");
3734
}
3835

3936
$this->elements = array_values($elements);
40-
$this->n = count($this->elements);
37+
$this->n = \count($this->elements);
4138
$this->k = $k;
4239

4340
$this->assertValid();
4441
}
4542

4643
protected function assertValid(): void
4744
{
48-
if ($this->n === 0) {
49-
throw new InvalidArgumentException('Cannot generate combinations/permutations from empty array.');
45+
if (0 === $this->n) {
46+
throw new \InvalidArgumentException('Cannot generate combinations/permutations from empty array.');
5047
}
5148
}
5249

5350
/**
5451
* @param list<T> $elements
52+
*
5553
* @return list<T>
5654
*/
5755
abstract protected function next(array $elements, int $i): array;
5856

5957
/**
6058
* @param list<T> $elements
6159
* @param list<T> $result
60+
*
6261
* @return iterable<int, list<T>>
6362
*/
6463
final protected function generate(array $elements, int $slot = 0, array &$result = [], int &$index = 0): iterable
@@ -78,7 +77,7 @@ final protected function generate(array $elements, int $slot = 0, array &$result
7877
}
7978
}
8079

81-
final public function getIterator(): Traversable
80+
final public function getIterator(): \Traversable
8281
{
8382
yield from $this->generate($this->elements);
8483
}

src/Combination/WithRepetition.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics\Combination;
56

67
use Blibio\Combinatorics\AbstractStrategy;
7-
use Override;
8-
use function array_slice;
98

109
/**
1110
* @template T
11+
*
1212
* @extends AbstractStrategy<T>
1313
*/
1414
final readonly class WithRepetition extends AbstractStrategy
1515
{
16-
#[Override]
16+
#[\Override]
1717
protected function next(array $elements, int $i): array
1818
{
19-
return array_slice($elements, $i);
19+
return \array_slice($elements, $i);
2020
}
2121

22-
#[Override]
22+
#[\Override]
2323
public function count(): int
2424
{
2525
/** @var int<0, max> */

src/Combination/WithoutRepetition.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics\Combination;
56

67
use Blibio\Combinatorics\AbstractStrategy;
7-
use InvalidArgumentException;
8-
use Override;
9-
use function array_slice;
108

119
/**
1210
* @template T
11+
*
1312
* @extends AbstractStrategy<T>
1413
*/
1514
final readonly class WithoutRepetition extends AbstractStrategy
1615
{
17-
#[Override]
16+
#[\Override]
1817
protected function assertValid(): void
1918
{
2019
parent::assertValid();
21-
20+
2221
if ($this->n < $this->k) {
23-
throw new InvalidArgumentException("\$k ({$this->k}) must not be greater than number of elements ({$this->n})");
22+
throw new \InvalidArgumentException("\$k ({$this->k}) must not be greater than number of elements ({$this->n})");
2423
}
2524
}
2625

27-
#[Override]
26+
#[\Override]
2827
protected function next(array $elements, int $i): array
2928
{
30-
return array_slice($elements, $i + 1);
29+
return \array_slice($elements, $i + 1);
3130
}
3231

33-
#[Override]
32+
#[\Override]
3433
public function count(): int
3534
{
3635
/** @var int<0, max> */

src/Combinatorics.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics;
56

6-
use InvalidArgumentException;
7-
87
final readonly class Combinatorics
98
{
109
/**
1110
* @template U
11+
*
1212
* @param array<array-key, U> $elements
13-
* @param int<1, max> $k
13+
* @param int<1, max> $k
14+
*
1415
* @return Combination\WithRepetition<U>
1516
*
16-
* @throws InvalidArgumentException
17+
* @throws \InvalidArgumentException
1718
*/
1819
public static function combinationsWithRepetition(array $elements, int $k): Combination\WithRepetition
1920
{
@@ -22,11 +23,13 @@ public static function combinationsWithRepetition(array $elements, int $k): Comb
2223

2324
/**
2425
* @template U
26+
*
2527
* @param array<array-key, U> $elements
26-
* @param int<1, max> $k
28+
* @param int<1, max> $k
29+
*
2730
* @return Combination\WithoutRepetition<U>
2831
*
29-
* @throws InvalidArgumentException
32+
* @throws \InvalidArgumentException
3033
*/
3134
public static function combinationsWithoutRepetition(array $elements, int $k): Combination\WithoutRepetition
3235
{
@@ -35,11 +38,13 @@ public static function combinationsWithoutRepetition(array $elements, int $k): C
3538

3639
/**
3740
* @template U
41+
*
3842
* @param array<array-key, U> $elements
39-
* @param int<1, max> $k
43+
* @param int<1, max> $k
44+
*
4045
* @return Permutation\WithRepetition<U>
4146
*
42-
* @throws InvalidArgumentException
47+
* @throws \InvalidArgumentException
4348
*/
4449
public static function permutationsWithRepetition(array $elements, int $k): Permutation\WithRepetition
4550
{
@@ -48,11 +53,13 @@ public static function permutationsWithRepetition(array $elements, int $k): Perm
4853

4954
/**
5055
* @template U
56+
*
5157
* @param array<array-key, U> $elements
52-
* @param int<1, max> $k
58+
* @param int<1, max> $k
59+
*
5360
* @return Permutation\WithoutRepetition<U>
5461
*
55-
* @throws InvalidArgumentException
62+
* @throws \InvalidArgumentException
5663
*/
5764
public static function permutationsWithoutRepetition(array $elements, int $k): Permutation\WithoutRepetition
5865
{

src/Permutation/WithRepetition.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics\Permutation;
56

67
use Blibio\Combinatorics\AbstractStrategy;
7-
use Override;
88

99
/**
1010
* @template T
11+
*
1112
* @extends AbstractStrategy<T>
1213
*/
1314
final readonly class WithRepetition extends AbstractStrategy
1415
{
15-
#[Override]
16+
#[\Override]
1617
protected function next(array $elements, int $i): array
1718
{
1819
return $elements;
1920
}
2021

21-
#[Override]
22+
#[\Override]
2223
public function count(): int
2324
{
2425
/** @var int<0, max> */

src/Permutation/WithoutRepetition.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace Blibio\Combinatorics\Permutation;
56

67
use Blibio\Combinatorics\AbstractStrategy;
7-
use InvalidArgumentException;
8-
use Override;
98

109
/**
1110
* @template T
11+
*
1212
* @extends AbstractStrategy<T>
1313
*/
1414
final readonly class WithoutRepetition extends AbstractStrategy
1515
{
16-
#[Override]
16+
#[\Override]
1717
protected function assertValid(): void
1818
{
1919
parent::assertValid();
20-
20+
2121
if ($this->n < $this->k) {
22-
throw new InvalidArgumentException("\$k ({$this->k}) must not be greater than number of elements ({$this->n})");
22+
throw new \InvalidArgumentException("\$k ({$this->k}) must not be greater than number of elements ({$this->n})");
2323
}
2424
}
2525

26-
#[Override]
26+
#[\Override]
2727
protected function next(array $elements, int $i): array
2828
{
2929
array_splice($elements, $i, 1);
3030

3131
return $elements;
3232
}
3333

34-
#[Override]
34+
#[\Override]
3535
public function count(): int
3636
{
3737
/** @var int<0, max> */

0 commit comments

Comments
 (0)