Skip to content

chore: rector for PHP 8.1 #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ parameters:
count: 1
path: src/Commands/Lister.php

-
message: '#^Call to an undefined method CodeIgniter\\I18n\\Time\:\:getMonthDay\(\)\.$#'
identifier: method.notFound
count: 1
path: src/RunResolver.php

-
message: '#^Call to an undefined method CodeIgniter\\I18n\\Time\:\:getWeekDay\(\)\.$#'
identifier: method.notFound
count: 1
path: src/RunResolver.php

-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''CodeIgniter\\\\I18n\\\\Time'' and CodeIgniter\\I18n\\Time will always evaluate to true\.$#'
identifier: method.alreadyNarrowedType
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
cacheDirectory="build/psalm/"
findUnusedBaselineEntry="false"
findUnusedCode="false"
ensureOverrideAttribute="false"
>
<projectFiles>
<directory name="src/" />
Expand Down
72 changes: 69 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@
* the LICENSE file that was distributed with this source code.
*/

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
Expand All @@ -34,14 +41,34 @@
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SetList::DEAD_CODE, LevelSetList::UP_TO_PHP_74, PHPUnitSetList::PHPUNIT_80]);
$rectorConfig->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_81,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->parallel();

// Github action cache
$rectorConfig->cacheClass(FileCacheStorage::class);
if (is_dir('/tmp')) {
$rectorConfig->cacheDirectory('/tmp/rector');
}

// The paths to refactor (can also be supplied with CLI arguments)
$rectorConfig->paths([
__DIR__ . '/src/',
Expand All @@ -63,23 +90,41 @@
}

// Set the target version for refactoring
$rectorConfig->phpVersion(PhpVersion::PHP_74);
$rectorConfig->phpVersion(PhpVersion::PHP_81);

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

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

StringifyStrNeedlesRector::class,
YieldDataProviderRector::class,

// Note: requires php 8
RemoveUnusedPromotedPropertyRector::class,
AnnotationWithValueToAttributeRector::class,

// May load view files directly when detecting classes
StringClassNameToClassConstantRector::class,

// Because of the BaseCommand
TypedPropertyFromAssignsRector::class => [
__DIR__ . '/src/Commands/Disable.php',
__DIR__ . '/src/Commands/Enable.php',
__DIR__ . '/src/Commands/Lister.php',
__DIR__ . '/src/Commands/Publish.php',
__DIR__ . '/src/Commands/Run.php',
__DIR__ . '/src/Commands/TaskCommand.php',
__DIR__ . '/tests/_support/Commands/TasksExample.php',
__DIR__ . '/tests/unit/TaskRunnerTest.php',
],
]);

// auto import fully qualified class names
$rectorConfig->importNames();

