Skip to content

Commit

Permalink
Fix phpcbf crash in UnnecessaryNamespaceUsageSniff when class name is…
Browse files Browse the repository at this point in the history
… empty, fixes #212
  • Loading branch information
xalopp committed Dec 15, 2024
1 parent 036ac8a commit 20da5f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,18 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
$end,
true
);
$classNameEnd = (int) $phpcsFile->findNext(
$classNameEnd = $phpcsFile->findNext(
$this->classNameTokens,
($classNameStart + 1),
$end,
true
);
$useEnd = $phpcsFile->findNext(

if (false === $classNameEnd) {
break;
}

$useEnd = $phpcsFile->findNext(
[
T_SEMICOLON,
T_COMMA,
Expand Down
15 changes: 15 additions & 0 deletions MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.pass.6.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace T;

use A;class B
{
/**
* B's constructor
*/
public function __construct()
{
$a = new A();
$a->b();
}
}
1 change: 1 addition & 0 deletions MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UnnecessaryNamespaceUsageUnitTest extends AbstractMo4SniffUnitTest
'UnnecessaryNamespaceUsageUnitTest.pass.3.inc' => [],
'UnnecessaryNamespaceUsageUnitTest.pass.4.inc' => [],
'UnnecessaryNamespaceUsageUnitTest.pass.5.inc' => [],
'UnnecessaryNamespaceUsageUnitTest.pass.6.inc' => [],
'UnnecessaryNamespaceUsageUnitTest.fail.1.inc' => [
17 => 1,
19 => 1,
Expand Down

0 comments on commit 20da5f6

Please sign in to comment.