Skip to content

Improve #92

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 2 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
63 changes: 29 additions & 34 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if ($fix) {
if (0 === $found) {
$phpcsFile->fixer->addContent($index['index'] - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($index['index'] - 1, str_repeat(' ', $expected));
}
$this->align($phpcsFile, $index['index'], $expected, $found);
}

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

if ($fix) {
if ('newline' === $found) {
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true);
$phpcsFile->fixer->beginChangeset();
for ($i = $prev + 1; $i < $index['value']; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
$phpcsFile->fixer->endChangeset();
} elseif (0 === $found) {
$phpcsFile->fixer->addContent($index['arrow'] - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($index['arrow'] - 1, str_repeat(' ', $expected));
}
$this->align($phpcsFile, $index['arrow'], $expected, $found);
}

continue;
Expand All @@ -709,28 +692,40 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start

$fix = $phpcsFile->addFixableError(
'Array value not aligned correctly; expected %s space(s) but found %s',
$index['arrow'],
$index['value'],
'ValueNotAligned',
[$expected, $found]
);

if ($fix) {
if ('newline' === $found) {
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true);
$phpcsFile->fixer->beginChangeset();
for ($i = $prev + 1; $i < $index['value']; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
$phpcsFile->fixer->endChangeset();
} elseif (0 === $found) {
$phpcsFile->fixer->addContent($index['value'] - 1, str_repeat(' ', $expected));
} else {
$phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected));
}
$this->align($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));
}
}
}
11 changes: 0 additions & 11 deletions SymfonyCustom/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,46 @@ class ClassCommentSniff extends PEARClassCommentSniff
'category' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'precedes @package',
],
'package' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @category',
],
'subpackage' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @package',
],
'author' => [
'required' => false,
'allow_multiple' => true,
'order_text' => 'follows @subpackage (if used) or @package',
],
'copyright' => [
'required' => false,
'allow_multiple' => true,
'order_text' => 'follows @author',
],
'license' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @copyright (if used) or @author',
],
'version' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @license',
],
'link' => [
'required' => false,
'allow_multiple' => true,
'order_text' => 'follows @version',
],
'see' => [
'required' => false,
'allow_multiple' => true,
'order_text' => 'follows @link',
],
'since' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @see (if used) or @link',
],
'deprecated' => [
'required' => false,
'allow_multiple' => false,
'order_text' => 'follows @since (if used) or @see (if used) or @link',
],
];
}
130 changes: 57 additions & 73 deletions SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,114 +29,94 @@ public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

$previousType = '';
$typeSeen = [];
$previousTag = false;
$previousIsCustom = false;

