Skip to content

Psr12 #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 8 additions & 50 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Sniffs\FixerHelper;

/**
* A test to ensure that arrays conform to the array coding standard.
Expand Down Expand Up @@ -74,12 +75,7 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr + 1; $i < $arrayStart; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $arrayStart);
}
}
} else {
Expand All @@ -100,12 +96,7 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
FixerHelper::removeAll($phpcsFile, $arrayStart + 1, $arrayEnd);
}
}

Expand Down Expand Up @@ -310,11 +301,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
[$currentIndent, $tokens[$end]['column'] - 1]
);
if ($fix) {
if (0 === $found) {
$phpcsFile->fixer->addContent($end - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($end - 1, str_repeat(' ', $expected));
}
FixerHelper::fixWhitespaceBefore($phpcsFile, $end, $expected, $found);
}
}

Expand Down Expand Up @@ -542,11 +529,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if ($fix) {
if (0 === $found) {
$phpcsFile->fixer->addContent($value['value'] - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($value['value'] - 1, str_repeat(' ', $expected));
}
FixerHelper::fixWhitespaceBefore($phpcsFile, $value['value'], $expected, $found);
}
}
}
Expand Down Expand Up @@ -651,7 +634,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if ($fix) {
$this->align($phpcsFile, $index['index'], $expected, $found);
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['index'], $expected, $found);
}

continue;
Expand All @@ -675,7 +658,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if ($fix) {
$this->align($phpcsFile, $index['arrow'], $expected, $found);
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['arrow'], $expected, $found);
}

continue;
Expand All @@ -698,34 +681,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if ($fix) {
$this->align($phpcsFile, $index['value'], $expected, $found);
FixerHelper::fixWhitespaceBefore($phpcsFile, $index['value'], $expected, $found);
}
}
}
}

/**
* @param File $phpcsFile
* @param int $elementIndex
* @param int $expected
* @param int|string $found
*/
private function align(File $phpcsFile, int $elementIndex, int $expected, $found): void
{
if ('newline' === $found) {
$phpcsFile->fixer->beginChangeset();

$prev = $phpcsFile->findPrevious(T_WHITESPACE, $elementIndex - 1, null, true);
for ($i = $prev + 1; $i < $elementIndex; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
$phpcsFile->fixer->endChangeset();
} elseif (0 === $found) {
$phpcsFile->fixer->addContent($elementIndex - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($elementIndex - 1, str_repeat(' ', $expected));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function process(File $phpcsFile, $stackPtr): void
$phpcsFile->addError(
'The %s annotation is forbidden to use',
$stackPtr,
'',
'Invalid',
[$tokens[$stackPtr]['content']]
);
}
Expand Down
47 changes: 19 additions & 28 deletions SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Sniffs\FixerHelper;
use SymfonyCustom\Sniffs\SniffHelper;

/**
Expand Down Expand Up @@ -70,7 +71,12 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
FixerHelper::removeLines(
$phpcsFile,
$previousElement,
$previousLine + 1,
$commentTagLine
);
}
}
} elseif ($currentIsCustom && $previousIsCustom) {
Expand All @@ -82,7 +88,12 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
FixerHelper::removeLines(
$phpcsFile,
$previousElement,
$previousLine + 1,
$commentTagLine
);
}
}
} elseif (!$currentIsCustom && !$isNewType) {
Expand All @@ -106,7 +117,12 @@ public function process(File $phpcsFile, $stackPtr): void

$phpcsFile->fixer->addContentBefore($firstOnLine, $content.$phpcsFile->eolChar);
} else {
$this->removeLines($phpcsFile, $previousElement, $previousLine + 2, $commentTagLine - 1);
FixerHelper::removeLines(
$phpcsFile,
$previousElement,
$previousLine + 2,
$commentTagLine
);
}
}
}
Expand All @@ -119,29 +135,4 @@ public function process(File $phpcsFile, $stackPtr): void
}
}
}

/**
* @param File $phpcsFile
* @param int $fromPtr
* @param int $fromLine
* @param int $toLine
*/
private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void
{
$tokens = $phpcsFile->getTokens();

$phpcsFile->fixer->beginChangeset();

for ($i = $fromPtr;; $i++) {
if ($tokens[$i]['line'] > $toLine) {
break;
}

if ($fromLine <= $tokens[$i]['line']) {
$phpcsFile->fixer->replaceToken($i, '');
}
}

$phpcsFile->fixer->endChangeset();
}
}
28 changes: 4 additions & 24 deletions SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Sniffs\FixerHelper;

/**
* Ensures doc blocks follow basic formatting.
Expand Down Expand Up @@ -38,10 +39,7 @@ public function process(File $phpcsFile, $stackPtr): void

$commentEnd = $tokens[$stackPtr]['comment_closer'];

$empty = [
T_DOC_COMMENT_WHITESPACE,
T_DOC_COMMENT_STAR,
];
$empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR];

$short = $phpcsFile->findNext($empty, $stackPtr + 1, $commentEnd, true);
if (false === $short) {
Expand Down Expand Up @@ -114,16 +112,7 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr + 1; $i < $short; $i++) {
if ($tokens[$i + 1]['line'] === $tokens[$short]['line']) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
FixerHelper::removeAll($phpcsFile, $stackPtr + 1, $short);
}
}

Expand Down Expand Up @@ -163,16 +152,7 @@ public function process(File $phpcsFile, $stackPtr): void
);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $prev + 1; $i < $commentEnd; $i++) {
if ($tokens[$i + 1]['line'] === $tokens[$commentEnd]['line']) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
FixerHelper::removeAll($phpcsFile, $prev + 1, $commentEnd);
}
}
}
Expand Down
54 changes: 0 additions & 54 deletions SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ public function process(File $phpcsFile, $stackPtr): void
}
}
}

$this->processWhitespace($phpcsFile, $commentStart);
}

/**
Expand Down Expand Up @@ -215,58 +213,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart): voi
parent::processParams($phpcsFile, $stackPtr, $commentStart);
}

/**
* @param File $phpcsFile
* @param int $commentStart
* @param bool $hasComment
*/
private function processWhitespace(File $phpcsFile, int $commentStart, bool $hasComment = true): void
{
$tokens = $phpcsFile->getTokens();
$before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true);

$startLine = $tokens[$commentStart]['line'];
$prevLine = $tokens[$before]['line'];

$found = $startLine - $prevLine - 1;

// Skip for class opening
if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) {
if ($found < 0) {
$found = 0;
}

if ($hasComment) {
$error = 'Expected 1 blank line before docblock; %s found';
$rule = 'SpacingBeforeDocblock';
} else {
$error = 'Expected 1 blank line before function; %s found';
$rule = 'SpacingBeforeFunction';
}

$fix = $phpcsFile->addFixableError($error, $commentStart, $rule, [$found]);

if ($fix) {
if ($found > 1) {
$phpcsFile->fixer->beginChangeset();

for ($i = $before + 1; $i < $commentStart - 1; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
} else {
// Try and maintain indentation.
if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) {
$phpcsFile->fixer->addNewlineBefore($commentStart - 1);
} else {
$phpcsFile->fixer->addNewlineBefore($commentStart);
}
}
}
}
}

