Skip to content

Commit 7324b1a

Browse files
authored
add rector (#1062)
1 parent eafc8aa commit 7324b1a

142 files changed

Lines changed: 1813 additions & 1957 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.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
"stan-setup": "phive install",
7777
"lowest": "validate-prefer-lowest",
7878
"lowest-setup": "composer update --prefer-lowest --prefer-stable --prefer-dist --no-interaction && cp composer.json composer.backup && composer require --dev dereuromark/composer-prefer-lowest && mv composer.backup composer.json",
79+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
80+
"rector-check": "vendor/bin/rector process --dry-run",
81+
"rector-fix": "vendor/bin/rector process",
7982
"test": "phpunit",
8083
"test-coverage": "phpunit --coverage-clover=clover.xml"
8184
}

phpstan-baseline.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,24 @@ parameters:
6060
count: 1
6161
path: src/Migration/Environment.php
6262

63+
-
64+
message: '#^Method Migrations\\Migration\\Manager\:\:getMigrationClassName\(\) should return class\-string\<Migrations\\MigrationInterface\> but returns string\.$#'
65+
identifier: return.type
66+
count: 2
67+
path: src/Migration/Manager.php
68+
6369
-
6470
message: '#^Parameter \#1 \.\.\.\$arg1 of function max expects non\-empty\-array, array given\.$#'
6571
identifier: argument.type
6672
count: 1
6773
path: src/Migration/Manager.php
6874

75+
-
76+
message: '#^Parameter \#3 \$length of function substr expects int\|null, int\<0, max\>\|false given\.$#'
77+
identifier: argument.type
78+
count: 1
79+
path: src/Migration/Manager.php
80+
6981
-
7082
message: '#^Offset 0 on non\-empty\-list\<string\> in isset\(\) always exists and is not nullable\.$#'
7183
identifier: isset.offset

rector.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
5+
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
6+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
7+
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
8+
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
9+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
10+
use Rector\Config\RectorConfig;
11+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
12+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
13+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
14+
use Rector\Set\ValueObject\SetList;
15+
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
16+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;
17+
18+
$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';
19+
20+
return RectorConfig::configure()
21+
->withPaths([
22+
__DIR__ . '/src',
23+
__DIR__ . '/tests',
24+
])
25+
26+
->withCache(
27+
cacheClass: FileCacheStorage::class,
28+
cacheDirectory: $cacheDir,
29+
)
30+
31+
->withPhpSets()
32+
->withAttributesSets()
33+
34+
->withSets([
35+
SetList::CODE_QUALITY,
36+
SetList::CODING_STYLE,
37+
SetList::DEAD_CODE,
38+
SetList::EARLY_RETURN,
39+
SetList::INSTANCEOF,
40+
SetList::TYPE_DECLARATION,
41+
])
42+
43+
->withSkip([
44+
ClassPropertyAssignToConstructorPromotionRector::class,
45+
CatchExceptionNameMatchingTypeRector::class,
46+
ClosureToArrowFunctionRector::class,
47+
RemoveUselessReturnTagRector::class,
48+
CompactToVariablesRector::class,
49+
ReturnTypeFromStrictFluentReturnRector::class,
50+
SplitDoubleAssignRector::class,
51+
NewlineAfterStatementRector::class,
52+
ExplicitBoolCompareRector::class,
53+
TypedPropertyFromCreateMockAssignRector::class,
54+
]);

