Skip to content

Commit

Permalink
NoImportFromGlobalNamespaceFixer - fix all places when there is a l…
Browse files Browse the repository at this point in the history
…ot of changes (#954)
  • Loading branch information
kubawerlos authored Jan 23, 2024
1 parent a996334 commit 7ddc5ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![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)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3526-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3527-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![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)
Expand Down
10 changes: 6 additions & 4 deletions src/Fixer/NoImportFromGlobalNamespaceFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function fixImports(Tokens $tokens, int $startIndex, int $endIndex, bool
continue;
}

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

Expand Down Expand Up @@ -135,19 +135,21 @@ private function updateComment(Tokens $tokens, array $imports, int $index): void
/**
* @param list<string> $imports
*/
private function updateUsage(Tokens $tokens, array $imports, int $index): void
private function updateUsageAndReturnNumberOfInsertedTokens(Tokens $tokens, array $imports, int $index): int
{
if (!\in_array($tokens[$index]->getContent(), $imports, true)) {
return;
return 0;
}

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

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

$tokens->insertAt($index, new Token([\T_NS_SEPARATOR, '\\']));

return 1;
}
}
8 changes: 8 additions & 0 deletions tests/Fixer/NoImportFromGlobalNamespaceFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ function Bar() {}
namespace N6; use DateTime; use stdClass; new DateTime(); new stdClass();
',
];

$expected = "<?php\nnamespace Foo;\n";
$input = "<?php\nnamespace Foo;\n use Bar;\n";
for ($i = 1; $i <= 256; $i++) {
$expected .= \sprintf("echo \\Bar::BAZ_%d;\n", $i);
$input .= \sprintf("echo Bar::BAZ_%d;\n", $i);
}
yield [$expected, $input];
}

/**
Expand Down

0 comments on commit 7ddc5ac

Please sign in to comment.