Skip to content
Open
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
138 changes: 51 additions & 87 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,19 @@
* 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;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
Expand All @@ -48,55 +36,36 @@
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
$rectorConfig = RectorConfig::configure()
->withSets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_82,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->parallel();

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

PHPUnitSetList::COMPOSER_BASED,
])
->withParallel()
// The paths to refactor (can also be supplied with CLI arguments)
$rectorConfig->paths([
->withPaths([
__DIR__ . '/src/',
__DIR__ . '/tests/',
]);

])
// Include Composer's autoload - required for global execution, remove if running locally
$rectorConfig->autoloadPaths([
->withAutoloadPaths([
__DIR__ . '/vendor/autoload.php',
]);

])
// Do you need to include constants, class aliases, or a custom autoloader?
$rectorConfig->bootstrapFiles([
->withBootstrapFiles([
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
]);

if (is_file(__DIR__ . '/phpstan.neon.dist')) {
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
}

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

->withPhpVersion(PhpVersion::PHP_82)
// Auto-import fully qualified class names
$rectorConfig->importNames();

->withImportNames(removeUnusedImports: false)
// Are there files or rules you need to skip?
$rectorConfig->skip([
->withSkip([
__DIR__ . '/app/Views',

StringifyStrNeedlesRector::class,
Expand All @@ -123,48 +92,43 @@

// Temporary fix
DeclareStrictTypesTestsRector::class,
])
->withRules([
RemoveAlwaysElseRector::class,
ChangeIfElseValueAssignToEarlyReturnRector::class,
SimplifyIfReturnBoolRector::class,
InlineIfToExplicitIfRector::class,
PreparedValueToEarlyReturnRector::class,
ChangeArrayPushToArrayAssignRector::class,
UnnecessaryTernaryExpressionRector::class,
FuncGetArgsToVariadicParamRector::class,
MakeInheritedMethodVisibilitySameAsParentRector::class,
SimplifyEmptyArrayCheckRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
TernaryEmptyArrayArrayDimFetchToCoalesceRector::class,
PrivatizeFinalClassPropertyRector::class,
SingleInArrayToCompareRector::class,
VersionCompareFuncCallToConstantRector::class,
])
->withConfiguredRule(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,
]);

// auto import fully qualified class names
$rectorConfig->importNames();
// Github action cache
if (is_dir('/tmp')) {
$rectorConfig->withCache('/tmp/rector');
}

if (is_file(__DIR__ . '/phpstan.neon.dist')) {
$rectorConfig->withPHPStanConfigs([
__DIR__ . '/phpstan.neon.dist',
]);
}

$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveAlwaysElseRector::class);
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
$rectorConfig->rule(SimplifyStrposLowerRector::class);
$rectorConfig->rule(CombineIfRector::class);
$rectorConfig->rule(SimplifyIfReturnBoolRector::class);
$rectorConfig->rule(InlineIfToExplicitIfRector::class);
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
$rectorConfig->rule(ShortenElseIfRector::class);
$rectorConfig->rule(SimplifyIfElseToTernaryRector::class);
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
$rectorConfig->rule(SimplifyRegexPatternRector::class);
$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(SingleInArrayToCompareRector::class);
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
$rectorConfig->rule(ExplicitBoolCompareRector::class);
};
return $rectorConfig;
10 changes: 6 additions & 4 deletions src/RunResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
continue 2;
}
}

return $next;
}