src/BaseMigration.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,18 @@ class BaseMigration implements MigrationInterface
3030
{
3131
/**
3232
* The Adapter instance
33-
*
34-
* @var \Migrations\Db\Adapter\AdapterInterface
3533
*/
3634
protected ?AdapterInterface $adapter = null;
3735

3836
/**
3937
* The ConsoleIo instance
40-
*
41-
* @var \Cake\Console\ConsoleIo
4238
*/
4339
protected ?ConsoleIo $io = null;
4440

4541
/**
4642
* The config instance.
47-
*
48-
* @var \Migrations\Config\ConfigInterface
4943
*/
50-
protected ?ConfigInterface $config;
44+
protected ?ConfigInterface $config = null;
5145

5246
/**
5347
* List of all the table objects created by this migration
@@ -58,15 +52,11 @@ class BaseMigration implements MigrationInterface
5852

5953
/**
6054
* Is migrating up prop
61-
*
62-
* @var bool
6355
*/
6456
protected bool $isMigratingUp = true;
6557

6658
/**
6759
* The version number.
68-
*
69-
* @var int
7060
*/
7161
protected int $version;
7262

@@ -77,8 +67,6 @@ class BaseMigration implements MigrationInterface
7767
* This option is global for all tables created in the migration file.
7868
* If you set it to false, you have to manually add the primary keys for your
7969
* tables using the Migrations\Table::addPrimaryKey() method
80-
*
81-
* @var bool
8270
*/
8371
public bool $autoId = true;
8472

@@ -110,7 +98,7 @@ public function setAdapter(AdapterInterface $adapter)
11098
*/
11199
public function getAdapter(): AdapterInterface
112100
{
113-
if (!$this->adapter) {
101+
if (!$this->adapter instanceof AdapterInterface) {
114102
throw new RuntimeException('Adapter not set.');
115103
}
116104

@@ -458,18 +446,13 @@ public function index(string|array $columns): Index
458446
*/
459447
public function preFlightCheck(): void
460448
{
461-
if (method_exists($this, MigrationInterface::CHANGE)) {
462-
if (
463-
method_exists($this, MigrationInterface::UP) ||
464-
method_exists($this, MigrationInterface::DOWN)
465-
) {
466-
$io = $this->getIo();
467-
if ($io) {
468-
$io->out(
469-
'<comment>warning</comment> Migration contains both change() and up()/down() methods.' .
470-
' <warning>Ignoring up() and down()</warning>.',
471-
);
472-
}
449+
if (method_exists($this, MigrationInterface::CHANGE) && (method_exists($this, MigrationInterface::UP) || method_exists($this, MigrationInterface::DOWN))) {
450+
$io = $this->getIo();
451+
if ($io instanceof ConsoleIo) {
452+
$io->out(
453+
'<comment>warning</comment> Migration contains both change() and up()/down() methods.' .
454+
' <warning>Ignoring up() and down()</warning>.',
455+
);
473456
}
474457
}
475458
}

src/BaseSeed.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,18 @@ class BaseSeed implements SeedInterface
2525
{
2626
/**
2727
* The Adapter instance
28-
*
29-
* @var \Migrations\Db\Adapter\AdapterInterface
3028
*/
3129
protected ?AdapterInterface $adapter = null;
3230

3331
/**
3432
* The ConsoleIo instance
35-
*
36-
* @var \Cake\Console\ConsoleIo
3733
*/
3834
protected ?ConsoleIo $io = null;
3935

4036
/**
4137
* The config instance.
42-
*
43-
* @var \Migrations\Config\ConfigInterface
44-
*/
45-
protected ?ConfigInterface $config;
46-
47-
/**
48-
* No-op constructor.
4938
*/
50-
public function __construct()
51-
{
52-
}
39+
protected ?ConfigInterface $config = null;
5340

5441
/**
5542
* {@inheritDoc}
@@ -81,7 +68,7 @@ public function setAdapter(AdapterInterface $adapter)
8168
*/
8269
public function getAdapter(): AdapterInterface
8370
{
84-
if (!$this->adapter) {
71+
if (!$this->adapter instanceof AdapterInterface) {
8572
throw new RuntimeException('Adapter not set.');
8673
}
8774

@@ -130,10 +117,8 @@ public function setConfig(ConfigInterface $config)
130117
public function getName(): string
131118
{
132119
$name = static::class;
133-
if (str_starts_with($name, 'Migrations\BaseSeed@anonymous')) {
134-
if (preg_match('#[/\\\\]([a-zA-Z0-9_]+)\.php:#', $name, $matches)) {
135-
$name = $matches[1];
136-
}
120+
if (str_starts_with($name, 'Migrations\BaseSeed@anonymous') && preg_match('#[/\\\\](\w+)\.php:#', $name, $matches)) {
121+
return $matches[1];
137122
}
138123

139124
return $name;
@@ -238,7 +223,7 @@ public function isIdempotent(): bool
238223
public function call(string $seeder, array $options = []): void
239224
{
240225
$io = $this->getIo();
241-
if ($io === null) {
226+
if (!$io instanceof ConsoleIo) {
242227
throw new RuntimeException('ConsoleIo is required for calling other seeders.');
243228
}
244229
$io->out('');
@@ -287,7 +272,7 @@ protected function runCall(string $seeder, array $options = []): void
287272
'source' => $options['source'],
288273
]);
289274
$io = $this->getIo();
290-
if ($io === null) {
275+
if (!$io instanceof ConsoleIo) {
291276
throw new RuntimeException('ConsoleIo is required for calling other seeders.');
292277
}
293278
$manager = $factory->createManager($io);

src/Command/BakeMigrationCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
*/
3030
class BakeMigrationCommand extends BakeSimpleMigrationCommand
3131
{
32-
/**
33-
* @var string
34-
*/
3532
protected string $_name;
3633

3734
/**
@@ -45,7 +42,7 @@ public static function defaultName(): string
4542
/**
4643
* @inheritDoc
4744
*/
48-
public function bake(string $name, Arguments $args, ConsoleIo $io): void
45+
protected function bake(string $name, Arguments $args, ConsoleIo $io): void
4946
{
5047
EventManager::instance()->on('Bake.initialize', function (Event $event): void {
5148
/** @var \Bake\View\BakeView $view */

0 commit comments

Comments
 (0)