Bug: Wrong transformation of `use function` keywords
Closed this issue · 2 comments
akadusei commented
Detailed description
Imposter wrongly prefixes use function
(and possibly use const
) keywords.
For instance:
use function Stringy\create as s;
becomes:
use My\App\Vendor\function Stringy\create as s;
instead of:
use function My\App\Vendor\Stringy\create as s;
Possible implementation
This should probably be refactored to take into account use function
and use const
?:
Try:
First, prefix use function
and use const
statements. Then, do a negative lookahead for both in a separate prefixing?
Something along these lines:
// Prefix 'use function'
$pattern = sprintf(
'/%1$s\\s+(?!(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'use\\s+function',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use function', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
// Prefix for 'use const'
// Similar to above
// Finally
$pattern = sprintf(
'/(?<!function.+)%1$s\\s+(?!(function|const)\\s+|(%2$s)|(\\\\(?!.*\\\\.*))|(Composer(\\\\|;)|(?!.*\\\\.*)))/',
'use',
$this->namespacePrefix
);
$replacement = sprintf('%1$s %2$s', 'use', $this->namespacePrefix);
$this->replace($pattern, $replacement, $targetFile);
Your environment
- Imposter plugin version: 0.3.0
- PHP version: 7.0.30
- Operating system: Ubuntu 16.04
- WordPress version: 4.9.8
tangrufus commented
Thanks for reporting!
I will patch the regex in the weekend.
For long term solution, we need something better than regex.