return $next;
Expand All @@ -119,14 +121,14 @@
* 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 124 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),
{
return match ($position) {

Check warning on line 126 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),
'minute' => $next->addMinutes(1),
'hour' => $next->addHours(1),
'monthDay', 'weekDay' => $next->addDays(1),
'month' => $next->addMonths(1),
default => $next,
'month' => $next->addMonths(1),
default => $next,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@

return $this->{$method}();
} finally {
if ($this->singleInstance && $this->getType() !== 'queue') {
if ($this->singleInstance && isset($lockKey) && $this->getType() !== 'queue') {

Check warning on line 147 in src/Task.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": @@ @@ return $this->{$method}(); } finally { - if ($this->singleInstance && isset($lockKey) && $this->getType() !== 'queue') { + if (($this->singleInstance || isset($lockKey)) && $this->getType() !== 'queue') { cache()->delete($lockKey); } }
cache()->delete($lockKey);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@

$this->cliWrite('Processing: ' . ($task->name ?: 'Task'), 'green');

try {

Check warning on line 69 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "UnwrapFinally": @@ @@ $output = null; $this->cliWrite('Processing: ' . ($task->name ?: 'Task'), 'green'); - try { $output = $task->run(); - $this->cliWrite('Executed: ' . ($task->name ?: 'Task'), 'cyan'); } catch (Throwable $e) { $this->cliWrite('Failed: ' . ($task->name ?: 'Task'), 'red'); @@ @@ log_message('error', $e->getMessage(), ['exception' => $e]); $error = $e; - } finally { - // Save performance info - $taskLog = new TaskLog([ - 'task' => $task, - 'output' => $output, - 'runStart' => $start, - 'runEnd' => Time::now(), - 'error' => $error, - ]); - - $this->updateLogs($taskLog); } + // Save performance info + $taskLog = new TaskLog([ + 'task' => $task, + 'output' => $output, + 'runStart' => $start, + 'runEnd' => Time::now(), + 'error' => $error, + ]); + $this->updateLogs($taskLog); } }
$output = $task->run();

$this->cliWrite('Executed: ' . ($task->name ?: 'Task'), 'cyan');
} catch (Throwable $e) {
$this->cliWrite('Failed: ' . ($task->name ?: 'Task'), 'red');

log_message('error', $e->getMessage(), $e->getTrace());
log_message('error', $e->getMessage(), ['exception' => $e]);

Check warning on line 76 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "FunctionCallRemoval": @@ @@ } catch (Throwable $e) { $this->cliWrite('Failed: ' . ($task->name ?: 'Task'), 'red'); - log_message('error', $e->getMessage(), ['exception' => $e]); + $error = $e; } finally { // Save performance info

Check warning on line 76 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } catch (Throwable $e) { $this->cliWrite('Failed: ' . ($task->name ?: 'Task'), 'red'); - log_message('error', $e->getMessage(), ['exception' => $e]); + log_message('error', $e->getMessage(), []); $error = $e; } finally { // Save performance info
$error = $e;
} finally {
// Save performance info
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/RunResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\I18n\Time;
use CodeIgniter\Tasks\RunResolver;
use CodeIgniter\Test\CIUnitTestCase as TestCase;

/**
* @internal
*/
final class RunResolverTest extends TestCase
{
public function testStopsAfterAllFieldsMatch()
{
$next = $this->createMock(Time::class);
$next->expects($this->once())->method('getMinute')->willReturn('4');
$next->expects($this->once())->method('getHour')->willReturn('10');
$next->expects($this->once())->method('getDay')->willReturn('13');
$next->expects($this->once())->method('getMonth')->willReturn('11');
$next->expects($this->once())->method('getDayOfWeek')->willReturn('5');

$this->assertSame($next, (new RunResolver())->nextRun('4 10 13 11 5', $next));
}

public function testStopsAfterFindingMatchInLaterIteration()
{
$match = $this->createMock(Time::class);
$match->expects($this->once())->method('getMinute')->willReturn('4');

$next = $this->createMock(Time::class);
$next->expects($this->once())->method('getMinute')->willReturn('3');
$next->expects($this->once())->method('addMinutes')->with(1)->willReturn($match);

$this->assertSame($match, (new RunResolver())->nextRun('4 * * * *', $next));
}

public function testStopsAtIterationLimitWhenNoFieldsMatch()
{
$next = $this->createMock(Time::class);
$next->expects($this->exactly(1000))->method('getMinute')->willReturn('3');
$next->expects($this->exactly(1000))->method('addMinutes')->with(1)->willReturnSelf();

$this->assertSame($next, (new RunResolver())->nextRun('4 * * * *', $next));
}
}
Loading