Skip to content
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

add rector/rector #254

Merged
merged 3 commits into from
Nov 28, 2021
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ phpcs.xml export-ignore text eol=lf
phpmd.ruleset.xml export-ignore text eol=lf
phpstan.neon export-ignore text eol=lf
phpunit.xml export-ignore text eol=lf
rector.php export-ignore text eol=lf
/tests export-ignore
/.github export-ignore
80 changes: 80 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -78,6 +81,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -130,6 +136,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -182,6 +191,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -232,6 +244,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -284,6 +299,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -311,6 +329,62 @@ jobs:
- name: "Run phpmd/phpmd"
run: "phpmd src github phpmd.ruleset.xml"

rector:
name: "Checks with Rector"

needs: "validate"

runs-on: "${{ matrix.operating-system }}"

continue-on-error: false

strategy:
fail-fast: false

matrix:
operating-system:
- "ubuntu-18.04"
- "ubuntu-20.04"

php-version:
- "7.4"

dependencies:
- ""

steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
ini-values: "${{ env.PHP_INI_VALUES }}"
coverage: "none"
tools: "composer:v2"

- name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""

- name: "Cache dependencies installed with composer"
uses: "actions/cache@v2.1.6"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

- name: "Update dependencies with composer"
run: "composer update --optimize-autoloader --prefer-dist --prefer-stable --no-progress --no-interaction -v --ansi ${{ matrix.dependencies }}"

- name: "Run checks with rector/rector"
run: "vendor/bin/rector process src tests --ansi --dry-run --no-diffs"

tests:
name: "UnitTests with PHPUnit"

Expand Down Expand Up @@ -338,6 +412,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down Expand Up @@ -393,6 +470,9 @@ jobs:
steps:
- name: "Checkout"
uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0

- name: "Install PHP"
uses: "shivammathur/setup-php@2.15.0"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:

steps:
- uses: "actions/checkout@v2.4.0"
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- uses: "micnncim/action-label-syncer@v1.3.0"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"phpstan/phpstan": "^1.2.0",
"phpstan/phpstan-deprecation-rules": "^1.0.0",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpunit/phpunit": "^9.5.10"
"phpunit/phpunit": "^9.5.10",
"rector/rector": "^0.12.5"
},
"config": {
"preferred-install": "dist",
Expand Down
190 changes: 190 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\ValueObject\PhpVersion;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// Define what rule sets will be applied
$services = $containerConfigurator->services();

// Dead Code
$services->set(\Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfFunctionExistsRector::class);
$services->set(\Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector::class);
$services->set(\Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector::class);
$services->set(\Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector::class);
$services->set(\Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector::class);
$services->set(\Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector::class);
$services->set(\Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector::class);
$services->set(\Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector::class);
$services->set(\Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector::class);
$services->set(\Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector::class);
$services->set(\Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector::class);
$services->set(\Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector::class);
$services->set(\Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector::class);
$services->set(\Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector::class);
$services->set(\Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector::class);
$services->set(\Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector::class);
$services->set(\Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector::class);
$services->set(\Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector::class);
$services->set(\Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector::class);
//$services->set(\Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector::class);
$services->set(\Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector::class);
$services->set(\Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector::class);
$services->set(\Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector::class);
$services->set(\Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector::class);
$services->set(\Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector::class);
$services->set(\Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector::class);
$services->set(\Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector::class);
$services->set(\Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector::class);
$services->set(\Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector::class);
$services->set(\Rector\DeadCode\Rector\Return_\RemoveDeadConditionAboveReturnRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector::class);
$services->set(\Rector\DeadCode\Rector\For_\RemoveDeadLoopRector::class);
// docblock
$services->set(\Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector::class);

// requires PHP 8+
//$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector::class);
$services->set(\Rector\DeadCode\Rector\ClassMethod\RemoveLastReturnRector::class);

// PHP 7.4
$services->set(\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::class)->call('configure', [[\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
#the_real_type
# https://wiki.php.net/rfc/deprecations_php_7_4
'is_real' => 'is_float',
#apache_request_headers_function
# https://wiki.php.net/rfc/deprecations_php_7_4
'apache_request_headers' => 'getallheaders',
]]]);
$services->set(\Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector::class);
$services->set(\Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector::class);
$services->set(\Rector\Php74\Rector\Double\RealToFloatTypeCastRector::class);
$services->set(\Rector\Php74\Rector\Assign\NullCoalescingOperatorRector::class);
$services->set(\Rector\Php74\Rector\Function_\ReservedFnFunctionRector::class);
$services->set(\Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector::class);
$services->set(\Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector::class);
$services->set(\Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector::class);
$services->set(\Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector::class);
$services->set(\Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector::class);
$services->set(\Rector\Php74\Rector\ArrayDimFetch\CurlyToSquareBracketArrayStringRector::class);

// CODE QUALITY
$services->set(\Rector\CodeQuality\Rector\Assign\CombinedAssignRector::class);
$services->set(\Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\GetClassToInstanceOfRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\SimplifyConditionsRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector::class);
$services->set(\Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector::class);
$services->set(\Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector::class);
$services->set(\Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector::class);
$services->set(\Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector::class);
$services->set(\Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToArrayFilterRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector::class);
$services->set(\Rector\CodeQuality\Rector\Concat\JoinStringConcatRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\CombineIfRector::class);
$services->set(\Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector::class);
$services->set(\Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector::class);
$services->set(\Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector::class);
$services->set(\Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector::class);
$services->set(\Rector\CodeQuality\Rector\For_\ForToForeachRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector::class);
$services->set(\Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector::class);
$services->set(\Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector::class);
$services->set(\Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\ShortenElseIfRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector::class);
$services->set(\Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector::class);
$services->set(\Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector::class);
$services->set(\Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector::class);
$services->set(\Rector\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector::class);
$services->set(\Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector::class);
$services->set(\Rector\CodeQuality\Rector\Array_\ArrayThisCallToThisMethodCallRector::class);
$services->set(\Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector::class);
$services->set(\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::class)->call('configure', [[\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
'split' => 'explode',
'join' => 'implode',
'sizeof' => 'count',
# https://www.php.net/manual/en/aliases.php
'chop' => 'rtrim',
'doubleval' => 'floatval',
'gzputs' => 'gzwrites',
'fputs' => 'fwrite',
'ini_alter' => 'ini_set',
'is_double' => 'is_float',
'is_integer' => 'is_int',
'is_long' => 'is_int',
'is_real' => 'is_float',
'is_writeable' => 'is_writable',
'key_exists' => 'array_key_exists',
'pos' => 'current',
'strchr' => 'strstr',
# mb
'mbstrcut' => 'mb_strcut',
'mbstrlen' => 'mb_strlen',
'mbstrpos' => 'mb_strpos',
'mbstrrpos' => 'mb_strrpos',
'mbsubstr' => 'mb_substr',
]]]);
$services->set(\Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector::class);
$services->set(\Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector::class);
$services->set(\Rector\Php52\Rector\Property\VarToPublicPropertyRector::class);
$services->set(\Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector::class);
$services->set(\Rector\CodeQuality\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector::class);
$services->set(\Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector::class);
$services->set(\Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector::class);
$services->set(\Rector\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector::class);
$services->set(\Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector::class);
$services->set(\Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector::class);
$services->set(\Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector::class);
$services->set(\Rector\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector::class);
$services->set(\Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector::class);
$services->set(\Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector::class);
$services->set(\Rector\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector::class);

// PRIVATIZATION
$services->set(\Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector::class);
$services->set(\Rector\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector::class);

$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);

$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, getcwd() . '/phpstan.neon');
};