Skip to content

Commit 2f5550d

Browse files
committed
bug #4220 NativeFunctionInvocationFixer - namespaced strict to remove backslash (kubawerlos)
This PR was merged into the 2.14-dev branch. Discussion ---------- NativeFunctionInvocationFixer - namespaced strict to remove backslash When we want only namespaced calls to have `\` in strict mode we should remove `\` for global namespace call. Commits ------- 1241bda NativeFunctionInvocationFixer - namespaced strict to remove backslash
2 parents 882013e + 1241bda commit 2f5550d

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function isRisky()
182182
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
183183
{
184184
if ('all' === $this->configuration['scope']) {
185-
$this->fixFunctionCalls($tokens, $this->functionFilter, 0, \count($tokens) - 1);
185+
$this->fixFunctionCalls($tokens, $this->functionFilter, 0, \count($tokens) - 1, false);
186186

187187
return;
188188
}
@@ -192,11 +192,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
192192
// 'scope' is 'namespaced' here
193193
/** @var NamespaceAnalysis $namespace */
194194
foreach (array_reverse($namespaces) as $namespace) {
195-
if ('' === $namespace->getFullName()) {
196-
continue;
197-
}
198-
199-
$this->fixFunctionCalls($tokens, $this->functionFilter, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex());
195+
$this->fixFunctionCalls($tokens, $this->functionFilter, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex(), '' === $namespace->getFullName());
200196
}
201197
}
202198

@@ -264,8 +260,9 @@ protected function createConfigurationDefinition()
264260
* @param callable $functionFilter
265261
* @param int $start
266262
* @param int $end
263+
* @param bool $tryToRemove
267264
*/
268-
private function fixFunctionCalls(Tokens $tokens, callable $functionFilter, $start, $end)
265+
private function fixFunctionCalls(Tokens $tokens, callable $functionFilter, $start, $end, $tryToRemove)
269266
{
270267
$functionsAnalyzer = new FunctionsAnalyzer();
271268

@@ -277,8 +274,11 @@ private function fixFunctionCalls(Tokens $tokens, callable $functionFilter, $sta
277274

278275
$prevIndex = $tokens->getPrevMeaningfulToken($index);
279276

280-
if (!$functionFilter($tokens[$index]->getContent())) {
281-
if ($this->configuration['strict'] && $tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
277+
if (!$functionFilter($tokens[$index]->getContent()) || $tryToRemove) {
278+
if (!$this->configuration['strict']) {
279+
continue;
280+
}
281+
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
282282
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
283283
}
284284

tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,20 @@ public function & strlen($name) {
579579
'strict' => true,
580580
],
581581
],
582+
'scope namespaced and strict enabled' => [
583+
'<?php
584+
$a = not_compiler_optimized_function();
585+
$b = intval($c);
586+
',
587+
'<?php
588+
$a = \not_compiler_optimized_function();
589+
$b = \intval($c);
590+
',
591+
[
592+
'scope' => 'namespaced',
593+
'strict' => true,
594+
],
595+
],
582596
];
583597
}
584598

0 commit comments

Comments
 (0)