Skip to content

Commit 9916802

Browse files
authored
chore: rector for PHP 8.1 (#184)
* chore: rector php 8.1 * update phpstan baseline
1 parent 2cb4066 commit 9916802

12 files changed

+113
-56
lines changed

phpstan-baseline.neon

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ parameters:
1212
count: 1
1313
path: src/Commands/Lister.php
1414

15+
-
16+
message: '#^Call to an undefined method CodeIgniter\\I18n\\Time\:\:getMonthDay\(\)\.$#'
17+
identifier: method.notFound
18+
count: 1
19+
path: src/RunResolver.php
20+
21+
-
22+
message: '#^Call to an undefined method CodeIgniter\\I18n\\Time\:\:getWeekDay\(\)\.$#'
23+
identifier: method.notFound
24+
count: 1
25+
path: src/RunResolver.php
26+
1527
-
1628
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''CodeIgniter\\\\I18n\\\\Time'' and CodeIgniter\\I18n\\Time will always evaluate to true\.$#'
1729
identifier: method.alreadyNarrowedType

psalm.xml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
cacheDirectory="build/psalm/"
1111
findUnusedBaselineEntry="false"
1212
findUnusedCode="false"
13+
ensureOverrideAttribute="false"
1314
>
1415
<projectFiles>
1516
<directory name="src/" />

rector.php

+69-3
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
1415
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
16+
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
17+
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
1518
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
1619
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
1720
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
1821
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
1922
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
23+
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
2024
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
2125
use Rector\CodeQuality\Rector\If_\CombineIfRector;
26+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2227
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
2328
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
2429
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
30+
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
2531
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
2632
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
2733
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
2834
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
35+
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
2936
use Rector\Config\RectorConfig;
3037
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
3138
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
@@ -34,14 +41,34 @@
3441
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
3542
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
3643
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
44+
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
45+
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
3746
use Rector\PHPUnit\Set\PHPUnitSetList;
47+
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
3848
use Rector\Set\ValueObject\LevelSetList;
3949
use Rector\Set\ValueObject\SetList;
50+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
51+
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
52+
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
53+
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
4054
use Rector\ValueObject\PhpVersion;
4155

4256
return static function (RectorConfig $rectorConfig): void {
43-
$rectorConfig->sets([SetList::DEAD_CODE, LevelSetList::UP_TO_PHP_74, PHPUnitSetList::PHPUNIT_80]);
57+
$rectorConfig->sets([
58+
SetList::DEAD_CODE,
59+
LevelSetList::UP_TO_PHP_81,
60+
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
61+
PHPUnitSetList::PHPUNIT_100,
62+
]);
63+
4464
$rectorConfig->parallel();
65+
66+
// Github action cache
67+
$rectorConfig->cacheClass(FileCacheStorage::class);
68+
if (is_dir('/tmp')) {
69+
$rectorConfig->cacheDirectory('/tmp/rector');
70+
}
71+
4572
// The paths to refactor (can also be supplied with CLI arguments)
4673
$rectorConfig->paths([
4774
__DIR__ . '/src/',
@@ -63,23 +90,41 @@
6390
}
6491

6592
// Set the target version for refactoring
66-
$rectorConfig->phpVersion(PhpVersion::PHP_74);
93+
$rectorConfig->phpVersion(PhpVersion::PHP_81);
6794

6895
// Auto-import fully qualified class names
6996
$rectorConfig->importNames();
7097

7198
// Are there files or rules you need to skip?
7299
$rectorConfig->skip([
73-
__DIR__ . '/src/Views',
100+
__DIR__ . '/app/Views',
74101

75102
StringifyStrNeedlesRector::class,
103+
YieldDataProviderRector::class,
76104

77105
// Note: requires php 8
78106
RemoveUnusedPromotedPropertyRector::class,
107+
AnnotationWithValueToAttributeRector::class,
79108

80109
// May load view files directly when detecting classes
81110
StringClassNameToClassConstantRector::class,
111+
112+
// Because of the BaseCommand
113+
TypedPropertyFromAssignsRector::class => [
114+
__DIR__ . '/src/Commands/Disable.php',
115+
__DIR__ . '/src/Commands/Enable.php',
116+
__DIR__ . '/src/Commands/Lister.php',
117+
__DIR__ . '/src/Commands/Publish.php',
118+
__DIR__ . '/src/Commands/Run.php',
119+
__DIR__ . '/src/Commands/TaskCommand.php',
120+
__DIR__ . '/tests/_support/Commands/TasksExample.php',
121+
__DIR__ . '/tests/unit/TaskRunnerTest.php',
122+
],
82123
]);
124+
125+
// auto import fully qualified class names
126+
$rectorConfig->importNames();
127+
83128
$rectorConfig->rule(SimplifyUselessVariableRector::class);
84129
$rectorConfig->rule(RemoveAlwaysElseRector::class);
85130
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
@@ -99,4 +144,25 @@
99144
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
100145
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
101146
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
147+
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
148+
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
149+
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
150+
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
151+
$rectorConfig
152+
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
153+
/**
154+
* The INLINE_PUBLIC value is default to false to avoid BC break,
155+
* if you use for libraries and want to preserve BC break, you don't
156+
* need to configure it, as it included in LevelSetList::UP_TO_PHP_74
157+
* Set to true for projects that allow BC break
158+
*/
159+
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
160+
]);
161+
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
162+
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
163+
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
164+
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
165+
$rectorConfig->rule(SingleInArrayToCompareRector::class);
166+
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
167+
$rectorConfig->rule(ExplicitBoolCompareRector::class);
102168
};

