Skip to content

Commit

Permalink
magento#22317: PR#22321 fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed May 2, 2019
1 parent 9312b47 commit 74739b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnnotationFormatValidator
* @param int $commentEndPtr
* @return int
*/
private function getShortDescriptionEndPosition(File $phpcsFile, int $shortPtr, $commentEndPtr) : int
private function getShortDescriptionEndPosition(File $phpcsFile, int $shortPtr, $commentEndPtr): int
{
$tokens = $phpcsFile->getTokens();
$shortPtrEnd = $shortPtr;
Expand All @@ -49,22 +49,22 @@ private function validateMultiLinesInShortDescription(
File $phpcsFile,
int $shortPtr,
int $commentEndPtr
) : void {
): void {
$tokens = $phpcsFile->getTokens();
$shortPtrEnd = $this->getShortDescriptionEndPosition(
$phpcsFile,
(int) $shortPtr,
(int)$shortPtr,
$commentEndPtr
);
$shortPtrEndContent = $tokens[$shortPtrEnd]['content'];
if (preg_match('/^[a-z]/', $shortPtrEndContent)
&& $shortPtrEnd != $shortPtr
&& !preg_match('/\bSee\b/', $shortPtrEndContent)
&& $tokens[$shortPtr]['line']+1 === $tokens[$shortPtrEnd]['line']
&& $tokens[$shortPtr]['line'] + 1 === $tokens[$shortPtrEnd]['line']
&& $tokens[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG
) {
$error = 'Short description should not be in multi lines';
$phpcsFile->addFixableError($error, $shortPtrEnd+1, 'MethodAnnotation');
$phpcsFile->addFixableError($error, $shortPtrEnd + 1, 'MethodAnnotation');
}
}

Expand All @@ -81,17 +81,17 @@ private function validateSpacingBetweenShortAndLongDescriptions(
int $shortPtr,
int $commentEndPtr,
array $emptyTypeTokens
) : void {
): void {
$tokens = $phpcsFile->getTokens();
$shortPtrEnd = $this->getShortDescriptionEndPosition(
$phpcsFile,
(int) $shortPtr,
(int)$shortPtr,
$commentEndPtr
);
$shortPtrEndContent = $tokens[$shortPtrEnd]['content'];
if (preg_match('/^[A-Z]/', $shortPtrEndContent)
&& !preg_match('/\bSee\b/', $shortPtrEndContent)
&& $tokens[$shortPtr]['line']+1 === $tokens[$shortPtrEnd]['line']
&& $tokens[$shortPtr]['line'] + 1 === $tokens[$shortPtrEnd]['line']
&& $tokens[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG
) {
$error = 'There must be exactly one blank line between lines';
Expand Down Expand Up @@ -119,7 +119,7 @@ private function validateShortDescriptionFormat(
int $stackPtr,
int $commentEndPtr,
array $emptyTypeTokens
) : void {
): void {
$tokens = $phpcsFile->getTokens();
if ($tokens[$shortPtr]['line'] !== $tokens[$stackPtr]['line'] + 1) {
$error = 'No blank lines are allowed before short description';
Expand Down Expand Up @@ -166,7 +166,7 @@ private function validateLongDescriptionFormat(
int $shortPtrEnd,
int $commentEndPtr,
array $emptyTypeTokens
) : void {
): void {
$tokens = $phpcsFile->getTokens();
$longPtr = $phpcsFile->findNext($emptyTypeTokens, $shortPtrEnd + 1, $commentEndPtr - 1, true);
if (strtolower($tokens[$longPtr]['content']) === '@inheritdoc') {
Expand All @@ -192,7 +192,7 @@ private function validateLongDescriptionFormat(
* @param int $commentStartPtr
* @param array $emptyTypeTokens
*/
public function validateTagsSpacingFormat(File $phpcsFile, int $commentStartPtr, array $emptyTypeTokens) : void
public function validateTagsSpacingFormat(File $phpcsFile, int $commentStartPtr, array $emptyTypeTokens): void
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$commentStartPtr]['comment_tags'][0])) {
Expand All @@ -214,7 +214,7 @@ public function validateTagsSpacingFormat(File $phpcsFile, int $commentStartPtr,
* @param File $phpcsFile
* @param int $commentStartPtr
*/
public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr) : void
public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr): void
{
$tokens = $phpcsFile->getTokens();
$tagGroups = [];
Expand Down Expand Up @@ -256,7 +256,7 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr)
* @param File $phpcsFile
* @param int $commentStartPtr
*/
public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr) : void
public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr): void
{
$tokens = $phpcsFile->getTokens();
$noAlignmentPositions = [];
Expand Down Expand Up @@ -287,7 +287,7 @@ public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr)
* @param array $actualPositions
* @return bool
*/
private function allTagsAligned(array $actualPositions)
private function allTagsAligned(array $actualPositions): bool
{
return count(array_unique($actualPositions)) === 1;
}
Expand All @@ -299,7 +299,7 @@ private function allTagsAligned(array $actualPositions)
* @param array $noAlignmentPositions
* @return bool
*/
private function noneTagsAligned(array $actualPositions, array $noAlignmentPositions)
private function noneTagsAligned(array $actualPositions, array $noAlignmentPositions): bool
{
return $actualPositions === $noAlignmentPositions;
}
Expand All @@ -317,7 +317,7 @@ private function validateNoExtraNewLineBeforeShortDescription(
int $commentStartPtr,
int $commentEndPtr,
array $emptyTypeTokens
) : void {
): void {
$tokens = $phpcsFile->getTokens();
$prevPtr = $phpcsFile->findPrevious($emptyTypeTokens, $commentEndPtr - 1, $commentStartPtr, true);
if ($tokens[$prevPtr]['line'] < ($tokens[$commentEndPtr]['line'] - 1)) {
Expand All @@ -341,7 +341,7 @@ public function validateDescriptionFormatStructure(
int $shortPtr,
int $commentEndPtr,
array $emptyTypeTokens
) : void {
): void {
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$commentStartPtr]['comment_tags'][0])
) {
Expand All @@ -355,7 +355,7 @@ public function validateDescriptionFormatStructure(
} else {
$this->validateShortDescriptionFormat(
$phpcsFile,
(int) $shortPtr,
(int)$shortPtr,
(int)$commentStartPtr,
(int)$commentEndPtr,
$emptyTypeTokens
Expand All @@ -364,7 +364,7 @@ public function validateDescriptionFormatStructure(
} else {
$this->validateShortDescriptionFormat(
$phpcsFile,
(int) $shortPtr,
(int)$shortPtr,
$commentStartPtr,
$commentEndPtr,
$emptyTypeTokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sniffs\Annotation;

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

/**
* Sniff to validate structure of public, private, protected method annotations
Expand Down Expand Up @@ -46,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr)
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($stackPtr), 0);
$commentEndPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr), 0);
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
$functionPtrContent = $tokens[$stackPtr+2]['content'] ;
$functionPtrContent = $tokens[$stackPtr + 2]['content'];
if (preg_match('/(?i)__construct/', $functionPtrContent)) {
return;
}
Expand All @@ -62,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
$this->annotationFormatValidator->validateDescriptionFormatStructure(
$phpcsFile,
$commentStartPtr,
(int) $shortPtr,
(int)$shortPtr,
$commentEndPtr,
$emptyTypeTokens
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sniffs\Annotation;
Expand Down Expand Up @@ -124,10 +123,9 @@ private function getMethodArguments(File $phpcsFile, int $openParenthesisPtr, in
private function getMethodParameters(array $paramDefinitions): array
{
$paramName = [];
$paramCount = count($paramDefinitions);
for ($i = 0; $i < $paramCount; $i++) {
if (isset($paramDefinitions[$i]['paramName'])) {
$paramName[] = $paramDefinitions[$i]['paramName'];
foreach ($paramDefinitions as $paramDefinition) {
if (isset($paramDefinition['paramName'])) {
$paramName[] = $paramDefinition['paramName'];
}
}
return $paramName;
Expand Down Expand Up @@ -372,13 +370,12 @@ private function validateDuplicateAnnotationDoesnotExists(
$parametersCount = count($paramPointers);
if ($argumentsCount <= $parametersCount && $argumentsCount > 0) {
$duplicateParameters = [];
$paramCount = count($paramDefinitions);
for ($i = 0; $i < $paramCount; $i++) {
if (isset($paramDefinitions[$i]['paramName'])) {
$parameterContent = $paramDefinitions[$i]['paramName'];
for ($j = $i + 1; $j < $paramCount; $j++) {
if (isset($paramDefinitions[$j]['paramName'])
&& $parameterContent === $paramDefinitions[$j]['paramName']
foreach ($paramDefinitions as $i => $paramDefinition) {
if (isset($paramDefinition['paramName'])) {
$parameterContent = $paramDefinition['paramName'];
foreach (array_slice($paramDefinitions, $i + 1) as $nextParamDefinition) {
if (isset($nextParamDefinition['paramName'])
&& $parameterContent === $nextParamDefinition['paramName']
) {
$duplicateParameters[] = $parameterContent;
}
Expand Down Expand Up @@ -473,6 +470,8 @@ private function validateParameterAnnotationFormatIsCorrect(
* @param array $methodArguments
* @param int $previousCommentOpenPtr
* @param int $previousCommentClosePtr
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
private function validateMethodParameterAnnotations(
int $stackPtr,
Expand Down Expand Up @@ -519,7 +518,8 @@ private function validateMethodParameterAnnotations(
$paramPointers
);
$tokens = $phpcsFile->getTokens();
for ($ptr = 0; $ptr < $argumentCount; $ptr++) {

foreach ($methodArguments as $ptr => $methodArgument) {
if (isset($paramPointers[$ptr])) {
$this->validateArgumentNameInParameterAnnotationExists(
$stackPtr,
Expand Down Expand Up @@ -605,6 +605,8 @@ public function process(File $phpcsFile, $stackPtr)
* @param File $phpcsFile
* @param array $paramPointers
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*
* @see https://devdocs.magento.com/guides/v2.3/coding-standards/docblock-standard-general.html#format-consistency
*/
private function validateFormattingConsistency(
Expand All @@ -616,20 +618,19 @@ private function validateFormattingConsistency(
$argumentPositions = [];
$commentPositions = [];
$tokens = $phpcsFile->getTokens();
$argumentCount = count($methodArguments);
for ($ptr = 0; $ptr < $argumentCount; $ptr++) {
foreach ($methodArguments as $ptr => $methodArgument) {
if (isset($paramPointers[$ptr])) {
$paramContent = $tokens[$paramPointers[$ptr] + 2]['content'];
$paramDefinition = $paramDefinitions[$ptr];
$argumentPositions[] = strpos($paramContent, $paramDefinition['paramName']);
$commentPositions[] = $paramDefinition['comment']
? strpos($paramContent, $paramDefinition['comment']) : null;
? strrpos($paramContent, $paramDefinition['comment']) : null;
}
}
if (!$this->allParamsAligned($argumentPositions, $commentPositions)
&& !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)) {
$phpcsFile->addFixableError(
'Visual alignment must be consistent',
'Method arguments visual alignment must be consistent',
$paramPointers[0],
'MethodArguments'
);
Expand Down

0 comments on commit 74739b9

Please sign in to comment.