Skip to content

Commit deab314

Browse files
authored
Add PHP CS Fixer (#1088)
1 parent bdb6552 commit deab314

199 files changed

Lines changed: 1336 additions & 1564 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
name: Rector + PHP CS Fixer
2+
13
on:
2-
pull_request_target:
4+
pull_request:
35
paths:
46
- 'src/**'
57
- 'config/**'
68
- 'tests/**'
7-
- '.github/workflows/rector.yml'
9+
- '.github/workflows/rector-cs.yml'
810
- 'composer.json'
911
- 'rector.php'
12+
- '.php-cs-fixer.dist.php'
1013

11-
name: Rector
14+
permissions:
15+
contents: read
1216

1317
concurrency:
1418
group: ${{ github.workflow }}-${{ github.ref }}
1519
cancel-in-progress: true
1620

1721
jobs:
1822
rector:
19-
uses: yiisoft/actions/.github/workflows/rector.yml@master
23+
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
2024
secrets:
2125
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
2226
with:
2327
repository: ${{ github.event.pull_request.head.repo.full_name }}
24-
php: >-
25-
['8.4']
28+
php: '8.4'

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ c3.php
4747

4848
# Docker
4949
docker/docker-compose.override.yml
50+
51+
# PHP CS Fixer
52+
/.php-cs-fixer.cache

.php-cs-fixer.dist.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\Finder;
7+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
8+
9+
$finder = (new Finder())->in([
10+
__DIR__ . '/config',
11+
__DIR__ . '/src',
12+
__DIR__ . '/tests',
13+
]);
14+
15+
return (new Config())
16+
->setParallelConfig(ParallelConfigFactory::detect())
17+
->setRules([
18+
'@PER-CS3.0' => true,
19+
'no_unused_imports' => true,
20+
'ordered_class_elements' => true,
21+
'class_attributes_separation' => ['elements' => ['method' => 'one']],
22+
])
23+
->setFinder($finder);

.styleci.yml

Lines changed: 0 additions & 85 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"yiisoft/db-implementation": "1.0.0"
3939
},
4040
"require-dev": {
41+
"friendsofphp/php-cs-fixer": "^3.89.1",
4142
"maglnet/composer-require-checker": "^4.7.1",
4243
"phpunit/phpunit": "^10.5.45",
4344
"rector/rector": "^2.0.10",

src/Cache/SchemaCache.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
final class SchemaCache
3434
{
35-
private int|null|DateInterval $duration = 3600;
35+
private int|DateInterval|null $duration = 3600;
3636
private bool $enabled = true;
3737
private array $exclude = [];
3838

@@ -41,9 +41,7 @@ final class SchemaCache
4141
*
4242
* @link https://www.php-fig.org/psr/psr-16/
4343
*/
44-
public function __construct(private CacheInterface $psrCache)
45-
{
46-
}
44+
public function __construct(private CacheInterface $psrCache) {}
4745

4846
/**
4947
* Remove a value with the specified key from cache.
@@ -96,7 +94,7 @@ public function set(mixed $key, mixed $value, ?string $tag = null): void
9694
/**
9795
* @return DateInterval|int|null The number of seconds that table metadata can remain valid in cache.
9896
*/
99-
public function getDuration(): int|null|DateInterval
97+
public function getDuration(): int|DateInterval|null
10098
{
10199
return $this->duration;
102100
}
@@ -161,7 +159,7 @@ public function setEnabled(bool $value): void
161159
*
162160
* @see setEnabled()
163161
*/
164-
public function setDuration(int|null|DateInterval $value): void
162+
public function setDuration(int|DateInterval|null $value): void
165163
{
166164
$this->duration = $value;
167165
}
@@ -199,7 +197,7 @@ public function setExclude(array $value): void
199197
private function normalize(mixed $key): string
200198
{
201199
if (is_string($key) || is_int($key)) {
202-
$key = (string)$key;
200+
$key = (string) $key;
203201
$length = strlen($key);
204202
return (strpbrk($key, '{}()/\@:') !== false || $length < 1 || $length > 64) ? md5($key) : $key;
205203
}

src/Command/AbstractCommand.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@
6969
*/
7070
abstract class AbstractCommand implements CommandInterface
7171
{
72-
/**
73-
* @param ConnectionInterface $db The database connection to use.
74-
*/
75-
public function __construct(
76-
protected readonly ConnectionInterface $db,
77-
) {
78-
}
79-
8072
/**
8173
* Command in this query mode returns count of affected rows.
8274
*
@@ -123,15 +115,22 @@ public function __construct(
123115
/**
124116
* @var string|null Name of the table to refresh schema for. Null means not to refresh the schema.
125117
*/
126-
protected string|null $refreshTableName = null;
127-
protected Closure|null $retryHandler = null;
118+
protected ?string $refreshTableName = null;
119+
protected ?Closure $retryHandler = null;
128120
protected bool $dbTypecasting = true;
129121
protected bool $phpTypecasting = false;
130122
/**
131123
* @var string The SQL statement to execute.
132124
*/
133125
private string $sql = '';
134126

127+
/**
128+
* @param ConnectionInterface $db The database connection to use.
129+
*/
130+
public function __construct(
131+
protected readonly ConnectionInterface $db,
132+
) {}
133+
135134
public function addCheck(string $table, string $name, string $expression): static
136135
{
137136
$sql = $this->getQueryBuilder()->addCheck($table, $name, $expression);
@@ -169,7 +168,7 @@ public function addForeignKey(
169168
string $referenceTable,
170169
array|string $referenceColumns,
171170
?string $delete = null,
172-
?string $update = null
171+
?string $update = null,
173172
): static {
174173
$sql = $this->getQueryBuilder()->addForeignKey(
175174
$table,
@@ -178,7 +177,7 @@ public function addForeignKey(
178177
$referenceTable,
179178
$referenceColumns,
180179
$delete,
181-
$update
180+
$update,
182181
);
183182
return $this->setSql($sql)->requireTableSchemaRefresh($table);
184183
}
@@ -241,7 +240,7 @@ public function createIndex(
241240
string $name,
242241
array|string $columns,
243242
?string $indexType = null,
244-
?string $indexMethod = null
243+
?string $indexMethod = null,
245244
): static {
246245
$sql = $this->getQueryBuilder()->createIndex($table, $name, $columns, $indexType, $indexMethod);
247246
return $this->setSql($sql)->requireTableSchemaRefresh($table);
@@ -438,14 +437,14 @@ public function queryColumn(): array
438437
return is_array($results) ? $results : [];
439438
}
440439

441-
public function queryOne(): array|null
440+
public function queryOne(): ?array
442441
{
443442
/** @psalm-var array<string,mixed>|false $results */
444443
$results = $this->queryInternal(self::QUERY_MODE_ROW);
445444
return is_array($results) ? $results : null;
446445
}
447446

448-
public function queryScalar(): bool|string|null|int|float
447+
public function queryScalar(): bool|string|int|float|null
449448
{
450449
$result = $this->queryInternal(self::QUERY_MODE_SCALAR);
451450

@@ -493,7 +492,7 @@ public function setSql(string $sql): static
493492
return $this;
494493
}
495494

496-
public function setRetryHandler(Closure|null $handler): static
495+
public function setRetryHandler(?Closure $handler): static
497496
{
498497
$this->retryHandler = $handler;
499498
return $this;
@@ -510,7 +509,7 @@ public function update(
510509
array $columns,
511510
array|ExpressionInterface|string $condition = '',
512511
array|ExpressionInterface|string|null $from = null,
513-
array $params = []
512+
array $params = [],
514513
): static {
515514
$sql = $this->getQueryBuilder()->update($table, $columns, $condition, $from, $params);
516515
return $this->setSql($sql)->bindValues($params);
@@ -530,7 +529,7 @@ public function upsertReturning(
530529
string $table,
531530
array|QueryInterface $insertColumns,
532531
array|bool $updateColumns = true,
533-
array|null $returnColumns = null,
532+
?array $returnColumns = null,
534533
): array {
535534
if ($returnColumns === []) {
536535
$this->upsert($table, $insertColumns, $updateColumns)->execute();

src/Command/CommandInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public function addForeignKey(
129129
array|string $columns,
130130
string $referenceTable,
131131
array|string $referenceColumns,
132-
string|null $delete = null,
133-
string|null $update = null
132+
?string $delete = null,
133+
?string $update = null,
134134
): static;
135135

136136
/**
@@ -229,7 +229,7 @@ public function bindParam(
229229
mixed &$value,
230230
?int $dataType = null,
231231
?int $length = null,
232-
mixed $driverOptions = null
232+
mixed $driverOptions = null,
233233
): static;
234234

235235
/**
@@ -318,7 +318,7 @@ public function createIndex(
318318
string $name,
319319
array|string $columns,
320320
?string $indexType = null,
321-
?string $indexMethod = null
321+
?string $indexMethod = null,
322322
): static;
323323

324324
/**
@@ -691,7 +691,7 @@ public function queryColumn(): array;
691691
*
692692
* @psalm-return array<string,mixed>|null
693693
*/
694-
public function queryOne(): array|null;
694+
public function queryOne(): ?array;
695695

696696
/**
697697
* Execute the SQL statement and returns the value of the first column in the first row of data.
@@ -706,7 +706,7 @@ public function queryOne(): array|null;
706706
*
707707
* @psalm-return null|scalar
708708
*/
709-
public function queryScalar(): bool|string|null|int|float;
709+
public function queryScalar(): bool|string|int|float|null;
710710

711711
/**
712712
* Creates an SQL command for renaming a column.
@@ -785,7 +785,7 @@ public function setRawSql(string $sql): static;
785785
*
786786
* @param Closure|null $handler A PHP callback to handle database exceptions.
787787
*/
788-
public function setRetryHandler(Closure|null $handler): static;
788+
public function setRetryHandler(?Closure $handler): static;
789789

790790
/**
791791
* Specifies the SQL statement to execute. The SQL statement will be quoted using
@@ -856,7 +856,7 @@ public function update(
856856
array $columns,
857857
array|ExpressionInterface|string $condition = '',
858858
array|ExpressionInterface|string|null $from = null,
859-
array $params = []
859+
array $params = [],
860860
): static;
861861

862862
/**
@@ -935,7 +935,7 @@ public function upsertReturning(
935935
string $table,
936936
array|QueryInterface $insertColumns,
937937
array|bool $updateColumns = true,
938-
array|null $returnColumns = null,
938+
?array $returnColumns = null,
939939
): array;
940940

941941
/**

0 commit comments

Comments
 (0)