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 3159be2

Browse files
committed
ruleset
1 parent 871af70 commit 3159be2

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

‎InfinityloopCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesAfterUsageSniff.php‎

Lines changed: 17 additions & 1 deletion
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(

‎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 によって変換されたページ (->オリジナル) /