From 87ae4dcbaf401c59ba24eb009503cfd5bc026688 Mon Sep 17 00:00:00 2001 From: Vossik Date: Thu, 25 Nov 2021 19:48:17 +0100 Subject: [PATCH 1/2] ReferenceUsedNamesAfterUsageSniff - report FQN if class is already used --- .../ReferenceUsedNamesAfterUsageSniff.php | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php index 00dc0f6..321e1cb 100644 --- a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php +++ b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php @@ -173,29 +173,47 @@ public function process(File $phpcsFile, $openTagPointer) : void $end = TokenHelper::findLastTokenOnLine($phpcsFile, $reference->startPointer); $lineLength = \strlen(TokenHelper::getContent($phpcsFile, $start, $end)); - if (!$shouldBeUsed - || ($this->count === null || $this->count > $referenced[$canonicalName]) - && ($this->length === null || $this->length > \strlen($canonicalName)) - && ($this->lineLength === null || $this->lineClassLength === null || $this->lineClassLength > \strlen($canonicalName) - || $this->lineLength > $lineLength) - ) { - continue; + $isAlreadyImported = false; + + foreach ($useStatements as $useStatement) { + if ($useStatement->getFullyQualifiedTypeName() === $canonicalName) { + $isAlreadyImported = true; + + break; + } + } + + $isImported = $isFullyQualified && $isAlreadyImported; + + if (!$isImported) { + if (!$shouldBeUsed + || ($this->count === null || $this->count > $referenced[$canonicalName]) + && ($this->length === null || $this->length > \strlen($canonicalName)) + && ($this->lineLength === null || $this->lineClassLength === null || $this->lineClassLength > \strlen($canonicalName) + || $this->lineLength > $lineLength) + ) { + continue; + } } $reason = ''; - if ($this->count !== null && $referenced[$canonicalName] >= $this->count) { + if ($isImported) { + $reason = 'because it\'s already imported.'; + } + + if (!$isImported && $this->count !== null && $referenced[$canonicalName] >= $this->count) { $reason = 'because it\'s used more than ' . $this->count . ' times.'; } - if ($this->length !== null && $this->length <= \strlen($canonicalName)) { + if (!$isImported && $this->length !== null && $this->length <= \strlen($canonicalName)) { $reason = $reason === '' ? 'because it\'s length is more than ' . $this->length . ' symbols.' : 'because it\'s used more than ' . $this->count . ' times and it\'s length is more than ' . $this->length . ' symbols.'; } - if ($this->lineLength !== null && $this->lineClassLength !== null && \strlen($canonicalName) >= $this->lineClassLength + if (!$isImported && $this->lineLength !== null && $this->lineClassLength !== null && \strlen($canonicalName) >= $this->lineClassLength && $lineLength >= $this->lineLength) { $reason = 'because line length is more than ' . $this->lineLength . ' symbols and class length is more than ' . $this->lineClassLength . ' symbols.'; From 5a33541dab75292f93456ea66a93c8b5c193722c Mon Sep 17 00:00:00 2001 From: Vossik Date: Thu, 25 Nov 2021 19:50:45 +0100 Subject: [PATCH 2/2] codestyle --- .../ControlStructures/RequireMultiLineNullCoalesceSniff.php | 4 ++-- .../Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php | 4 ++-- .../Sniffs/TypeHints/UnionTypeHintFormatSniff.php | 4 ++-- .../Sniffs/WhiteSpace/MemberVarSpacingSniff.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/InfinityloopCodingStandard/Sniffs/ControlStructures/RequireMultiLineNullCoalesceSniff.php b/InfinityloopCodingStandard/Sniffs/ControlStructures/RequireMultiLineNullCoalesceSniff.php index 41c9ee5..121dbcf 100644 --- a/InfinityloopCodingStandard/Sniffs/ControlStructures/RequireMultiLineNullCoalesceSniff.php +++ b/InfinityloopCodingStandard/Sniffs/ControlStructures/RequireMultiLineNullCoalesceSniff.php @@ -55,7 +55,7 @@ public function process(File $phpcsFile, $coalescePointer) : void $phpcsFile->fixer->endChangeset(); } - private function getEndOfLineBefore(\PHP_CodeSniffer\Files\File $phpcsFile, int $pointer) : int + private function getEndOfLineBefore(File $phpcsFile, int $pointer) : int { $tokens = $phpcsFile->getTokens(); @@ -103,7 +103,7 @@ private function getEndOfLineBefore(\PHP_CodeSniffer\Files\File $phpcsFile, int return $endOfLineBefore; } - private function getIndentation(\PHP_CodeSniffer\Files\File $phpcsFile, int $endOfLinePointer) : string + private function getIndentation(File $phpcsFile, int $endOfLinePointer) : string { $pointerAfterWhitespace = TokenHelper::findNextExcluding($phpcsFile, \T_WHITESPACE, $endOfLinePointer + 1); $actualIndentation = TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $pointerAfterWhitespace - 1); diff --git a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php index 321e1cb..06f031d 100644 --- a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php +++ b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php @@ -388,7 +388,7 @@ static function (bool $carry, string $use) use ($canonicalName) : bool { $phpcsFile->fixer->endChangeset(); } - private function getUseStatementPlacePointer(\PHP_CodeSniffer\Files\File $phpcsFile, int $openTagPointer, array $useStatements) : int + private function getUseStatementPlacePointer(File $phpcsFile, int $openTagPointer, array $useStatements) : int { if (\count($useStatements) !== 0) { $lastUseStatement = \array_values($useStatements)[\count($useStatements) - 1]; @@ -436,7 +436,7 @@ private function getUseStatementPlacePointer(\PHP_CodeSniffer\Files\File $phpcsF return $useStatementPlacePointer; } - private function getReferences(\PHP_CodeSniffer\Files\File $phpcsFile, int $openTagPointer) : array + private function getReferences(File $phpcsFile, int $openTagPointer) : array { $references = []; diff --git a/InfinityloopCodingStandard/Sniffs/TypeHints/UnionTypeHintFormatSniff.php b/InfinityloopCodingStandard/Sniffs/TypeHints/UnionTypeHintFormatSniff.php index 722eab6..7db1fe3 100644 --- a/InfinityloopCodingStandard/Sniffs/TypeHints/UnionTypeHintFormatSniff.php +++ b/InfinityloopCodingStandard/Sniffs/TypeHints/UnionTypeHintFormatSniff.php @@ -224,7 +224,7 @@ private function checkTypeHint(File $phpcsFile, TypeHint $typeHint) : void private function getTypeHintContentWithoutNull( File $phpcsFile, - \SlevomatCodingStandard\Helpers\TypeHint $typeHint, + TypeHint $typeHint, ) : string { $tokens = $phpcsFile->getTokens(); @@ -258,7 +258,7 @@ private function getTypeHintContentWithoutNull( private function fixTypeHint( File $phpcsFile, - \SlevomatCodingStandard\Helpers\TypeHint $typeHint, + TypeHint $typeHint, string $fixedTypeHint, ) : void { diff --git a/InfinityloopCodingStandard/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/InfinityloopCodingStandard/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index 69590e7..9676ca9 100644 --- a/InfinityloopCodingStandard/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/InfinityloopCodingStandard/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -168,12 +168,12 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) : ?int } //@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing - protected function processVariable(\PHP_CodeSniffer\Files\File $phpcsFile, $stackPtr) : void + protected function processVariable(File $phpcsFile, $stackPtr) : void { } //@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing - protected function processVariableInString(\PHP_CodeSniffer\Files\File $phpcsFile, $stackPtr) : void + protected function processVariableInString(File $phpcsFile, $stackPtr) : void { } }