Skip to content

Commit 4a98d9c

Browse files
committed
Cleanup
1 parent 6bc9678 commit 4a98d9c

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -156,74 +156,74 @@ function (ReferencedName $referencedName): string {
156156
}
157157

158158
if (NamespaceHelper::isFullyQualifiedName($name)) {
159+
if (!$this->isClassRequiredToBeUsed($name)) {
160+
continue;
161+
}
162+
159163
$isExceptionByName = StringHelper::endsWith($name, 'Exception')
160164
|| $name === '\Throwable'
161165
|| (StringHelper::endsWith($name, 'Error') && !NamespaceHelper::hasNamespace($name))
162166
|| in_array($canonicalName, $this->getSpecialExceptionNames(), true);
163167
$inIgnoredNames = in_array($canonicalName, $this->getIgnoredNames(), true);
164-
if (
165-
$this->isClassRequiredToBeUsed($name) &&
166-
(
167-
!$this->allowFullyQualifiedExceptions
168-
|| !$isExceptionByName
169-
|| $inIgnoredNames
170-
)
171-
) {
172-
$previousKeywordPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge(TokenHelper::$nameTokenCodes, [T_WHITESPACE, T_COMMA]), $nameStartPointer - 1);
173-
if (!in_array($tokens[$previousKeywordPointer]['code'], $this->getFullyQualifiedKeywords(), true)) {
174-
if (
175-
!NamespaceHelper::hasNamespace($name)
176-
&& NamespaceHelper::findCurrentNamespaceName($phpcsFile, $nameStartPointer) === null
177-
) {
178-
$fix = $phpcsFile->addFixableError(sprintf(
179-
'Type %s should not be referenced via a fully qualified name, but via an unqualified name without the leading \\, because the file does not have a namespace and the type cannot be put in a use statement.',
180-
$name
181-
), $nameStartPointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME_WITHOUT_NAMESPACE);
182-
if ($fix) {
183-
$phpcsFile->fixer->beginChangeset();
184-
$phpcsFile->fixer->replaceToken($nameStartPointer, substr($tokens[$nameStartPointer]['content'], 1));
185-
$phpcsFile->fixer->endChangeset();
186-
}
187-
} else {
188-
$fix = $phpcsFile->addFixableError(sprintf(
189-
'Type %s should not be referenced via a fully qualified name, but via a use statement.',
190-
$name
191-
), $nameStartPointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
192-
if ($fix) {
193-
$useStatements = UseStatementHelper::getUseStatements($phpcsFile, $openTagPointer);
194-
if (count($useStatements) === 0) {
195-
$namespacePointer = $phpcsFile->findNext(T_NAMESPACE, $openTagPointer);
196-
$useStatementPlacePointer = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $namespacePointer + 1);
197-
} else {
198-
$lastUseStatement = array_values($useStatements)[count($useStatements) - 1];
199-
$useStatementPlacePointer = $phpcsFile->findNext(T_SEMICOLON, $lastUseStatement->getPointer() + 1);
200-
}
201168

202-
$phpcsFile->fixer->beginChangeset();
169+
if ($isExceptionByName && !$inIgnoredNames && $this->allowFullyQualifiedExceptions) {
170+
continue;
171+
}
203172

204-
for ($i = $referencedName->getStartPointer(); $i <= $referencedName->getEndPointer(); $i++) {
205-
$phpcsFile->fixer->replaceToken($i, '');
206-
}
173+
$previousKeywordPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge(TokenHelper::$nameTokenCodes, [T_WHITESPACE, T_COMMA]), $nameStartPointer - 1);
174+
if (!in_array($tokens[$previousKeywordPointer]['code'], $this->getFullyQualifiedKeywords(), true)) {
175+
if (
176+
!NamespaceHelper::hasNamespace($name)
177+
&& NamespaceHelper::findCurrentNamespaceName($phpcsFile, $nameStartPointer) === null
178+
) {
179+
$fix = $phpcsFile->addFixableError(sprintf(
180+
'Type %s should not be referenced via a fully qualified name, but via an unqualified name without the leading \\, because the file does not have a namespace and the type cannot be put in a use statement.',
181+
$name
182+
), $nameStartPointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME_WITHOUT_NAMESPACE);
183+
if ($fix) {
184+
$phpcsFile->fixer->beginChangeset();
185+
$phpcsFile->fixer->replaceToken($nameStartPointer, substr($tokens[$nameStartPointer]['content'], 1));
186+
$phpcsFile->fixer->endChangeset();
187+
}
188+
} else {
189+
$fix = $phpcsFile->addFixableError(sprintf(
190+
'Type %s should not be referenced via a fully qualified name, but via a use statement.',
191+
$name
192+
), $nameStartPointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME);
193+
if ($fix) {
194+
$useStatements = UseStatementHelper::getUseStatements($phpcsFile, $openTagPointer);
195+
if (count($useStatements) === 0) {
196+
$namespacePointer = $phpcsFile->findNext(T_NAMESPACE, $openTagPointer);
197+
$useStatementPlacePointer = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $namespacePointer + 1);
198+
} else {
199+
$lastUseStatement = array_values($useStatements)[count($useStatements) - 1];
200+
$useStatementPlacePointer = $phpcsFile->findNext(T_SEMICOLON, $lastUseStatement->getPointer() + 1);
201+
}
207202

208-
$nameToReference = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
209-
$alreadyUsed = false;
210-
foreach ($useStatements as $useStatement) {
211-
if ($useStatement->getFullyQualifiedTypeName() === $canonicalName) {
212-
$nameToReference = $useStatement->getNameAsReferencedInFile();
213-
$alreadyUsed = true;
214-
break;
215-
}
216-
}
203+
$phpcsFile->fixer->beginChangeset();
217204

218-
$phpcsFile->fixer->addContent($referencedName->getStartPointer(), $nameToReference);
205+
for ($i = $referencedName->getStartPointer(); $i <= $referencedName->getEndPointer(); $i++) {
206+
$phpcsFile->fixer->replaceToken($i, '');
207+
}
219208

220-
if (!$alreadyUsed) {
221-
$phpcsFile->fixer->addNewline($useStatementPlacePointer);
222-
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s;', $canonicalName));
209+
$nameToReference = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
210+
$alreadyUsed = false;
211+
foreach ($useStatements as $useStatement) {
212+
if ($useStatement->getFullyQualifiedTypeName() === $canonicalName) {
213+
$nameToReference = $useStatement->getNameAsReferencedInFile();
214+
$alreadyUsed = true;
215+
break;
223216
}
217+
}
218+
219+
$phpcsFile->fixer->addContent($referencedName->getStartPointer(), $nameToReference);
224220

225-
$phpcsFile->fixer->endChangeset();
221+
if (!$alreadyUsed) {
222+
$phpcsFile->fixer->addNewline($useStatementPlacePointer);
223+
$phpcsFile->fixer->addContent($useStatementPlacePointer, sprintf('use %s;', $canonicalName));
226224
}
225+
226+
$phpcsFile->fixer->endChangeset();
227227
}
228228
}
229229
}

0 commit comments

Comments
 (0)