public static getUnqualifiedNameFromFullyQualifiedName ( string $name ) : string | ||
$name | string | |
리턴 | string |
/** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $openTagPointer */ public function process(PHP_CodeSniffer_File $phpcsFile, $openTagPointer) { $unusedNames = UseStatementHelper::getUseStatements($phpcsFile, $openTagPointer); $referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $openTagPointer, $this->searchAnnotations); foreach ($referencedNames as $referencedName) { $name = $referencedName->getNameAsReferencedInFile(); $pointer = $referencedName->getPointer(); $nameParts = NamespaceHelper::getNameParts($name); $nameAsReferencedInFile = $nameParts[0]; $normalizedNameAsReferencedInFile = UseStatement::normalizedNameAsReferencedInFile($nameAsReferencedInFile); if (!NamespaceHelper::isFullyQualifiedName($name) && isset($unusedNames[$normalizedNameAsReferencedInFile])) { if ($unusedNames[$normalizedNameAsReferencedInFile]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) { $phpcsFile->addError(sprintf('Case of reference name %s and use statement %s do not match', $nameAsReferencedInFile, $unusedNames[$normalizedNameAsReferencedInFile]->getNameAsReferencedInFile()), $pointer, self::CODE_MISMATCHING_CASE); } unset($unusedNames[$normalizedNameAsReferencedInFile]); } } foreach ($unusedNames as $value) { $fullName = $value->getFullyQualifiedTypeName(); if ($value->getNameAsReferencedInFile() !== $fullName) { if ($value->getNameAsReferencedInFile() !== NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($fullName)) { $fullName .= sprintf(' (as %s)', $value->getNameAsReferencedInFile()); } } $fix = $phpcsFile->addFixableError(sprintf('Type %s is not used in this file', $fullName), $value->getPointer(), self::CODE_UNUSED_USE); if ($fix) { $phpcsFile->fixer->beginChangeset(); $endPointer = $phpcsFile->findNext(T_SEMICOLON, $value->getPointer()) + 1; for ($i = $value->getPointer(); $i <= $endPointer; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->endChangeset(); } } }
/** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $usePointer * @return string */ public static function getNameAsReferencedInClassFromUse(PHP_CodeSniffer_File $phpcsFile, $usePointer) { $endPointer = $phpcsFile->findNext([T_SEMICOLON, T_COMMA], $usePointer + 1); $asPointer = $phpcsFile->findNext(T_AS, $usePointer + 1, $endPointer); if ($asPointer !== false) { $tokens = $phpcsFile->getTokens(); return $tokens[$phpcsFile->findNext(T_STRING, $asPointer + 1)]['content']; } $name = self::getFullyQualifiedTypeNameFromUse($phpcsFile, $usePointer); return NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name); }
/** * @param \PHP_CodeSniffer_File $phpcsFile * @param \SlevomatCodingStandard\Helpers\UseStatement[] $useStatements */ private function fixAlphabeticalOrder(PHP_CodeSniffer_File $phpcsFile, array $useStatements) { $firstUseStatement = reset($useStatements); $lastUseStatement = end($useStatements); $lastSemicolonPointer = $phpcsFile->findNext(T_SEMICOLON, $lastUseStatement->getPointer()); $phpcsFile->fixer->beginChangeset(); for ($i = $firstUseStatement->getPointer(); $i <= $lastSemicolonPointer; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } uasort($useStatements, function (UseStatement $a, UseStatement $b) { return $this->compareUseStatements($a, $b); }); $phpcsFile->fixer->addContent($firstUseStatement->getPointer(), implode(PHP_EOL, array_map(function (UseStatement $useStatement) { $unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName()); if ($unqualifiedName === $useStatement->getNameAsReferencedInFile()) { return sprintf('use %s;', $useStatement->getFullyQualifiedTypeName()); } return sprintf('use %s as %s;', $useStatement->getFullyQualifiedTypeName(), $useStatement->getNameAsReferencedInFile()); }, $useStatements))); $phpcsFile->fixer->endChangeset(); }