Skip to content

Commit 5fde865

Browse files
authored
Merge pull request #7 from adamwojs/issue_3
Added support for non-namespaced files
2 parents 5223e17 + 3c3e9ac commit 5fde865

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

src/Analyzer/NamespaceAnalyzer.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public function getNamespaces(): array
2727

2828
$imports = $this->getImportsPerNamespace();
2929

30+
if (empty($imports)) {
31+
// Global namespace without imports
32+
return [
33+
new NamespaceInfo(
34+
"",
35+
new Range(0, $this->tokens->count()),
36+
[]
37+
)
38+
];
39+
}
40+
3041
$this->tokens->rewind();
3142
foreach ($this->tokens as $index => $token) {
3243
if (!$token->isGivenKind(T_NAMESPACE)) {
@@ -42,9 +53,14 @@ public function getNamespaces(): array
4253
));
4354

4455
$scope = $this->getNamespaceScope($declarationEndIndex);
45-
$namespaceImports = array_filter($imports, function (ImportInfo $info) use ($scope) {
46-
return $scope->inRange($info->getDeclaration()->getStartIndex());
47-
});
56+
57+
$namespaceImports = [];
58+
foreach ($imports as $shortName => $import) {
59+
if ($scope->inRange($import->getDeclaration()->getStartIndex())) {
60+
$namespaceImports[$shortName] = $import;
61+
unset($imports[$shortName]);
62+
}
63+
}
4864

4965
$namespaces[] = new NamespaceInfo(
5066
$namespaceName,
@@ -53,6 +69,14 @@ public function getNamespaces(): array
5369
);
5470
}
5571

72+
if (!empty($imports)) {
73+
$namespaces[] = new NamespaceInfo(
74+
"",
75+
$this->getNamespaceScope(reset($imports)->getDeclaration()->getStartIndex()),
76+
$imports
77+
);
78+
}
79+
5680
return $namespaces;
5781
}
5882

src/Analyzer/Range.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function inRange(int $index): bool
5656

5757
return true;
5858
}
59+
60+
public function __toString()
61+
{
62+
return sprintf("[%s,%s]", $this->startIndex, $this->endIndex ?? 'INF');
63+
}
5964
}

src/Fixer/Phpdoc/ForceFQCNFixer.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,20 @@ public function fix(SplFileInfo $file, Tokens $tokens): void
5959

6060
$tokens->rewind();
6161

62-
$currentNamespace = null;
6362
foreach ($tokens as $index => $token) {
64-
if ($token->isGivenKind(T_NAMESPACE)) {
65-
$namespaceFQCN = $this->getNamespaceDeclaration($tokens, $index);
63+
if ($token->isGivenKind(T_DOC_COMMENT)) {
64+
$currentNamespace = null;
6665
foreach ($namespaces as $namespace) {
67-
if ($namespace->getName() == $namespaceFQCN) {
66+
if ($namespace->getScope()->inRange($index)) {
6867
$currentNamespace = $namespace;
6968
break;
7069
}
7170
}
7271

73-
continue;
74-
}
72+
if ($currentNamespace === null) {
73+
continue;
74+
}
7575

76-
if ($token->isGivenKind(T_DOC_COMMENT)) {
7776
$docBlock = new DocBlock($token->getContent());
7877

7978
$annotations = $docBlock->getAnnotationsOfType(Annotation::getTagsWithTypes());
@@ -129,21 +128,4 @@ private function fixAnnotation(NamespaceInfo $currentNamespace, Annotation $anno
129128
$annotation->setTypes($types);
130129
}
131130
}
132-
133-
/**
134-
* @param \PhpCsFixer\Tokenizer\Tokens $tokens
135-
* @param int $index
136-
*
137-
* @return string
138-
*/
139-
private function getNamespaceDeclaration(Tokens $tokens, int $index): string
140-
{
141-
$declarationStartIndex = $index;
142-
$declarationEndIndex = $tokens->getNextTokenOfKind($index, [';', '{']);
143-
144-
return trim($tokens->generatePartialCode(
145-
$declarationStartIndex + 1,
146-
$declarationEndIndex - 1
147-
));
148-
}
149131
}

0 commit comments

Comments
 (0)