Skip to main contentNedix.io

Refactoring

These refactoring tools can significantly speed up the periodical upgrade process of a PHP application.

Phpactor

Installation

curl -sSL https://github.com/phpactor/phpactor/releases/latest/download/phpactor.phar -o phpactor.phar \
&& chmod +x phpactor.phar

Configuration

phpactor.yml

Run fixes

./phpactor.phar class:transform --transform=add_missing_docblocks_return "src/**/*.php"

PHP CS Fixer

Installation

composer require --dev friendsofphp/php-cs-fixer kubawerlos/php-cs-fixer-custom-fixers

Configuration

.php-cs-fixer.php
<?php
$config = new PhpCsFixer\Config();
$config->setRules([
'@PSR2' => true,
'@PSR12' => true,
]);

Run fixers

vendor/bin/php-cs-fixer fix src

Rector

Installation

composer require --dev rector/rector

Configuration

rector.php
<?php
use Rector\Config\RectorConfig;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromDoctrineCollectionRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToManyRelationTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector;
use Rector\Set\ValueObject\SetList;

Run fixers

vendor/bin/rector process src

PHP_CodeSniffer

Installation

composer require --dev squizlabs/php_codesniffer slevomat/coding-standard

Configuration

phpcs.xml
<?xml version="1.0"?>
<ruleset>
<autoload>./vendor/autoload.php</autoload>
<config name="installed_paths" value="../../slevomat/coding-standard"/>
<rule ref="PSR2"/>
<rule ref="PSR12"/>
</ruleset>

Run fixers

vendor/bin/phpcbf src

Symplify

Symplify supports rules and rulesets from several refactoring tools, such as PHP_CodeSniffer and PHP CS Fixer.

Installation

composer require --dev symplify/coding-standard symplify/easy-coding-standard

Configuration

ecs.php
<?php
use PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer;
use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

Run fixers

vendor/bin/ecs check src --fix