Skip to content

Commit

Permalink
[DX] Make use of configure() method (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 30, 2021
1 parent d1fd984 commit 58c8a93
Show file tree
Hide file tree
Showing 132 changed files with 2,168 additions and 2,584 deletions.
4 changes: 2 additions & 2 deletions build/config/config-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

$services = $containerConfigurator->services();
$services->set(DowngradeParameterTypeWideningRector::class)
->call('configure', [[
->configure([
DowngradeParameterTypeWideningRector::SAFE_TYPES => [
// phsptan
Type::class,
Expand Down Expand Up @@ -83,7 +83,7 @@
'hasParameter',
],
],
]]);
]);
};

/**
Expand Down
47 changes: 15 additions & 32 deletions build/target-repository/docs/beyond_php_file_processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ Let´s say you want to define a custom configuration where you want to update th
All you have to do is using the ChangePackageVersionComposerRector:

```php
<?php
// rector.php
declare(strict_types=1);

use Rector\Composer\Rector\ChangePackageVersionComposerRector;
use Rector\Composer\ValueObject\PackageAndVersion;
Expand All @@ -29,21 +27,16 @@ use Symplify\SymfonyPhpConfig\ValueObjectInliner;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangePackageVersionComposerRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
ChangePackageVersionComposerRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
new PackageAndVersion('symfony/yaml', '^5.0'),
]),
]]);
->configure([
new PackageAndVersion('symfony/yaml', '^5.0'),
]);
};
```

There are some more rules related to manipulate your composer.json files. Let´s see them in action:

```php
<?php
// rector.php
declare(strict_types=1);

use Rector\Composer\Rector\AddPackageToRequireComposerRector;
use Rector\Composer\Rector\AddPackageToRequireDevComposerRector;
Expand All @@ -59,37 +52,27 @@ return static function (ContainerConfigurator $containerConfigurator): void {

// Add a package to the require section of your composer.json
$services->set(AddPackageToRequireComposerRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
AddPackageToRequireComposerRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
new PackageAndVersion('symfony/yaml', '^5.0'),
]),
]]);
->configure([
new PackageAndVersion('symfony/yaml', '^5.0'),
]);

// Add a package to the require dev section of your composer.json
$services->set(AddPackageToRequireDevComposerRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
AddPackageToRequireDevComposerRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
new PackageAndVersion('phpunit/phpunit', '^9.0'),
]),
]]);
->configure([
new PackageAndVersion('phpunit/phpunit', '^9.0'),
]);

// Remove a package from composer.json
$services->set(RemovePackageComposerRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
RemovePackageComposerRector::PACKAGE_NAMES => ['symfony/console']
]]);
->configure([
'symfony/console'
]);

// Replace a package in the composer.json
$services->set(ReplacePackageAndVersionComposerRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
ReplacePackageAndVersionComposerRector::REPLACE_PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
new ReplacePackageAndVersion('vendor1/package2', 'vendor2/package1', '^3.0'),
]),
]]);
->configure([
new ReplacePackageAndVersion('vendor1/package2', 'vendor2/package1', '^3.0'),
]);
};
```

Expand Down
9 changes: 3 additions & 6 deletions build/target-repository/docs/how_to_configure_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenameClassRector::class)
->call('configure', [[
// we use constant for keys to save you from typos
RenameClassRector::OLD_TO_NEW_CLASSES => [
'App\SomeOldClass' => 'App\SomeNewClass',
],
]]);
->configure([
'App\SomeOldClass' => 'App\SomeNewClass',
]);
};
```
8 changes: 3 additions & 5 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => ValueObjectInliner::inline([
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, new ObjectType('SomeType')),
]),
]]);
->configure([
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, new ObjectType('SomeType')),
]);
};
```

Expand Down
52 changes: 25 additions & 27 deletions config/set/code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,31 @@
$services->set(ArrayThisCallToThisMethodCallRector::class);
$services->set(CommonNotEqualRector::class);
$services->set(RenameFunctionRector::class)
->call('configure', [[
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',
],
]]);
->configure([
'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(SetTypeToCastRector::class);
$services->set(LogicalToBooleanRector::class);
$services->set(VarToPublicPropertyRector::class);
Expand Down
10 changes: 4 additions & 6 deletions config/set/coding-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@
$services->set(UseMessageVariableForSprintfInSymfonyStyleRector::class);

$services->set(FuncCallToConstFetchRector::class)
->call('configure', [[
FuncCallToConstFetchRector::FUNCTIONS_TO_CONSTANTS => [
'php_sapi_name' => 'PHP_SAPI',
'pi' => 'M_PI',
],
]]);
->configure([
'php_sapi_name' => 'PHP_SAPI',
'pi' => 'M_PI',
]);

$services->set(SeparateMultiUseImportsRector::class);
$services->set(RemoveDoubleUnderscoreInMethodNameRector::class);
Expand Down
25 changes: 10 additions & 15 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,27 @@
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);

$services = $containerConfigurator->services();
$services->set(RemoveInterfacesRector::class)
->call('configure', [[
RemoveInterfacesRector::INTERFACES_TO_REMOVE => [
// @see https://wiki.php.net/rfc/stringable
'Stringable',
],
]]);
->configure([
// @see https://wiki.php.net/rfc/stringable
'Stringable',
]);

$services->set(DowngradeNamedArgumentRector::class);

$services->set(DowngradeAttributeToAnnotationRector::class)
->call('configure', [[
DowngradeAttributeToAnnotationRector::ATTRIBUTE_TO_ANNOTATION => ValueObjectInliner::inline([
// Symfony
new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'),
// Nette
new DowngradeAttributeToAnnotation('Nette\DI\Attributes\Inject', 'inject'),
]),
]]);
->configure([
// Symfony
new DowngradeAttributeToAnnotation('Symfony\Contracts\Service\Attribute\Required', 'required'),
// Nette
new DowngradeAttributeToAnnotation('Nette\DI\Attributes\Inject', 'inject'),
]);

$services->set(DowngradeUnionTypeTypedPropertyRector::class);
$services->set(DowngradeUnionTypeDeclarationRector::class);
Expand Down
37 changes: 17 additions & 20 deletions config/set/flysystem-20.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenameMethodRector::class)
->call('configure', [[
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
// Rename is now move, specific for files.
new MethodCallRename('League\Flysystem\FilesystemInterface', 'rename', 'move'),
->configure([
// Rename is now move, specific for files.
new MethodCallRename('League\Flysystem\FilesystemInterface', 'rename', 'move'),

// No arbitrary abbreviations
new MethodCallRename('League\Flysystem\FilesystemInterface', 'createDir', 'createDirectory'),
// No arbitrary abbreviations
new MethodCallRename('League\Flysystem\FilesystemInterface', 'createDir', 'createDirectory'),

// Writes are now deterministic
new MethodCallRename('League\Flysystem\FilesystemInterface', 'update', 'write'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'updateStream', 'writeStream'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'put', 'write'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'putStream', 'writeStream'),
// Writes are now deterministic
new MethodCallRename('League\Flysystem\FilesystemInterface', 'update', 'write'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'updateStream', 'writeStream'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'put', 'write'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'putStream', 'writeStream'),

// Metadata getters are renamed
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getTimestamp', 'lastModified'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'has', 'fileExists'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getMimetype', 'mimeType'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getSize', 'fileSize'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getVisibility', 'visibility'),
]),
]]);
// Metadata getters are renamed
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getTimestamp', 'lastModified'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'has', 'fileExists'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getMimetype', 'mimeType'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getSize', 'fileSize'),
new MethodCallRename('League\Flysystem\FilesystemInterface', 'getVisibility', 'visibility'),
]);
};
Loading

0 comments on commit 58c8a93

Please sign in to comment.