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 support for Rector #237

Open
1 task done
claytoncollie opened this issue Sep 26, 2024 · 3 comments
Open
1 task done

Add support for Rector #237

claytoncollie opened this issue Sep 26, 2024 · 3 comments

Comments

@claytoncollie
Copy link
Contributor

Is your enhancement related to a problem? Please describe.

Similar to #201 but this ticket is specific to adding support for Rector.

This might even be an open feature branch similar to #133

Designs

No response

Describe alternatives you've considered

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@tobeycodes
Copy link
Member

I did a upgrade to php8.3 last week and this is the config I used. The rules that I disabled were good rules but they changed a lot of the codebase for things that were using new PHP features not fixing deprecated/breaking changes. I'd love to see all these rules enabled though by default in wp-scaffold

<?php
/**
 * Rector configuration.
 *
 * @package XXX
 */

declare(strict_types=1);

use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\Config\RectorConfig;
use Rector\Php53\Rector\Ternary\TernaryToElvisRector;
use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Php80\Rector\Identical\StrEndsWithRector;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\Php80\Rector\NotIdentical\StrContainsRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
	->withPaths(
		[
			__DIR__ . '/client-mu-plugins/xxx',
			__DIR__ . '/client-mu-plugins/xxx',
			__DIR__ . '/client-mu-plugins/xxx',
			__DIR__ . '/client-mu-plugins/xx',
			__DIR__ . '/themes/xxx',
		]
	)
	->withSkip(
		[
			__DIR__ . '/**/node_modules/**',
			__DIR__ . '/**/vendor/**',
			OptionalParametersAfterRequiredRector::class,
			LongArrayToShortArrayRector::class,
			TernaryToNullCoalescingRector::class,
			ClosureToArrowFunctionRector::class,
			StrEndsWithRector::class,
			ChangeSwitchToMatchRector::class,
			NullToStrictStringFuncCallArgRector::class,
			FirstClassCallableRector::class,
			AddOverrideAttributeToOverriddenMethodsRector::class,
			StringClassNameToClassConstantRector::class,
			TernaryToElvisRector::class,
			RemoveExtraParametersRector::class,
			ThisCallOnStaticMethodToStaticCallRector::class,
			ClassPropertyAssignToConstructorPromotionRector::class,
			IfIssetToCoalescingRector::class,
			ReadOnlyPropertyRector::class,
			StrContainsRector::class,
			AddClosureVoidReturnTypeWhereNoReturnRector::class,
			MixedTypeRector::class,
			StrStartsWithRector::class,
			StringifyStrNeedlesRector::class,
		]
	)
	->withPhpSets( php83: true )
	->withTypeCoverageLevel( 0 );

@claytoncollie
Copy link
Contributor Author

claytoncollie commented Dec 6, 2024

@tobeycodes Here is the config I am using for new sites.

<?php
/**
 * Rector Rules
 *
 * @see https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
 */

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\Set\ValueObject\SetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
use Rector\Php53\Rector\Ternary\TernaryToElvisRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;

return static function ( RectorConfig $rectorConfig ): void {
	$rectorConfig->paths(
		[
			__DIR__ . '/themes/theme-name',
			__DIR__ . '/mu-plugins/plugin-name'
		]
	);

	$rectorConfig->skip(
		[
			__DIR__ . '/**/dist/**',
			__DIR__ . '/**/node_modules/**',
			__DIR__ . '/**/vendor/**',
			DisallowedEmptyRuleFixerRector::class,
			SimplifyEmptyArrayCheckRector::class,
			SimplifyEmptyCheckOnEmptyArrayRector::class,
			NewlineAfterStatementRector::class,
			RenameParamToMatchTypeRector::class,
			TernaryToElvisRector::class,
			RemoveExtraParametersRector::class,
			ClosureToArrowFunctionRector::class,
			FirstClassCallableRector::class,
			RemoveUnusedNonEmptyArrayBeforeForeachRector::class,
			RemoveUselessReturnTagRector::class,
			RenamePropertyToMatchTypeRector::class,
			ExplicitBoolCompareRector::class,
			RemoveUselessVarTagRector::class,
			RenameVariableToMatchMethodCallReturnTypeRector::class,
		]
	);

	$rectorConfig->autoloadPaths(
		[
			__DIR__ . '/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php',
		]
	);

	$rectorConfig->phpstanConfigs(
		[
			__DIR__ . '/phpstan.neon',
		]
	);

	$rectorConfig->sets(
		[
			SetList::STRICT_BOOLEANS,
			SetList::GMAGICK_TO_IMAGICK,
			SetList::NAMING,
			SetList::PRIVATIZATION,
			SetList::TYPE_DECLARATION,
			SetList::EARLY_RETURN,
			SetList::INSTANCEOF,
			SetList::DEAD_CODE,
			SetList::CODE_QUALITY,
			SetList::CODING_STYLE,
			SetList::PHP_82,
			LevelSetList::UP_TO_PHP_82,
		]
	);

	$rectorConfig->phpVersion( PhpVersion::PHP_82 );
};

@tobeycodes
Copy link
Member

@claytoncollie Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants