Skip to content

Commit

Permalink
Merge 5a58f07 into df93d48
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Oct 27, 2019
2 parents df93d48 + 5a58f07 commit 31ea9dc
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
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
}
}
42 changes: 42 additions & 0 deletions test/Sniffs/Classes/ConstAfterTraitsUnitTest.inc
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';
};
42 changes: 42 additions & 0 deletions test/Sniffs/Classes/ConstAfterTraitsUnitTest.inc.fixed
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()
{
}
};
25 changes: 25 additions & 0 deletions test/Sniffs/Classes/ConstAfterTraitsUnitTest.php
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 [];
}
}

0 comments on commit 31ea9dc

Please sign in to comment.