Skip to content

Commit 56d7c32

Browse files
Add rector actions. (#127)
* Add rector actions. * Update dependencies yiisoft/cache `2.0`. * Update dependencies yiisoft/aliases `2.0`. * Fixed unit tests. * Add tests php `8.2`. * Use union types.
1 parent b21b112 commit 56d7c32

22 files changed

Lines changed: 145 additions & 219 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
php:
4141
- 8.0
4242
- 8.1
43+
- 8.2
4344

4445
steps:
4546
- name: Checkout

.github/workflows/rector.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
on:
2+
pull_request:
3+
paths-ignore:
4+
- 'docs/**'
5+
- 'README.md'
6+
- 'CHANGELOG.md'
7+
- '.gitignore'
8+
- '.gitattributes'
9+
- 'infection.json.dist'
10+
- 'psalm.xml'
11+
12+
push:
13+
paths-ignore:
14+
- 'docs/**'
15+
- 'README.md'
16+
- 'CHANGELOG.md'
17+
- '.gitignore'
18+
- '.gitattributes'
19+
- 'infection.json.dist'
20+
- 'psalm.xml'
21+
22+
name: rector
23+
24+
jobs:
25+
rector:
26+
uses: yiisoft/actions/.github/workflows/rector.yml@master
27+
with:
28+
os: >-
29+
['ubuntu-latest']
30+
php: >-
31+
['8.0']

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
"require-dev": {
3030
"ext-json": "*",
3131
"phpunit/phpunit": "^9.5",
32+
"rector/rector": "^0.14",
3233
"roave/infection-static-analysis-plugin": "^1.16",
3334
"spatie/phpunit-watcher": "^1.23",
3435
"vimeo/psalm": "^4.18",
35-
"yiisoft/aliases": "^1.1|^2.0",
36+
"yiisoft/aliases": "^2.0",
3637
"yiisoft/json": "^1.0"
3738
},
3839
"autoload": {

rector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->paths([
11+
__DIR__ . '/src',
12+
__DIR__ . '/tests',
13+
]);
14+
15+
// register a single rule
16+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
17+
18+
// define sets of rules
19+
$rectorConfig->sets([
20+
LevelSetList::UP_TO_PHP_80,
21+
]);
22+
};

src/BaseTokenizer.php

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
*/
3434
abstract class BaseTokenizer
3535
{
36-
/**
37-
* @var string SQL code.
38-
*/
39-
private string $sql;
40-
4136
/**
4237
* @var int SQL code string length.
4338
*/
@@ -76,11 +71,14 @@ abstract class BaseTokenizer
7671
/**
7772
* @var SqlToken|null resulting token of a last {@see tokenize()} call.
7873
*/
79-
private ?SqlToken $token = null;
80-
81-
public function __construct(string $sql)
82-
{
83-
$this->sql = $sql;
74+
private SqlToken|null $token = null;
75+
76+
public function __construct(
77+
/**
78+
* @var string SQL code.
79+
*/
80+
private string $sql
81+
) {
8482
}
8583

8684
/**
@@ -173,7 +171,7 @@ abstract protected function isComment(int &$length): bool;
173171
*
174172
* @return bool whether there's an operator at the current offset.
175173
*/
176-
abstract protected function isOperator(int &$length, ?string &$content): bool;
174+
abstract protected function isOperator(int &$length, string|null &$content): bool;
177175

178176
/**
179177
* Returns whether there's an identifier at the current offset.
@@ -186,7 +184,7 @@ abstract protected function isOperator(int &$length, ?string &$content): bool;
186184
*
187185
* @return bool whether there's an identifier at the current offset.
188186
*/
189-
abstract protected function isIdentifier(int &$length, ?string &$content): bool;
187+
abstract protected function isIdentifier(int &$length, string|null &$content): bool;
190188

191189
/**
192190
* Returns whether there's a string literal at the current offset.
@@ -199,7 +197,7 @@ abstract protected function isIdentifier(int &$length, ?string &$content): bool;
199197
*
200198
* @return bool whether there's a string literal at the current offset.
201199
*/
202-
abstract protected function isStringLiteral(int &$length, ?string &$content): bool;
200+
abstract protected function isStringLiteral(int &$length, string|null &$content): bool;
203201

204202
/**
205203
* Returns whether the given string is a keyword.
@@ -211,11 +209,8 @@ abstract protected function isStringLiteral(int &$length, ?string &$content): bo
211209
*
212210
* @return bool whether the given string is a keyword.
213211
*/
214-
abstract protected function isKeyword(string $string, ?string &$content): bool;
212+
abstract protected function isKeyword(string $string, string|null &$content): bool;
215213

216-
/**
217-
* @param string $sql
218-
*/
219214
public function setSql(string $sql): void
220215
{
221216
$this->sql = $sql;
@@ -237,16 +232,14 @@ protected function startsWithAnyLongest(
237232
array $with,
238233
bool $caseSensitive,
239234
int &$length,
240-
?string &$content = null
235+
string &$content = null
241236
): bool {
242237
if (empty($with)) {
243238
return false;
244239
}
245240

246241
if (!is_array(reset($with))) {
247-
usort($with, static function (string $string1, string $string2) {
248-
return mb_strlen($string2, 'UTF-8') - mb_strlen($string1, 'UTF-8');
249-
});
242+
usort($with, static fn (string $string1, string $string2) => mb_strlen($string2, 'UTF-8') - mb_strlen($string1, 'UTF-8'));
250243

251244
$map = [];
252245

@@ -279,7 +272,7 @@ protected function startsWithAnyLongest(
279272
*
280273
* @return string result string, it may be empty if there's nothing to return.
281274
*/
282-
protected function substring(int $length, bool $caseSensitive = true, ?int $offset = null): string
275+
protected function substring(int $length, bool $caseSensitive = true, int $offset = null): string
283276
{
284277
if ($offset === null) {
285278
$offset = $this->offset;
@@ -310,7 +303,7 @@ protected function substring(int $length, bool $caseSensitive = true, ?int $offs
310303
*
311304
* @return int index after the given string or end of string index.
312305
*/
313-
protected function indexAfter(string $string, ?int $offset = null): int
306+
protected function indexAfter(string $string, int $offset = null): int
314307
{
315308
if ($offset === null) {
316309
$offset = $this->offset;
@@ -333,10 +326,6 @@ protected function indexAfter(string $string, ?int $offset = null): int
333326

334327
/**
335328
* Determines whether there is a delimited string at the current offset and adds it to the token children.
336-
*
337-
* @param int $length
338-
*
339-
* @return bool
340329
*/
341330
private function tokenizeDelimitedString(int &$length): bool
342331
{
@@ -360,10 +349,6 @@ private function tokenizeDelimitedString(int &$length): bool
360349

361350
/**
362351
* Determines whether there is an operator at the current offset and adds it to the token children.
363-
*
364-
* @param int $length
365-
*
366-
* @return bool
367352
*/
368353
private function tokenizeOperator(int &$length): bool
369354
{
@@ -457,8 +442,6 @@ private function addTokenFromBuffer(): void
457442
/**
458443
* Adds the specified length to the current offset.
459444
*
460-
* @param int $length
461-
*
462445
* @throws InvalidArgumentException
463446
*/
464447
private function advance(int $length): void
@@ -473,8 +456,6 @@ private function advance(int $length): void
473456

474457
/**
475458
* Returns whether the SQL code is completely traversed.
476-
*
477-
* @return bool
478459
*/
479460
private function isEof(): bool
480461
{

src/Builder/InConditionBuilder.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,12 @@ protected function buildSubqueryInCondition(
4848
/**
4949
* Builds SQL for IN condition.
5050
*
51-
* @param string|null $operator
52-
* @param array|Traversable $columns
5351
* @param iterable|Iterator $values
54-
* @param array $params
5552
*
5653
* @return string SQL.
5754
*/
5855
protected function buildCompositeInCondition(
59-
?string $operator,
56+
string|null $operator,
6057
Traversable|array $columns,
6158
iterable|Iterator $values,
6259
array &$params = []

src/Builder/LikeConditionBuilder.php

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

1010
final class LikeConditionBuilder extends BaseLikeConditionBuilder
1111
{
12-
protected ?string $escapeCharacter = '\\';
12+
protected string|null $escapeCharacter = '\\';
1313

1414
public function __construct(QueryBuilderInterface $queryBuilder)
1515
{

src/ColumnSchemaBuilder.php

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

77
use Yiisoft\Db\Schema\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;
88

9-
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
9+
final class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder implements \Stringable
1010
{
1111
/**
1212
* Builds the unsigned string for column. Defaults to unsupported.

src/CommandPDO.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function execute(): int
101101
return $result;
102102
}
103103

104-
protected function internalExecute(?string $rawSql): void
104+
protected function internalExecute(string|null $rawSql): void
105105
{
106106
$attempt = 0;
107107

@@ -112,7 +112,10 @@ protected function internalExecute(?string $rawSql): void
112112
&& $this->isolationLevel !== null
113113
&& $this->db->getTransaction() === null
114114
) {
115-
$this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
115+
$this->db->transaction(
116+
fn (string|null $rawSql) => $this->internalExecute($rawSql),
117+
$this->isolationLevel,
118+
);
116119
} else {
117120
$this->pdoStatement?->execute();
118121
}
@@ -179,9 +182,6 @@ protected function queryInternal(int $queryMode): mixed
179182
* Splits the specified SQL codes into individual SQL statements and returns them or `false` if there's a single
180183
* statement.
181184
*
182-
* @param string $sql
183-
* @param array $params
184-
*
185185
* @throws InvalidArgumentException
186186
*
187187
* @return array|bool (array|string)[][]|bool
@@ -217,11 +217,6 @@ private function splitStatements(string $sql, array $params): bool|array
217217
/**
218218
* Returns named bindings used in the specified statement token.
219219
*
220-
* @param SqlToken $statement
221-
* @param array $params
222-
*
223-
* @return array
224-
*
225220
* @psalm-param array<string, string> $params
226221
*/
227222
private function extractUsedParams(SqlToken $statement, array $params): array

src/ConnectionPDO.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Yiisoft\Db\Schema\SchemaInterface;
1616
use Yiisoft\Db\Transaction\TransactionInterface;
1717

18-
use function strncmp;
19-
2018
/**
2119
* Database connection class prefilled for SQLite Server.
2220
*/
@@ -29,13 +27,13 @@ public function __clone()
2927
{
3028
$this->transaction = null;
3129

32-
if (strncmp($this->driver->getDsn(), 'sqlite::memory:', 15) !== 0) {
30+
if (!str_starts_with($this->driver->getDsn(), 'sqlite::memory:')) {
3331
/** reset PDO connection, unless its sqlite in-memory, which can only have one connection */
3432
$this->pdo = null;
3533
}
3634
}
3735

38-
public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
36+
public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
3937
{
4038
$command = new CommandPDO($this, $this->queryCache);
4139

0 commit comments

Comments
 (0)