$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveAlwaysElseRector::class);
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
Expand All @@ -99,4 +144,25 @@
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
$rectorConfig
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
/**
* The INLINE_PUBLIC value is default to false to avoid BC break,
* if you use for libraries and want to preserve BC break, you don't
* need to configure it, as it included in LevelSetList::UP_TO_PHP_74
* Set to true for projects that allow BC break
*/
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
]);
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
$rectorConfig->rule(SingleInArrayToCompareRector::class);
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
$rectorConfig->rule(ExplicitBoolCompareRector::class);
};
14 changes: 7 additions & 7 deletions src/FrequenciesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{
$min = $hour = 0;

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

Expand Down Expand Up @@ -323,7 +323,7 @@
{
$min = $hour = 0;

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

Expand Down Expand Up @@ -403,7 +403,7 @@
{
$min = $hour = 0;

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

Expand All @@ -425,7 +425,7 @@
{
$min = $hour = 0;

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

Expand All @@ -446,7 +446,7 @@
{
$min = $hour = 0;

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

Expand All @@ -466,7 +466,7 @@
{
$min = $hour = 0;

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

Expand All @@ -482,11 +482,11 @@
*
* @return $this
*/
protected function setDayOfWeek(int $day, ?string $time = null)

Check warning on line 485 in src/FrequenciesTrait.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * * @return $this */ - protected function setDayOfWeek(int $day, ?string $time = null) + private function setDayOfWeek(int $day, ?string $time = null) { $min = $hour = '*'; if ($time !== null && $time !== '' && $time !== '0') {
{
$min = $hour = '*';

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

Expand Down
27 changes: 7 additions & 20 deletions src/RunResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,15 @@
* definitely make it smarter in the future to cut down on the
* amount of iterations needed.
*/
protected function increment(Time $next, string $position): Time

Check warning on line 122 in src/RunResolver.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * definitely make it smarter in the future to cut down on the * amount of iterations needed. */ - protected function increment(Time $next, string $position): Time + private function increment(Time $next, string $position): Time { return match ($position) { 'minute' => $next->addMinutes(1),
{
switch ($position) {
case 'minute':
$next = $next->addMinutes(1);
break;

case 'hour':
$next = $next->addHours(1);
break;

case 'monthDay':
case 'weekDay':
$next = $next->addDays(1);
break;

case 'month':
$next = $next->addMonths(1);
break;
}

return $next;
return match ($position) {

Check warning on line 124 in src/RunResolver.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "MatchArmRemoval": @@ @@ 'hour' => $next->addHours(1), 'monthDay', 'weekDay' => $next->addDays(1), 'month' => $next->addMonths(1), - default => $next, }; } /**
'minute' => $next->addMinutes(1),
'hour' => $next->addHours(1),
'monthDay', 'weekDay' => $next->addDays(1),
'month' => $next->addMonths(1),
default => $next,
};
}

/**
Expand Down
20 changes: 6 additions & 14 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@
*/
protected string $type;

/**
* The actual content that should be run.
*
* @var mixed
*/
protected $action;

/**
* If not empty, lists the allowed environments
* this can run in.
Expand All @@ -74,18 +67,17 @@
protected string $name;

/**
* @param mixed $action
* @param $action mixed The actual content that should be run.
*
* @throws TasksException
*/
public function __construct(string $type, $action)
public function __construct(string $type, protected mixed $action)
{
if (! in_array($type, $this->types, true)) {
throw TasksException::forInvalidTaskType($type);
}

$this->type = $type;
$this->action = $action;
$this->type = $type;
}

/**
Expand Down Expand Up @@ -144,12 +136,12 @@
$cron = service('cronExpression');

// Allow times to be set during testing
if (! empty($testTime)) {
if ($testTime !== null && $testTime !== '' && $testTime !== '0') {

Check warning on line 139 in src/Task.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": @@ @@ { $cron = service('cronExpression'); // Allow times to be set during testing - if ($testTime !== null && $testTime !== '' && $testTime !== '0') { + if ($testTime !== null && $testTime !== '' || $testTime !== '0') { $cron->testTime($testTime); } // Are we restricting to environments?

Check warning on line 139 in src/Task.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": @@ @@ { $cron = service('cronExpression'); // Allow times to be set during testing - if ($testTime !== null && $testTime !== '' && $testTime !== '0') { + if (($testTime !== null || $testTime !== '') && $testTime !== '0') { $cron->testTime($testTime); } // Are we restricting to environments?
$cron->testTime($testTime);
}

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

Expand Down Expand Up @@ -198,10 +190,10 @@
/**
* Checks if it runs within the specified environment.
*/
protected function runsInEnvironment(string $environment): bool

Check warning on line 193 in src/Task.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ /** * Checks if it runs within the specified environment. */ - protected function runsInEnvironment(string $environment): bool + private function runsInEnvironment(string $environment): bool { // If nothing is specified then it should run if ($this->environments === []) {
{
// If nothing is specified then it should run
if (empty($this->environments)) {
if ($this->environments === []) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@

foreach ($tasks as $task) {
// If specific tasks were chosen then skip executing remaining tasks
if (! empty($this->only) && ! in_array($task->name, $this->only, true)) {
if ($this->only !== [] && ! in_array($task->name, $this->only, true)) {

Check warning on line 55 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ } foreach ($tasks as $task) { // If specific tasks were chosen then skip executing remaining tasks - if ($this->only !== [] && !in_array($task->name, $this->only, true)) { + if (!($this->only !== []) && in_array($task->name, $this->only, true)) { continue; } if (!$task->shouldRun($this->testTime) && $this->only === []) {

Check warning on line 55 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "LogicalNot": @@ @@ } foreach ($tasks as $task) { // If specific tasks were chosen then skip executing remaining tasks - if ($this->only !== [] && !in_array($task->name, $this->only, true)) { + if ($this->only !== [] && in_array($task->name, $this->only, true)) { continue; } if (!$task->shouldRun($this->testTime) && $this->only === []) {
continue;
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/mock/MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
final class MockTest extends TasksTestCase
{
protected MockScheduler $scheduler;
private MockScheduler $scheduler;

protected function setUp(): void
{
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/CronExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class CronExpressionTest extends TestCase
{
protected CronExpression $cron;
private CronExpression $cron;

protected function setUp(): void
{
Expand Down Expand Up @@ -207,7 +207,11 @@ public static function provideEveryHour(): iterable
$h . ':10 PM',
], range(1, 12));

return [...$hours24, ...$hoursAM, ...$hoursPM];
return [
...$hours24,
...$hoursAM,
...$hoursPM,
];
}

public static function provideNextRun(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/FrequenciesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
final class FrequenciesTraitTest extends TestCase
{
protected object $class;
private object $class;

protected function setUp(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
final class SchedulerTest extends TestCase
{
protected Scheduler $scheduler;
private Scheduler $scheduler;

protected function setUp(): void
{
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ final class TaskTest extends TasksTestCase

protected $namespace;

/**
* @var bool|resource
*/
protected $streamFilter;

protected function setUp(): void
{
parent::setUp();
Expand Down