The AddNamedArgumentsRector rule enhances your code by converting function, method, or constructor calls to use named arguments where possible. Named arguments improve readability and reduce errors by explicitly naming the parameters being passed.
- str_contains('foo', 'bar');
+ str_contains(haystack: 'foo', needle: 'bar');
This feature works for:
- Functions
- Static methods
- Instance methods
- Constructors
You can install the package via Composer:
composer require --dev savinmikhail/add_named_arguments_rector
To enable the rule, add it to your Rector configuration (rector.php
):
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use SavinMikhail\AddNamedArgumentsRector\AddNamedArgumentsRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddNamedArgumentsRector::class);
};
By default, the rule applies named arguments universally where possible. However, if you want more control over when the rule applies, you can use a strategy:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use SavinMikhail\AddNamedArgumentsRector\AddNamedArgumentsRector;
use SavinMikhail\AddNamedArgumentsRector\Config\PhpyhStrategy;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(
AddNamedArgumentsRector::class,
[PhpyhStrategy::class]
);
};
See PhpyhStrategy
as example, you can create your own strategy by implementing the ConfigStrategy
interface. For example:
<?php
declare(strict_types=1);
namespace YourNamespace;
use PhpParser\Node;
use SavinMikhail\AddNamedArgumentsRector\Config\ConfigStrategy;
class CustomStrategy implements ConfigStrategy
{
public static function shouldApply(Node $node, array $parameters): bool
{
// Add your custom logic here
return true;
}
}
Then, configure it in your rector.php
:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use SavinMikhail\AddNamedArgumentsRector\AddNamedArgumentsRector;
use YourNamespace\CustomStrategy;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(
AddNamedArgumentsRector::class,
[CustomStrategy::class]
);
};
The package includes tests that demonstrate how the rule behaves in various scenarios. Feel free to explore the tests for examples and usage details.
This rule was developed as a standalone feature following this discussion.
Contributions, feedback, and suggestions are welcome! Feel free to open issues or submit pull requests.