src/FrequenciesTrait.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function daily(?string $time = null)
7171
{
7272
$min = $hour = 0;
7373

74-
if (! empty($time)) {
74+
if ($time !== null && $time !== '' && $time !== '0') {
7575
[$min, $hour] = $this->parseTime($time);
7676
}
7777

@@ -323,7 +323,7 @@ public function monthly(?string $time = null)
323323
{
324324
$min = $hour = 0;
325325

326-
if (! empty($time)) {
326+
if ($time !== null && $time !== '' && $time !== '0') {
327327
[$min, $hour] = $this->parseTime($time);
328328
}
329329

@@ -403,7 +403,7 @@ public function quarterly(?string $time = null)
403403
{
404404
$min = $hour = 0;
405405

406-
if (! empty($time)) {
406+
if ($time !== null && $time !== '' && $time !== '0') {
407407
[$min, $hour] = $this->parseTime($time);
408408
}
409409

@@ -425,7 +425,7 @@ public function yearly(?string $time = null)
425425
{
426426
$min = $hour = 0;
427427

428-
if (! empty($time)) {
428+
if ($time !== null && $time !== '' && $time !== '0') {
429429
[$min, $hour] = $this->parseTime($time);
430430
}
431431

@@ -446,7 +446,7 @@ public function weekdays(?string $time = null)
446446
{
447447
$min = $hour = 0;
448448

449-
if (! empty($time)) {
449+
if ($time !== null && $time !== '' && $time !== '0') {
450450
[$min, $hour] = $this->parseTime($time);
451451
}
452452

@@ -466,7 +466,7 @@ public function weekends(?string $time = null)
466466
{
467467
$min = $hour = 0;
468468

469-
if (! empty($time)) {
469+
if ($time !== null && $time !== '' && $time !== '0') {
470470
[$min, $hour] = $this->parseTime($time);
471471
}
472472

@@ -486,7 +486,7 @@ protected function setDayOfWeek(int $day, ?string $time = null)
486486
{
487487
$min = $hour = '*';
488488

489-
if (! empty($time)) {
489+
if ($time !== null && $time !== '' && $time !== '0') {
490490
[$min, $hour] = $this->parseTime($time);
491491
}
492492

src/RunResolver.php

+7-20
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,13 @@ public function nextRun(string $expression, Time $next): Time
121121
*/
122122
protected function increment(Time $next, string $position): Time
123123
{
124-
switch ($position) {
125-
case 'minute':
126-
$next = $next->addMinutes(1);
127-
break;
128-
129-
case 'hour':
130-
$next = $next->addHours(1);
131-
break;
132-
133-
case 'monthDay':
134-
case 'weekDay':
135-
$next = $next->addDays(1);
136-
break;
137-
138-
case 'month':
139-
$next = $next->addMonths(1);
140-
break;
141-
}
142-
143-
return $next;
124+
return match ($position) {
125+
'minute' => $next->addMinutes(1),
126+
'hour' => $next->addHours(1),
127+
'monthDay', 'weekDay' => $next->addDays(1),
128+
'month' => $next->addMonths(1),
129+
default => $next,
130+
};
144131
}
145132

146133
/**

src/Task.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ class Task
5555
*/
5656
protected string $type;
5757

58-
/**
59-
* The actual content that should be run.
60-
*
61-
* @var mixed
62-
*/
63-
protected $action;
64-
6558
/**
6659
* If not empty, lists the allowed environments
6760
* this can run in.
@@ -74,18 +67,17 @@ class Task
7467
protected string $name;
7568

7669
/**
77-
* @param mixed $action
70+
* @param $action mixed The actual content that should be run.
7871
*
7972
* @throws TasksException
8073
*/
81-
public function __construct(string $type, $action)
74+
public function __construct(string $type, protected mixed $action)
8275
{
8376
if (! in_array($type, $this->types, true)) {
8477
throw TasksException::forInvalidTaskType($type);
8578
}
8679

87-
$this->type = $type;
88-
$this->action = $action;
80+
$this->type = $type;
8981
}
9082

9183
/**
@@ -144,12 +136,12 @@ public function shouldRun(?string $testTime = null): bool
144136
$cron = service('cronExpression');
145137

146138
// Allow times to be set during testing
147-
if (! empty($testTime)) {
139+
if ($testTime !== null && $testTime !== '' && $testTime !== '0') {
148140
$cron->testTime($testTime);
149141
}
150142

151143
// Are we restricting to environments?
152-
if (! empty($this->environments) && ! $this->runsInEnvironment($_SERVER['CI_ENVIRONMENT'])) {
144+
if ($this->environments !== [] && ! $this->runsInEnvironment($_SERVER['CI_ENVIRONMENT'])) {
153145
return false;
154146
}
155147

@@ -201,7 +193,7 @@ public function lastRun()
201193
protected function runsInEnvironment(string $environment): bool
202194
{
203195
// If nothing is specified then it should run
204-
if (empty($this->environments)) {
196+
if ($this->environments === []) {
205197
return true;
206198
}
207199

src/TaskRunner.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public function run()
5252

5353
foreach ($tasks as $task) {
5454
// If specific tasks were chosen then skip executing remaining tasks
55-
if (! empty($this->only) && ! in_array($task->name, $this->only, true)) {
55+
if ($this->only !== [] && ! in_array($task->name, $this->only, true)) {
5656
continue;
5757
}
5858

59-
if (! $task->shouldRun($this->testTime) && empty($this->only)) {
59+
if (! $task->shouldRun($this->testTime) && $this->only === []) {
6060
continue;
6161
}
6262

tests/mock/MockTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
final class MockTest extends TasksTestCase
2222
{
23-
protected MockScheduler $scheduler;
23+
private MockScheduler $scheduler;
2424

2525
protected function setUp(): void
2626
{

tests/unit/CronExpressionTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
final class CronExpressionTest extends TestCase
2424
{
25-
protected CronExpression $cron;
25+
private CronExpression $cron;
2626

2727
protected function setUp(): void
2828
{
@@ -207,7 +207,11 @@ public static function provideEveryHour(): iterable
207207
$h . ':10 PM',
208208
], range(1, 12));
209209

210-
return [...$hours24, ...$hoursAM, ...$hoursPM];
210+
return [
211+
...$hours24,
212+
...$hoursAM,
213+
...$hoursPM,
214+
];
211215
}
212216

213217
public static function provideNextRun(): iterable

tests/unit/FrequenciesTraitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
final class FrequenciesTraitTest extends TestCase
2121
{
22-
protected object $class;
22+
private object $class;
2323

2424
protected function setUp(): void
2525
{

tests/unit/SchedulerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
final class SchedulerTest extends TestCase
2222
{
23-
protected Scheduler $scheduler;
23+
private Scheduler $scheduler;
2424

2525
protected function setUp(): void
2626
{

tests/unit/TaskTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ final class TaskTest extends TasksTestCase
2626

2727
protected $namespace;
2828

29-
/**
30-
* @var bool|resource
31-
*/
32-
protected $streamFilter;
33-
3429
protected function setUp(): void
3530
{
3631
parent::setUp();

0 commit comments

Comments
 (0)