Skip to content

Commit 7ddc5ac

Browse files
authored
NoImportFromGlobalNamespaceFixer - fix all places when there is a lot of changes (#954)
1 parent a996334 commit 7ddc5ac

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3526-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3527-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)

src/Fixer/NoImportFromGlobalNamespaceFixer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function fixImports(Tokens $tokens, int $startIndex, int $endIndex, bool
8585
continue;
8686
}
8787

88-
$this->updateUsage($tokens, $imports, $index);
88+
$endIndex += $this->updateUsageAndReturnNumberOfInsertedTokens($tokens, $imports, $index);
8989
}
9090
}
9191

@@ -135,19 +135,21 @@ private function updateComment(Tokens $tokens, array $imports, int $index): void
135135
/**
136136
* @param list<string> $imports
137137
*/
138-
private function updateUsage(Tokens $tokens, array $imports, int $index): void
138+
private function updateUsageAndReturnNumberOfInsertedTokens(Tokens $tokens, array $imports, int $index): int
139139
{
140140
if (!\in_array($tokens[$index]->getContent(), $imports, true)) {
141-
return;
141+
return 0;
142142
}
143143

144144
$prevIndex = $tokens->getPrevMeaningfulToken($index);
145145
\assert(\is_int($prevIndex));
146146

147147
if ($tokens[$prevIndex]->isGivenKind([\T_CONST, \T_DOUBLE_COLON, \T_NS_SEPARATOR, \T_OBJECT_OPERATOR, \T_FUNCTION])) {
148-
return;
148+
return 0;
149149
}
150150

151151
$tokens->insertAt($index, new Token([\T_NS_SEPARATOR, '\\']));
152+
153+
return 1;
152154
}
153155
}

tests/Fixer/NoImportFromGlobalNamespaceFixerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ function Bar() {}
305305
namespace N6; use DateTime; use stdClass; new DateTime(); new stdClass();
306306
',
307307
];
308+
309+
$expected = "<?php\nnamespace Foo;\n";
310+
$input = "<?php\nnamespace Foo;\n use Bar;\n";
311+
for ($i = 1; $i <= 256; $i++) {
312+
$expected .= \sprintf("echo \\Bar::BAZ_%d;\n", $i);
313+
$input .= \sprintf("echo Bar::BAZ_%d;\n", $i);
314+
}
315+
yield [$expected, $input];
308316
}
309317

310318
/**

0 commit comments

Comments
 (0)