`native_function_invocation`: support first-class callable syntax
Feature request
Follow-up to #9480 (fixed in #9484) and my comment there.
In that comment I reported that global_namespace_import does not import an unqualified first-class callable like array_values(...). After digging further, global_namespace_import behaves correctly: it doesn't import unqualified regular calls either. The real gap is in native_function_invocation. It adds the leading \ to regular calls but skips first-class callables, so global_namespace_import never gets a fully qualified reference to import.
Current behavior (v3.95.27, PHP 8.5.2):
php-cs-fixer fix --diff --dry-run --allow-risky=yes --rules='{"native_function_invocation": {"include": ["@all"]}}' test.phpRegular call, gets qualified as expected:
<?php
namespace App;
$values = array_values([]);-$values = array_values([]);
+$values = \array_values([]);First-class callable, no changes:
<?php
namespace App;
$fn = array_values(...);
$result = [] |> array_unique(...) |> array_values(...);Desired behavior:
<?php
namespace App;
$fn = \array_values(...);
$result = [] |> \array_unique(...) |> \array_values(...);Combined with global_namespace_import:
With {"native_function_invocation": {"include": ["@all"]}, "global_namespace_import": {"import_functions": true}}, the regular call ends up imported:
namespace App;
+use function array_values;
$values = array_values([]);The first-class callable version stays untouched. With the fix above, both forms would behave the same.
Since PHP 8.5's pipe operator, first-class callables are much more common, so the gap shows up often in modern code.
Link to Discussion where feature request was formed
https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/9480#issuecomment-4238412150
Contribution Checks
- I have verified if this feature request was already discussed
- I am familiar with "Feature or bug?"
Source: PHP-CS-Fixer/PHP-CS-Fixer