foreach ($tokens[$stackPtr]['comment_tags'] as $commentTag) {
$currentType = $tokens[$commentTag]['content'];
$commentTagLine = $tokens[$commentTag]['line'];

$previousString = $phpcsFile->findPrevious(
T_DOC_COMMENT_STRING,
$commentTag,
$stackPtr
);
$currentIsCustom = !in_array($currentType, SniffHelper::TAGS);
$isNewType = !in_array($currentType, $typeSeen);

$previousTag = $phpcsFile->findPrevious(
T_DOC_COMMENT_TAG,
$commentTag - 1,
$stackPtr
);
$commentTagLine = $tokens[$commentTag]['line'];

$previousString = $phpcsFile->findPrevious(T_DOC_COMMENT_STRING, $commentTag, $stackPtr);
$previousLine = -1;

if (false !== $previousString) {
$previousLine = $tokens[$previousString]['line'];
$previousElement = $previousString;
}

if (false !== $previousTag) {
$previousType = $tokens[$previousTag]['content'];
$previousTagLine = $tokens[$previousTag]['line'];

if ($previousTagLine > $previousLine) {
$previousLine = $previousTagLine;
$previousElement = $previousTag;
}
} else {
$previousType = null;
}

if (isset($previousElement) && $previousLine >= 0) {
$currentIsCustom = !in_array($currentType, SniffHelper::TAGS);
$previousIsCustom = '' !== $previousType
&& !in_array($previousType, SniffHelper::TAGS);

if (($previousType === $currentType) || ($currentIsCustom && $previousIsCustom)) {
if ($previousType === $currentType) {
if ($previousLine !== $commentTagLine - 1) {
if ($previousType === $currentType) {
$fix = $phpcsFile->addFixableError(
'Expected no empty lines between annotations of the same type',
$commentTag,
'SameType'
);
} else {
$fix = $phpcsFile->addFixableError(
'Expected no empty lines between custom annotations',
$commentTag,
'CustomType'
);
}
$fix = $phpcsFile->addFixableError(
'Expected no empty lines between annotations of the same type',
$commentTag,
'SameType'
);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
$this->removeLines(
$phpcsFile,
$previousElement,
$previousLine + 1,
$commentTagLine - 1
);
$phpcsFile->fixer->endChangeset();
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
}
}
} else {
if ($previousLine !== $commentTagLine - 2) {
} elseif ($currentIsCustom && $previousIsCustom) {
if ($previousLine !== $commentTagLine - 1) {
$fix = $phpcsFile->addFixableError(
'Expected exactly one empty line between annotations of different types',
'Expected no empty lines between custom annotations',
$commentTag,
'DifferentType'
'CustomType'
);

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

if ($previousLine === $commentTagLine - 1) {
$firstOnLine = $phpcsFile->findFirstOnLine(
[],
$commentTag,
true
);
$star = $phpcsFile->findNext(
T_DOC_COMMENT_STAR,
$firstOnLine
);
$content = $phpcsFile->getTokensAsString(
$firstOnLine,
$star - $firstOnLine + 1
);
$phpcsFile->fixer->addContentBefore(
$firstOnLine,
$content.$phpcsFile->eolChar
);
} else {
$this->removeLines(
$phpcsFile,
$previousElement,
$previousLine + 2,
$commentTagLine - 1
);
}
$phpcsFile->fixer->endChangeset();
$this->removeLines($phpcsFile, $previousElement, $previousLine + 1, $commentTagLine - 1);
}
}
} elseif (!$currentIsCustom && !$isNewType) {
$phpcsFile->addError(
'Annotation of the same type should be together',
$commentTag,
'GroupSameType'
);
} elseif ($previousLine !== $commentTagLine - 2) {
$fix = $phpcsFile->addFixableError(
'Expected exactly one empty line between annotations of different types',
$commentTag,
'DifferentType'
);

if ($fix) {
if ($previousLine === $commentTagLine - 1) {
$firstOnLine = $phpcsFile->findFirstOnLine([], $commentTag, true);
$star = $phpcsFile->findNext(T_DOC_COMMENT_STAR, $firstOnLine);
$content = $phpcsFile->getTokensAsString($firstOnLine, $star - $firstOnLine + 1);

$phpcsFile->fixer->addContentBefore($firstOnLine, $content.$phpcsFile->eolChar);
} else {
$this->removeLines($phpcsFile, $previousElement, $previousLine + 2, $commentTagLine - 1);
}
}
}
}

$previousType = $currentType;
$previousTag = $commentTag;
$previousIsCustom = $currentIsCustom;
if (!$currentIsCustom && $isNewType) {
$typeSeen[] = $currentType;
}
}
}

Expand All @@ -150,6 +130,8 @@ private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $
{
$tokens = $phpcsFile->getTokens();

$phpcsFile->fixer->beginChangeset();

for ($i = $fromPtr;; $i++) {
if ($tokens[$i]['line'] > $toLine) {
break;
Expand All @@ -159,5 +141,7 @@ private function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $
$phpcsFile->fixer->replaceToken($i, '');
}
}

$phpcsFile->fixer->endChangeset();
}
}
30 changes: 20 additions & 10 deletions SymfonyCustom/Sniffs/Errors/UserDeprecatedSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,32 @@ public function process(File $phpcsFile, $stackPtr): void

do {
$string = $phpcsFile->findNext(T_STRING, $opener, $closer);

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

if ('E_USER_DEPRECATED' === $tokens[$string]['content'] && '@' !== $tokens[$stackPtr - 1]['content']) {
$phpcsFile->addError(
'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator',
$stackPtr,
'Invalid'
);
$opener = $string + 1;

break;
} else {
$opener = $string + 1;
if ('E_USER_DEPRECATED' !== $tokens[$string]['content']) {
continue;
}

if ('@' === $tokens[$stackPtr - 1]['content']) {
continue;
}

if ('@' === $tokens[$stackPtr - 2]['content']
&& 'T_NS_SEPARATOR' === $tokens[$stackPtr - 1]['type']
) {
continue;
}

$phpcsFile->addError(
'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator',
$stackPtr,
'Invalid'
);
break;
} while ($opener < $closer);
}
}
Loading