/**
* @param File $phpcsFile
* @param int $stackPtr
Expand Down
50 changes: 0 additions & 50 deletions SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void
$phpcsFile->fixer->replaceToken($string, implode(' ', $newContent));
}
}

$this->processWhitespace($phpcsFile, $commentStart);
}

/**
Expand All @@ -129,52 +127,4 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
protected function processVariableInString(File $phpcsFile, $stackPtr): void
{
}

/**
* @param File $phpcsFile
* @param int $commentStart
*/
private function processWhitespace(File $phpcsFile, int $commentStart): void
{
$tokens = $phpcsFile->getTokens();
$before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true);

$startLine = $tokens[$commentStart]['line'];
$prevLine = $tokens[$before]['line'];

$found = $startLine - $prevLine - 1;

// Skip for class opening
if ($found < 1 && T_OPEN_CURLY_BRACKET !== $tokens[$before]['code']) {
if ($found < 0) {
$found = 0;
}

$fix = $phpcsFile->addFixableError(
'Expected 1 blank line before docblock; %s found',
$commentStart,
'SpacingBeforeDocblock',
[$found]
);

if ($fix) {
if ($found > 1) {
$phpcsFile->fixer->beginChangeset();

for ($i = $before + 1; $i < $commentStart - 1; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
} else {
// Try and maintain indentation.
if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) {
$phpcsFile->fixer->addNewlineBefore($commentStart - 1);
} else {
$phpcsFile->fixer->addNewlineBefore($commentStart);
}
}
}
}
}
}
Loading