Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9274ae

Browse files
authored
Merge pull request #19 from vossik/use
improved ReferenceUsedNamesAfterUsageSniff
2 parents faf48e1 + 3159be2 commit f9274ae

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

‎InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php‎

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ReferenceUsedNamesAfterUsageSniff implements \PHP_CodeSniffer\Sniffs\Sniff
3333

3434
public ?int $count = null;
3535
public ?int $length = null;
36+
public ?int $lineLength = null;
37+
public ?int $lineClassLength = null;
3638

3739
/**
3840
* @return array<int, (int|string)>
@@ -165,9 +167,17 @@ public function process(File $phpcsFile, $openTagPointer) : void
165167
$shouldBeUsed = $isFullyQualified;
166168
}
167169

170+
$tokens = $phpcsFile->getTokens();
171+
172+
$start = TokenHelper::findFirstTokenOnLine($phpcsFile, $reference->startPointer);
173+
$end = TokenHelper::findLastTokenOnLine($phpcsFile, $reference->startPointer);
174+
$lineLength = \strlen(TokenHelper::getContent($phpcsFile, $start, $end));
175+
168176
if (!$shouldBeUsed
169177
|| ($this->count !== null && $referenced[$canonicalName] < $this->count)
170178
&& ($this->length !== null && $this->length > \strlen($canonicalName))
179+
&& ($this->lineLength !== null && $this->lineClassLength !== null && ($this->lineClassLength >= \strlen($canonicalName)
180+
|| $this->lineLength >= $lineLength))
171181
) {
172182
continue;
173183
}
@@ -185,6 +195,12 @@ public function process(File $phpcsFile, $openTagPointer) : void
185195
. $this->length . ' symbols.';
186196
}
187197

198+
if ($this->lineLength !== null && $this->lineClassLength !== null && \strlen($canonicalName) > $this->lineClassLength
199+
&& $lineLength > $this->lineLength) {
200+
$reason = 'because line length is more than ' . $this->lineLength
201+
. ' symbols and class length is more than ' . $this->lineClassLength . ' symbols.';
202+
}
203+
188204
$referenceErrors[] = (object) [
189205
'reference' => $reference,
190206
'canonicalName' => $canonicalName,
@@ -210,7 +226,7 @@ public function process(File $phpcsFile, $openTagPointer) : void
210226
$startPointer = $reference->startPointer;
211227
$canonicalName = $referenceData->canonicalName;
212228
$useStatements = UseStatementHelper::getUseStatementsForPointer($phpcsFile, $reference->startPointer);
213-
[$nameToReference, $isConflicting] = $this->getNormalizedClassName($reference->name, $useStatements);
229+
[$nameToReference, $isConflicting] = $this->getNormalizedClassName($reference->name, $useStatements, $phpcsFile);
214230
$canonicalNameToReference = \strtolower($nameToReference);
215231

216232
$canBeFixed = \array_reduce(
@@ -437,40 +453,58 @@ private function getReferences(\PHP_CodeSniffer\Files\File $phpcsFile, int $open
437453
return $references;
438454
}
439455

440-
private function getNormalizedClassName(string $name, array$useStatements) : array
456+
private function getUniqueNameFromNamespace(string $first, string$second) : array
441457
{
442-
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
458+
$firstSplit = \explode('\\', \ltrim($first, '\\'));
459+
$secondSplit = \explode('\\', \ltrim($second, '\\'));
443460

444-
foreach ($useStatementsas$useStatement) {
445-
$useStatementUnqualified = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName());
461+
$i = 0;
462+
$toUse = null;
446463

447-
if ($unqualifiedName !== $useStatementUnqualified) {
448-
continue;
464+
foreach ($firstSplit as $value) {
465+
if (!isset($secondSplit[$i])) {
466+
break;
449467
}
450468

451-
$nameSplit = \explode('\\', \ltrim($name, '\\'));
452-
$useStatementSplit = \explode('\\', \ltrim($useStatement->getFullyQualifiedTypeName(), '\\'));
469+
if (\substr($value, 0, 1) !== \substr($secondSplit[$i], 0, 1)) {
470+
$toUse = \substr($value, 0, 1);
453471

454-
$i = 0;
455-
$toUse = null;
472+
break;
473+
}
456474

457-
foreach ($nameSplit as $value) {
458-
if (!isset($useStatementSplit[$i])) {
459-
break;
460-
}
475+
$i++;
476+
}
461477

462-
if (\substr($value, 0, 1) !== \substr($useStatementSplit[$i], 0, 1)) {
463-
$toUse = \substr($value, 0, 1);
478+
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($first);
464479

465-
break;
466-
}
480+
return $toUse === null
481+
? [$unqualifiedName, false]
482+
: [$toUse . $unqualifiedName, true];
483+
}
484+
485+
private function getNormalizedClassName(string $name, array $useStatements, File $phpcsFile) : array
486+
{
487+
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($name);
488+
$className = ClassHelper::getName($phpcsFile, TokenHelper::findNext($phpcsFile, \T_CLASS, 0));
489+
490+
if ($className === $unqualifiedName) {
491+
return $this->getUniqueNameFromNamespace(
492+
$name,
493+
ClassHelper::getFullyQualifiedName($phpcsFile, TokenHelper::findNext($phpcsFile, \T_CLASS, 0)),
494+
);
495+
}
496+
497+
foreach ($useStatements as $useStatement) {
498+
$useStatementUnqualified = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName());
467499

468-
$i++;
500+
if ($unqualifiedName !== $useStatementUnqualified) {
501+
continue;
469502
}
470503

471-
return $toUse === null
472-
? [$unqualifiedName, false]
473-
: [$toUse . $unqualifiedName, true];
504+
return $this->getUniqueNameFromNamespace(
505+
$name,
506+
$useStatement->getFullyQualifiedTypeName(),
507+
);
474508
}
475509

476510
return [$unqualifiedName, false];

‎InfinityloopCodingStandard/ruleset.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@
506506
<rule ref="InfinityloopCodingStandard.Namespaces.ReferenceUsedNamesAfterUsage">
507507
<properties>
508508
<property name="count" value="3"/>
509-
<property name="length" value="45"/>
509+
<property name="length" value="60"/>
510+
<property name="lineLength" value="130"/>
511+
<property name="lineClassLength" value="45"/>
510512
</properties>
511513
</rule>
512514
</ruleset>

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Specialized version of Slevomat ReferenceUsedNamesOnlySniff with added rules whe
9393
Sniff provides the following settings:
9494
- count - Minimum number of occurrences after which the class will get imported via Use statement.
9595
- length - The maximum length of the class up to which it will be used via FQN, if this length is exceeded it will be imported via Use statement.
96+
- lineLength, lineClassLength - Class will get imported only if it's length is greater than lineClassLength and if the line length is greater than lineLength.
9697

9798
### Slevomat sniffs
9899

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /