Skip to content

Allow no comment for a constructor where needed #440

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
33 changes: 30 additions & 3 deletions Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento2\Sniffs\Annotation;
@@ -52,6 +54,7 @@ public function register(): array
* Validates whether valid token exists before closing comment tag
*
* @param string $type
*
* @return bool
*/
private function isTokenBeforeClosingCommentTagValid(string $type): bool
@@ -65,6 +68,7 @@ private function isTokenBeforeClosingCommentTagValid(string $type): bool
* @param File $phpcsFile
* @param int $previousCommentClosePtr
* @param int $stackPtr
*
* @return bool
*/
private function validateCommentBlockExists(File $phpcsFile, int $previousCommentClosePtr, int $stackPtr): bool
@@ -82,6 +86,7 @@ private function validateCommentBlockExists(File $phpcsFile, int $previousCommen
if ($attributeFlag) {
continue;
}

if ($tokenCode === T_ATTRIBUTE) {
$attributeFlag = true;
continue;
@@ -91,13 +96,15 @@ private function validateCommentBlockExists(File $phpcsFile, int $previousCommen
return false;
}
}

return true;
}

/**
* Checks whether the parameter type is invalid
*
* @param string $type
*
* @return bool
*/
private function isInvalidType(string $type): bool
@@ -111,6 +118,7 @@ private function isInvalidType(string $type): bool
* @param File $phpcsFile
* @param int $openParenthesisPtr
* @param int $closedParenthesisPtr
*
* @return array
*/
private function getMethodArguments(File $phpcsFile, int $openParenthesisPtr, int $closedParenthesisPtr): array
@@ -127,13 +135,15 @@ private function getMethodArguments(File $phpcsFile, int $openParenthesisPtr, in
$i = $argumentsPtr - 1;
}
}

return $methodArguments;
}

/**
* Get parameters from method annotation
*
* @param array $paramDefinitions
*
* @return array
*/
private function getMethodParameters(array $paramDefinitions): array
@@ -144,6 +154,7 @@ private function getMethodParameters(array $paramDefinitions): array
$paramName[] = $paramDefinition['paramName'];
}
}

return $paramName;
}

@@ -194,6 +205,7 @@ private function validateInheritdocAnnotationWithBracesExists(
* @param int $previousCommentOpenPtr
* @param int $previousCommentClosePtr
* @param string $inheritdocAnnotation
*
* @return bool
*/
private function validateInheritdocAnnotationExists(
@@ -208,6 +220,7 @@ private function validateInheritdocAnnotationExists(
return true;
}
}

return false;
}

@@ -398,6 +411,7 @@ private function validateDuplicateAnnotationDoesnotExists(
}
}
}

foreach ($duplicateParameters as $value) {
$phpcsFile->addError(
$value . ' duplicate found in method annotation',
@@ -449,6 +463,7 @@ private function validateParameterAnnotationFormatIsCorrect(
'NotValidType'
);
}

$this->validateParameterPresentInMethodSignature(
$ptr,
ltrim($paramDefinitions[1], '&'),
@@ -567,13 +582,17 @@ public function process(File $phpcsFile, $stackPtr)
$previousCommentOpenPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
$previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0);
if ($previousCommentClosePtr && $previousCommentOpenPtr) {
if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)) {
$methodName = $tokens[$stackPtr + 2]['content'];
if ($methodName !== '__construct'
&& !$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)
) {
$phpcsFile->addError('Comment block is missing', $stackPtr, 'NoCommentBlock');
return;
}
} else {
return;
}

$openParenthesisPtr = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1, $numTokens);
$closedParenthesisPtr = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr + 1, $numTokens);
$methodArguments = $this->getMethodArguments($phpcsFile, $openParenthesisPtr, $closedParenthesisPtr);
@@ -606,6 +625,7 @@ public function process(File $phpcsFile, $stackPtr)
}
}
}

$this->validateMethodParameterAnnotations(
$stackPtr,
$paramDefinitions,
@@ -643,12 +663,15 @@ private function validateFormattingConsistency(
if (isset($paramDefinition['paramName'])) {
$argumentPositions[] = strpos($paramContent, $paramDefinition['paramName']);
}

$commentPositions[] = $paramDefinition['comment']
? strrpos($paramContent, $paramDefinition['comment']) : null;
}
}

if (!$this->allParamsAligned($argumentPositions, $commentPositions)
&& !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)) {
&& !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)
) {
$phpcsFile->addError(
'Method arguments visual alignment must be consistent',
$paramPointers[0],
@@ -662,6 +685,7 @@ private function validateFormattingConsistency(
*
* @param array $argumentPositions
* @param array $commentPositions
*
* @return bool
*/
private function allParamsAligned(array $argumentPositions, array $commentPositions): bool
@@ -676,6 +700,7 @@ private function allParamsAligned(array $argumentPositions, array $commentPositi
* @param array $argumentPositions
* @param array $commentPositions
* @param array $paramDefinitions
*
* @return bool
*/
private function noneParamsAligned(array $argumentPositions, array $commentPositions, array $paramDefinitions): bool
@@ -687,9 +712,11 @@ private function noneParamsAligned(array $argumentPositions, array $commentPosit
if ($type === null) {
continue;
}

$paramName = $paramDefinitions[$index]['paramName'];
if (($argumentPosition !== strlen($type) + 1) ||
(isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1))) {
(isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1))
) {
$flag = false;
break;
}
10 changes: 10 additions & 0 deletions Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc
Original file line number Diff line number Diff line change
@@ -51,3 +51,13 @@ public function methodWithAttributeAndWithoutDocblock(string $text): string
{
return $text;
}

class ConstructorCommentNotRequired
{
private $property = false;

public function __construct()
{
$this->property = true;
}
}