Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
SlevomatCodingStandard/Sniffs/Classes/TraitUseDeclarationSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use SlevomatCodingStandard\Helpers\ClassHelper; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
use const T_ANON_CLASS; | ||
use const T_CLASS; | ||
use const T_COMMA; | ||
use const T_OPEN_CURLY_BRACKET; | ||
use const T_SEMICOLON; | ||
use const T_TRAIT; | ||
use const T_WHITESPACE; | ||
|
||
class TraitUseDeclarationSniff implements Sniff | ||
{ | ||
|
||
public const CODE_MULTIPLE_TRAITS_PER_DECLARATION = 'MultipleTraitsPerDeclaration'; | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_CLASS, | ||
T_ANON_CLASS, | ||
T_TRAIT, | ||
]; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile | ||
* @param int $classPointer | ||
*/ | ||
public function process(File $phpcsFile, $classPointer): void | ||
{ | ||
$usePointers = ClassHelper::getTraitUsePointers($phpcsFile, $classPointer); | ||
|
||
foreach ($usePointers as $usePointer) { | ||
$this->checkDeclaration($phpcsFile, $usePointer); | ||
} | ||
} | ||
|
||
|
||
private function checkDeclaration(File $phpcsFile, int $usePointer): void | ||
{ | ||
$commaPointer = TokenHelper::findNextLocal($phpcsFile, T_COMMA, $usePointer + 1); | ||
if ($commaPointer === null) { | ||
return; | ||
} | ||
|
||
$endPointer = TokenHelper::findNext($phpcsFile, [T_OPEN_CURLY_BRACKET, T_SEMICOLON], $usePointer + 1); | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[$endPointer]['code'] === T_OPEN_CURLY_BRACKET) { | ||
$phpcsFile->addError('Multiple traits per use statement are forbidden.', $usePointer, self::CODE_MULTIPLE_TRAITS_PER_DECLARATION); | ||
return; | ||
} | ||
|
||
$fix = $phpcsFile->addFixableError('Multiple traits per use statement are forbidden.', $usePointer, self::CODE_MULTIPLE_TRAITS_PER_DECLARATION); | ||
|
||
if (!$fix) { | ||
return; | ||
} | ||
|
||
$indentation = ''; | ||
$currentPointer = $usePointer - 1; | ||
while ($tokens[$currentPointer]['code'] === T_WHITESPACE && $tokens[$currentPointer]['content'] !== $phpcsFile->eolChar) { | ||
$indentation .= $tokens[$currentPointer]['content']; | ||
$currentPointer--; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
|
||
$commaPointers = TokenHelper::findNextAll($phpcsFile, T_COMMA, $usePointer + 1, $endPointer); | ||
foreach ($commaPointers as $commaPointer) { | ||
$pointerAfterComma = TokenHelper::findNextEffective($phpcsFile, $commaPointer + 1); | ||
$phpcsFile->fixer->replaceToken($commaPointer, ';' . $phpcsFile->eolChar . $indentation . 'use '); | ||
for ($i = $commaPointer + 1; $i < $pointerAfterComma; $i++) { | ||
$phpcsFile->fixer->replaceToken($i, ''); | ||
} | ||
} | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
||
class TraitUseDeclarationSniffTest extends TestCase | ||
{ | ||
|
||
public function testNoErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/traitUseDeclarationNoErrors.php'); | ||
self::assertNoSniffErrorInFile($report); | ||
} | ||
|
||
public function testErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/traitUseDeclarationErrors.php'); | ||
|
||
self::assertSame(3, $report->getErrorCount()); | ||
|
||
self::assertSniffError($report, 6, TraitUseDeclarationSniff::CODE_MULTIPLE_TRAITS_PER_DECLARATION); | ||
self::assertSniffError($report, 8, TraitUseDeclarationSniff::CODE_MULTIPLE_TRAITS_PER_DECLARATION); | ||
self::assertSniffError($report, 10, TraitUseDeclarationSniff::CODE_MULTIPLE_TRAITS_PER_DECLARATION); | ||
|
||
self::assertAllFixedInFile($report); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
tests/Sniffs/Classes/data/traitUseDeclarationErrors.fixed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
class Whatever | ||
{ | ||
|
||
use A; | ||
use B; | ||
|
||
use C; | ||
use D; | ||
use E; | ||
|
||
use F, G { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kukulich
Author
Contributor
|
||
G::doSomething as doAnything; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
class Whatever | ||
{ | ||
|
||
use A, B; | ||
|
||
use C, D, E; | ||
|
||
use F, G { | ||
G::doSomething as doAnything; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
class Whatever | ||
{ | ||
|
||
use A; | ||
|
||
use B; | ||
|
||
|
||
use C; | ||
|
||
use D; | ||
|
||
} |
Isn't this one supposed to be fixed as
?