Skip to content

Commit 6daa646

Browse files
committed
"squizlabs/php_codesniffer": "^4.0.0"
1 parent 44b105c commit 6daa646

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

PhpCollective/Sniffs/Commenting/DocBlockThrowsSniff.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ protected function extractExceptions(File $phpCsFile, int $stackPointer): array
144144
continue;
145145
}
146146

147+
// Handle the case where there's no namespace separator, string token, or fully qualified name token
148+
// (e.g., for parenthesis after 'new')
149+
if (!$this->isGivenKind([T_NS_SEPARATOR, T_STRING, T_NAME_FULLY_QUALIFIED], $tokens[$contentIndex])) {
150+
continue;
151+
}
152+
147153
$exceptions[] = $this->extractException($phpCsFile, $contentIndex);
148154

149155
continue;
@@ -228,9 +234,17 @@ protected function extractException(File $phpCsFile, int $contentIndex): array
228234
$fullClass = '';
229235

230236
$position = $contentIndex;
231-
while ($this->isGivenKind([T_NS_SEPARATOR, T_STRING], $tokens[$position])) {
232-
$fullClass .= $tokens[$position]['content'];
237+
238+
// Handle T_NAME_FULLY_QUALIFIED token (e.g., \BadMethodCallException)
239+
// or separate tokens (T_NS_SEPARATOR and T_STRING)
240+
if ($this->isGivenKind([T_NAME_FULLY_QUALIFIED], $tokens[$position])) {
241+
$fullClass = $tokens[$position]['content'];
233242
++$position;
243+
} else {
244+
while ($this->isGivenKind([T_NS_SEPARATOR, T_STRING], $tokens[$position])) {
245+
$fullClass .= $tokens[$position]['content'];
246+
++$position;
247+
}
234248
}
235249

236250
$class = $fullClass = ltrim($fullClass, '\\');

PhpCollective/Sniffs/Namespaces/UseStatementSniff.php

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -639,32 +639,45 @@ protected function checkPropertyForInstanceOf(File $phpcsFile, int $stackPtr): v
639639
return;
640640
}
641641

642-
$lastIndex = null;
643-
$j = $startIndex;
644-
$extractedUseStatement = '';
645-
$lastSeparatorIndex = null;
646-
while (true) {
647-
if (!$this->isGivenKind([T_NS_SEPARATOR, T_STRING], $tokens[$j])) {
648-
break;
642+
// Handle T_NAME_FULLY_QUALIFIED token (PHP CodeSniffer v4)
643+
$className = '';
644+
if ($tokens[$startIndex]['code'] === T_NAME_FULLY_QUALIFIED) {
645+
$extractedUseStatement = ltrim($tokens[$startIndex]['content'], '\\');
646+
if (strpos($extractedUseStatement, '\\') === false) {
647+
return; // Not a namespaced class
649648
}
649+
$lastSeparatorPos = strrpos($extractedUseStatement, '\\');
650+
$className = substr($extractedUseStatement, $lastSeparatorPos + 1);
651+
$lastIndex = $startIndex;
652+
$lastSeparatorIndex = null; // No separate separator token in v4
653+
} else {
654+
// Handle separate tokens (T_NS_SEPARATOR and T_STRING)
655+
$lastIndex = null;
656+
$j = $startIndex;
657+
$extractedUseStatement = '';
658+
$lastSeparatorIndex = null;
659+
while (true) {
660+
if (!$this->isGivenKind([T_NS_SEPARATOR, T_STRING], $tokens[$j])) {
661+
break;
662+
}
650663

651-
$lastIndex = $j;
652-
$extractedUseStatement .= $tokens[$j]['content'];
653-
if ($this->isGivenKind([T_NS_SEPARATOR], $tokens[$j])) {
654-
$lastSeparatorIndex = $j;
664+
$lastIndex = $j;
665+
$extractedUseStatement .= $tokens[$j]['content'];
666+
if ($this->isGivenKind([T_NS_SEPARATOR], $tokens[$j])) {
667+
$lastSeparatorIndex = $j;
668+
}
669+
++$j;
655670
}
656-
++$j;
657-
}
658671

659-
if ($lastIndex === null || $lastSeparatorIndex === null) {
660-
return;
661-
}
662-
663-
$extractedUseStatement = ltrim($extractedUseStatement, '\\');
672+
if ($lastIndex === null || $lastSeparatorIndex === null) {
673+
return;
674+
}
664675

665-
$className = '';
666-
for ($k = $lastSeparatorIndex + 1; $k <= $lastIndex; ++$k) {
667-
$className .= $tokens[$k]['content'];
676+
$extractedUseStatement = ltrim($extractedUseStatement, '\\');
677+
$className = '';
678+
for ($k = $lastSeparatorIndex + 1; $k <= $lastIndex; ++$k) {
679+
$className .= $tokens[$k]['content'];
680+
}
668681
}
669682

670683
$error = 'Use statement ' . $extractedUseStatement . ' for class ' . $className . ' should be in use block.';
@@ -677,13 +690,23 @@ protected function checkPropertyForInstanceOf(File $phpcsFile, int $stackPtr): v
677690

678691
$addedUseStatement = $this->addUseStatement($phpcsFile, $className, $extractedUseStatement);
679692

680-
for ($k = $lastSeparatorIndex; $k > $startIndex; --$k) {
681-
$phpcsFile->fixer->replaceToken($k, '');
682-
}
683-
$phpcsFile->fixer->replaceToken($startIndex, '');
693+
if ($lastSeparatorIndex !== null) {
694+
// Legacy: remove individual tokens
695+
for ($k = $lastSeparatorIndex; $k > $startIndex; --$k) {
696+
$phpcsFile->fixer->replaceToken($k, '');
697+
}
698+
$phpcsFile->fixer->replaceToken($startIndex, '');
684699

685-
if ($addedUseStatement['alias'] !== null) {
686-
$phpcsFile->fixer->replaceToken($lastIndex, $addedUseStatement['alias']);
700+
if ($addedUseStatement['alias'] !== null) {
701+
$phpcsFile->fixer->replaceToken($lastIndex, $addedUseStatement['alias']);
702+
}
703+
} else {
704+
// PHP CodeSniffer v4: replace single T_NAME_FULLY_QUALIFIED token
705+
if ($addedUseStatement['alias'] !== null) {
706+
$phpcsFile->fixer->replaceToken($startIndex, $addedUseStatement['alias']);
707+
} else {
708+
$phpcsFile->fixer->replaceToken($startIndex, $className);
709+
}
687710
}
688711

689712
$phpcsFile->fixer->endChangeset();

0 commit comments

Comments
 (0)