savinmikhail / add_named_arguments_rector
Rector rule to add names to arguments for functions'/methods' calls
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:rector-extension
Requires
- php: >=8.2
- nikic/php-parser: ^4.15 || ^5.1
- phpstan/phpstan: ^2.1.1
- rector/rector: ^2.0.0
- symplify/rule-doc-generator-contracts: ^11.2
Requires (Dev)
- ergebnis/composer-normalize: ^2.45
- friendsofphp/php-cs-fixer: ^3.62
- icanhazstring/composer-unused: ^0.8.11
- maglnet/composer-require-checker: ^4.14
- phpunit/phpunit: ^10.5
- phpyh/coding-standard: ^2.6.1
README
AddNamedArgumentsRector
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.
Example
- str_contains('foo', 'bar'); + str_contains(haystack: 'foo', needle: 'bar');
This feature works for:
- Functions
- Static methods
- Instance methods
- Constructors
Installation
You can install the package via Composer:
composer require --dev savinmikhail/add_named_arguments_rector
Usage
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); };
Customization
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] ); };
Implementing Your Own Strategy
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] ); };
Tests as Documentation
The package includes tests that demonstrate how the rule behaves in various scenarios. Feel free to explore the tests for examples and usage details.
Related Discussion
This rule was developed as a standalone feature following this discussion.
Contributing
Contributions, feedback, and suggestions are welcome! Feel free to open issues or submit pull requests.