-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/WebimpressCodingStandard/Sniffs/Classes/ConstAfterTraitsSniff.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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebimpressCodingStandard\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
use const T_ANON_CLASS; | ||
use const T_CONST; | ||
use const T_FUNCTION; | ||
use const T_INTERFACE; | ||
use const T_VARIABLE; | ||
|
||
class ConstAfterTraitsSniff extends AbstractScopeSniff | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct([T_ANON_CLASS, T_CLASS, T_INTERFACE], [T_CONST]); | ||
} | ||
|
||
/** | ||
* @param int $stackPtr | ||
* @param int $currScope | ||
*/ | ||
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$scopeOpener = $tokens[$currScope]['scope_opener']; | ||
|
||
$find = $phpcsFile->findNext([T_FUNCTION, T_VARIABLE], $scopeOpener, $stackPtr); | ||
if ($find) { | ||
$error = $tokens[$find]['code'] === T_FUNCTION | ||
? 'Method declaration forbidden in line %d before constant declaration' | ||
: 'Property declaration forbidden in line %d before constant declaration'; | ||
|
||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BeforeConstant', [$tokens[$find]['line']]); | ||
if ($fix) { | ||
$before = $phpcsFile->findPrevious(Tokens::$emptyTokens + Tokens::$methodPrefixes + [ | ||
T_VAR => T_VAR, | ||
], $find - 1, null, true); | ||
|
||
$from = $phpcsFile->findPrevious(Tokens::$emptyTokens + Tokens::$scopeModifiers, $stackPtr - 1, null, true); | ||
$eos = $phpcsFile->findEndOfStatement($stackPtr); | ||
|
||
$toMove = $phpcsFile->getTokensAsString($from + 1, $eos - $from); | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->addContent($before, $toMove); | ||
for ($i = $from + 1; $i <= $eos; ++$i) { | ||
$phpcsFile->fixer->replaceToken($i, ''); | ||
} | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
* | ||
* @param int $stackPtr | ||
*/ | ||
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) | ||
{ | ||
// do not process constants outside the scope | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
class ConstAfterTraitsClass | ||
{ | ||
use MyTrait; | ||
use MyOtherTrait { | ||
func1 as func2; | ||
} | ||
|
||
/** property comment **/ | ||
var $property = 'val'; | ||
|
||
/** constant comment */ | ||
const CONST_1 = 'const'; | ||
} | ||
|
||
interface ConstAfterTraitsInterface | ||
{ | ||
/** method comment */ | ||
public function method(); | ||
|
||
/** constant comment */ | ||
const CONST_2 = 'const'; | ||
} | ||
|
||
$a = new class() { | ||
static $prop; | ||
const CONST_3 = 'const'; | ||
}; | ||
|
||
$b = new class() { | ||
/** property comment */ | ||
public static $prop; | ||
|
||
/** method comment */ | ||
public function method() | ||
{ | ||
} | ||
|
||
/** const comment */ | ||
public const CONST_4 = 'const'; | ||
}; |
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,42 @@ | ||
<?php | ||
|
||
class ConstAfterTraitsClass | ||
{ | ||
use MyTrait; | ||
use MyOtherTrait { | ||
func1 as func2; | ||
} | ||
|
||
/** constant comment */ | ||
const CONST_1 = 'const'; | ||
|
||
/** property comment **/ | ||
var $property = 'val'; | ||
} | ||
|
||
interface ConstAfterTraitsInterface | ||
{ | ||
|
||
/** constant comment */ | ||
const CONST_2 = 'const'; | ||
/** method comment */ | ||
public function method(); | ||
} | ||
|
||
$a = new class() { | ||
const CONST_3 = 'const'; | ||
static $prop; | ||
}; | ||
|
||
$b = new class() { | ||
|
||
/** const comment */ | ||
public const CONST_4 = 'const'; | ||
/** property comment */ | ||
public static $prop; | ||
|
||
/** method comment */ | ||
public function method() | ||
{ | ||
} | ||
}; |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebimpressCodingStandardTest\Sniffs\Classes; | ||
|
||
use WebimpressCodingStandardTest\Sniffs\AbstractTestCase; | ||
|
||
class ConstAfterTraitsUnitTest extends AbstractTestCase | ||
{ | ||
protected function getErrorList(string $testFile = '') : array | ||
{ | ||
return [ | ||
14 => 1, | ||
23 => 1, | ||
28 => 1, | ||
41 => 1, | ||
]; | ||
} | ||
|
||
protected function getWarningList(string $testFile = '') : array | ||
{ | ||
return []; | ||
} | ||
} |