Skip to content
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

magento/magento2#22317: CodeSniffer should not mark correctly aligned DocBlock elements as code style violation. #22321

Merged
merged 2 commits into from
Apr 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sniffs\Annotation;

use PHP_CodeSniffer\Files\File;
Expand Down Expand Up @@ -249,6 +250,60 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr)
}
}

/**
* Validates tag aligning format
*
* @param File $phpcsFile
* @param int $commentStartPtr
*/
public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr) : void
{
$tokens = $phpcsFile->getTokens();
$noAlignmentPositions = [];
$actualPositions = [];
$stackPtr = null;
foreach ($tokens[$commentStartPtr]['comment_tags'] as $tag) {
$content = $tokens[$tag]['content'];
if (preg_match('/^@/', $content) && ($tokens[$tag]['line'] === $tokens[$tag + 2]['line'])) {
$noAlignmentPositions[] = $tokens[$tag + 1]['column'] + 1;
$actualPositions[] = $tokens[$tag + 2]['column'];
$stackPtr = $stackPtr ?? $tag;
}
}

if (!$this->allTagsAligned($actualPositions)
&& !$this->noneTagsAligned($actualPositions, $noAlignmentPositions)) {
$phpcsFile->addFixableError(
'Tags visual alignment must be consistent',
$stackPtr,
'MethodArguments'
);
}
}

/**
* Check whether all docblock params are aligned.
*
* @param array $actualPositions
* @return bool
*/
private function allTagsAligned(array $actualPositions)
{
return count(array_unique($actualPositions)) === 1;
}

/**
* Check whether all docblock params are not aligned.
*
* @param array $actualPositions
* @param array $noAlignmentPositions
* @return bool
*/
private function noneTagsAligned(array $actualPositions, array $noAlignmentPositions)
{
return $actualPositions === $noAlignmentPositions;
}

/**
* Validates extra newline before short description
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function process(File $phpcsFile, $stackPtr)
$emptyTypeTokens
);
$this->annotationFormatValidator->validateTagGroupingFormat($phpcsFile, $commentStartPtr);
$this->annotationFormatValidator->validateTagAligningFormat($phpcsFile, $commentStartPtr);
}
}
}
Loading