From 38aa821415e1df639891c3dcecdf3c3c7c2dcedc Mon Sep 17 00:00:00 2001 From: Vossik Date: Wed, 10 Nov 2021 22:09:59 +0100 Subject: [PATCH 1/2] fixed logic in ReferenceUsedNamesAfterUsageSniff --- .../ReferenceUsedNamesAfterUsageSniff.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php index 62b83a8..5e5db45 100644 --- a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php +++ b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php @@ -174,29 +174,29 @@ public function process(File $phpcsFile, $openTagPointer) : void $lineLength = \strlen(TokenHelper::getContent($phpcsFile, $start, $end)); if (!$shouldBeUsed - || ($this->count !== null && $referenced[$canonicalName] < $this->count) - && ($this->length !== null && $this->length > \strlen($canonicalName)) - && ($this->lineLength !== null && $this->lineClassLength !== null && ($this->lineClassLength >= \strlen($canonicalName) - || $this->lineLength >= $lineLength)) + || ($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 ($referenced[$canonicalName] >= $this->count) { + if ($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 ($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.'; + . $this->length . ' symbols.'; } - if ($this->lineLength !== null && $this->lineClassLength !== null && \strlen($canonicalName) > $this->lineClassLength - && $lineLength > $this->lineLength) { + if ($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.'; } @@ -485,7 +485,7 @@ private function getUniqueNameFromNamespace(string $first, string $second) : arr private function getNormalizedClassName(string $name, array $useStatements, File $phpcsFile) : array { $unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name); - $className = ClassHelper::getName($phpcsFile, TokenHelper::findNext($phpcsFile, \T_CLASS, 0)); + $className = ClassHelper::getName($phpcsFile, TokenHelper::findNext($phpcsFile, [\T_CLASS, \T_INTERFACE, T_TRAIT], 0)); if ($className === $unqualifiedName) { return $this->getUniqueNameFromNamespace( From 96637872306cf4c7c01cd695f8ba9e4400a2484c Mon Sep 17 00:00:00 2001 From: Vossik Date: Wed, 10 Nov 2021 22:13:20 +0100 Subject: [PATCH 2/2] codestyle --- .../Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php index 5e5db45..00dc0f6 100644 --- a/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php +++ b/InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php @@ -485,7 +485,7 @@ private function getUniqueNameFromNamespace(string $first, string $second) : arr private function getNormalizedClassName(string $name, array $useStatements, File $phpcsFile) : array { $unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name); - $className = ClassHelper::getName($phpcsFile, TokenHelper::findNext($phpcsFile, [\T_CLASS, \T_INTERFACE, T_TRAIT], 0)); + $className = ClassHelper::getName($phpcsFile, TokenHelper::findNext($phpcsFile, [\T_CLASS, \T_INTERFACE, \T_TRAIT], 0)); if ($className === $unqualifiedName) { return $this->getUniqueNameFromNamespace(