Skip to content

Commit

Permalink
feat: added rules for 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Apr 5, 2024
1 parent 9020216 commit 3cb145e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ See available [Codeigniter4 rules](/docs/rector_rules_overview.md)

## Requirement

This package initiated, CodeIgniter for `4.5` already prepare rector with minimum PHP 8.1. And we follow minimum PHP 8.1 for the future.
Minimum PHP 8.1

## Install

Expand Down
94 changes: 94 additions & 0 deletions config/sets/codeigniter45.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

use PHPDevsr\Rector\Codeigniter4\Utils\PassStrictParameterToFunctionParameterRector;
use PHPDevsr\Rector\Codeigniter4\Utils\RemoveErrorSuppressInTryCatchStmtsRector;
use PHPDevsr\Rector\Codeigniter4\Utils\UnderscoreToCamelCaseVariableNameRector;
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_\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\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
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\StmtsAwareInterface\DeclareStrictTypesRector;

// see: https://laravel.com/docs/5.0/upgrade
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');

$rectorConfig->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_81,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->rules([
DeclareStrictTypesRector::class,
UnderscoreToCamelCaseVariableNameRector::class,
SimplifyUselessVariableRector::class,
RemoveAlwaysElseRector::class,
PassStrictParameterToFunctionParameterRector::class,
CountArrayToEmptyArrayComparisonRector::class,
ChangeNestedForeachIfsToEarlyContinueRector::class,
ChangeIfElseValueAssignToEarlyReturnRector::class,
SimplifyStrposLowerRector::class,
CombineIfRector::class,
SimplifyIfReturnBoolRector::class,
InlineIfToExplicitIfRector::class,
PreparedValueToEarlyReturnRector::class,
ShortenElseIfRector::class,
SimplifyIfElseToTernaryRector::class,
UnusedForeachValueToArrayKeysRector::class,
ChangeArrayPushToArrayAssignRector::class,
UnnecessaryTernaryExpressionRector::class,
RemoveErrorSuppressInTryCatchStmtsRector::class,
SimplifyRegexPatternRector::class,
FuncGetArgsToVariadicParamRector::class,
MakeInheritedMethodVisibilitySameAsParentRector::class,
SimplifyEmptyArrayCheckRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
TernaryEmptyArrayArrayDimFetchToCoalesceRector::class,
EmptyOnNullableObjectToInstanceOfRector::class,
DisallowedEmptyRuleFixerRector::class,
PrivatizeFinalClassPropertyRector::class,
CompleteDynamicPropertiesRector::class,
BooleanInIfConditionRuleFixerRector::class,
SingleInArrayToCompareRector::class,
VersionCompareFuncCallToConstantRector::class,
]);

$rectorConfig
->ruleWithConfiguration(StringClassNameToClassConstantRector::class, [
// keep '\\' prefix string on string '\Foo\Bar'
StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true,
]);
};
10 changes: 10 additions & 0 deletions config/sets/level/up-to-codeigniter-45.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use PHPDevsr\Rector\Codeigniter4\Set\CodeigniterSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([CodeigniterSetList::CODEIGNITER_45]);
};
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
return RectorConfig::configure()
->withImportNames(removeUnusedImports:true)
->withSets([
CodeigniterSetList::CODEIGNITER_44,
CodeigniterSetList::CODEIGNITER_45,
])
->withPaths([
__DIR__ . '/src',
Expand Down
5 changes: 5 additions & 0 deletions src/Set/CodeigniterLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ final class CodeigniterLevelSetList
* @var string
*/
public const UP_TO_CODEIGNITER_44 = __DIR__ . '/../../config/sets/level/up-to-codeigniter-44.php';

/**
* @var string
*/
public const UP_TO_CODEIGNITER_45 = __DIR__ . '/../../config/sets/level/up-to-codeigniter-45.php';
}
5 changes: 5 additions & 0 deletions src/Set/CodeigniterSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ final class CodeigniterSetList
* @var string
*/
public const CODEIGNITER_44 = __DIR__ . '/../../config/sets/codeigniter44.php';

/**
* @var string
*/
public const CODEIGNITER_45 = __DIR__ . '/../../config/sets/codeigniter45.php';
}
11 changes: 2 additions & 9 deletions src/Utils/UnderscoreToCamelCaseVariableNameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
* @see https://regex101.com/r/OtFn8I/1
*/
private const PARAM_NAME_REGEX = '#(?<paramPrefix>@param\s.*\s+\$)(?<paramName>%s)#ms';

private ReservedKeywordAnalyzer $reservedKeywordAnalyzer;
private PhpDocInfoFactory $phpDocInfoFactory;
private bool $hasChanged = false;

public function __construct(
ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
PhpDocInfoFactory $phpDocInfoFactory
) {
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
$this->phpDocInfoFactory = $phpDocInfoFactory;
public function __construct(private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer, private readonly PhpDocInfoFactory $phpDocInfoFactory)
{
}

public function getRuleDefinition(): RuleDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PHPDevsr\Rector\Codeigniter4\Tests\Utils\UnderscoreToCamelCaseVariableNameRector;

use PHPUnit\Framework\Attributes\Test;
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
Expand All @@ -15,10 +16,8 @@ public static function provideData(): Iterator
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
#[Test]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
Expand Down

0 comments on commit 3cb145e

Please sign in to comment.