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

Upgrade PSR2 -> PSR12 for ControlStructureSpacing #416

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
10 changes: 6 additions & 4 deletions Magento2/Helpers/Commenting/PHPDocFormattingValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ public function hasDeprecatedWellFormatted($commentStartPtr, $tokens)
}
$seePtr = $this->getTagPosition('@see', $commentStartPtr, $tokens);
if ($seePtr === -1) {
if (preg_match(
"/This [a-zA-Z]* will be removed in version \d.\d.\d without replacement/",
$tokens[$deprecatedPtr + 2]['content']
)) {
if (
preg_match(
"/This [a-zA-Z]* will be removed in version \d.\d.\d without replacement/",
$tokens[$deprecatedPtr + 2]['content']
)
) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ public static function resetTestFile()
public static function usesPhp8NameTokens()
{
$version = Helper::getVersion();
if ((\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
if (
(\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
&& \version_compare($version, '3.5.7', '<') === true)
|| \version_compare($version, '4.0.0', '>=') === true
) {
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Helpers/PHPCSUtils/Utils/Conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static function getCondition(File $phpcsFile, $stackPtr, $types = [], $fi
}

foreach ($conditions as $ptr => $type) {
if (isset($tokens[$ptr]) === true
if (
isset($tokens[$ptr]) === true
&& \in_array($type, $types, true) === true
) {
// We found a token with the required type.
Expand Down
36 changes: 24 additions & 12 deletions Magento2/Helpers/PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public static function getProperties(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$arrowOpenClose = self::getArrowFunctionOpenClose($phpcsFile, $stackPtr);

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| ($tokens[$stackPtr]['code'] !== \T_FUNCTION
&& $tokens[$stackPtr]['code'] !== \T_CLOSURE
&& $arrowOpenClose === false)
Expand Down Expand Up @@ -169,7 +170,8 @@ public static function getProperties(File $phpcsFile, $stackPtr)
break;
}

if ($tokens[$i]['type'] === 'T_NULLABLE'
if (
$tokens[$i]['type'] === 'T_NULLABLE'
// Handle nullable tokens in PHPCS < 2.8.0.
|| (\defined('T_NULLABLE') === false && $tokens[$i]['code'] === \T_INLINE_THEN)
// Handle nullable tokens with arrow functions in PHPCS 2.8.0 - 2.9.0.
Expand Down Expand Up @@ -242,14 +244,16 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| isset(Collections::arrowFunctionTokensBC()[$tokens[$stackPtr]['code']]) === false
|| \strtolower($tokens[$stackPtr]['content']) !== 'fn'
) {
return false;
}

if ($tokens[$stackPtr]['type'] === 'T_FN'
if (
$tokens[$stackPtr]['type'] === 'T_FN'
&& isset($tokens[$stackPtr]['scope_closer']) === true
&& \version_compare(Helper::getVersion(), '3.5.4', '>') === true
) {
Expand Down Expand Up @@ -304,7 +308,8 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
true
);

if ($arrow === false
if (
$arrow === false
|| ($tokens[$arrow]['code'] !== \T_DOUBLE_ARROW && $tokens[$arrow]['type'] !== 'T_FN_ARROW')
) {
return false;
Expand All @@ -315,11 +320,13 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
$lastEndToken = null;

for ($scopeCloser = ($arrow + 1); $scopeCloser < $phpcsFile->numTokens; $scopeCloser++) {
if (isset(self::$arrowFunctionEndTokens[$tokens[$scopeCloser]['code']]) === true
if (
isset(self::$arrowFunctionEndTokens[$tokens[$scopeCloser]['code']]) === true
// BC for misidentified ternary else in some PHPCS versions.
&& ($tokens[$scopeCloser]['code'] !== \T_COLON || $inTernary === false)
) {
if ($lastEndToken !== null
if (
$lastEndToken !== null
&& $tokens[$scopeCloser]['code'] === \T_CLOSE_PARENTHESIS
&& $tokens[$scopeCloser]['parenthesis_opener'] < $arrow
) {
Expand All @@ -338,7 +345,8 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
}
}

if (isset($tokens[$scopeCloser]['scope_closer']) === true
if (
isset($tokens[$scopeCloser]['scope_closer']) === true
&& $tokens[$scopeCloser]['code'] !== \T_INLINE_ELSE
&& $tokens[$scopeCloser]['code'] !== \T_END_HEREDOC
&& $tokens[$scopeCloser]['code'] !== \T_END_NOWDOC
Expand Down Expand Up @@ -488,7 +496,8 @@ public static function getParameters(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$arrowOpenClose = self::getArrowFunctionOpenClose($phpcsFile, $stackPtr);

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| ($tokens[$stackPtr]['code'] !== \T_FUNCTION
&& $tokens[$stackPtr]['code'] !== \T_CLOSURE
&& $tokens[$stackPtr]['code'] !== \T_USE
Expand All @@ -500,7 +509,8 @@ public static function getParameters(File $phpcsFile, $stackPtr)
if ($tokens[$stackPtr]['code'] === \T_USE) {
// This will work PHPCS 3.x/4.x cross-version without much overhead.
$opener = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($opener === false
if (
$opener === false
|| $tokens[$opener]['code'] !== \T_OPEN_PARENTHESIS
|| UseStatements::isClosureUse($phpcsFile, $stackPtr) === false
) {
Expand Down Expand Up @@ -732,13 +742,15 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr)
return false;
}

if ($tokens[$stackPtr]['type'] === 'T_FN'
if (
$tokens[$stackPtr]['type'] === 'T_FN'
&& isset($tokens[$stackPtr]['scope_closer']) === true
) {
return true;
}

if (isset(Collections::arrowFunctionTokensBC()[$tokens[$stackPtr]['code']]) === false
if (
isset(Collections::arrowFunctionTokensBC()[$tokens[$stackPtr]['code']]) === false
|| \strtolower($tokens[$stackPtr]['content']) !== 'fn'
) {
return false;
Expand Down
15 changes: 10 additions & 5 deletions Magento2/Helpers/PHPCSUtils/Utils/ObjectDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static function getName(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| ($tokens[$stackPtr]['code'] === \T_ANON_CLASS || $tokens[$stackPtr]['code'] === \T_CLOSURE)
) {
return null;
Expand All @@ -79,15 +80,17 @@ public static function getName(File $phpcsFile, $stackPtr)
/*
* BC: Work-around JS ES6 classes not being tokenized as T_CLASS in PHPCS < 3.0.0.
*/
if (isset($phpcsFile->tokenizerType)
if (
isset($phpcsFile->tokenizerType)
&& $phpcsFile->tokenizerType === 'JS'
&& $tokenCode === \T_STRING
&& $tokens[$stackPtr]['content'] === 'class'
) {
$tokenCode = \T_CLASS;
}

if ($tokenCode !== \T_FUNCTION
if (
$tokenCode !== \T_FUNCTION
&& $tokenCode !== \T_CLASS
&& $tokenCode !== \T_INTERFACE
&& $tokenCode !== \T_TRAIT
Expand All @@ -97,7 +100,8 @@ public static function getName(File $phpcsFile, $stackPtr)
);
}

if ($tokenCode === \T_FUNCTION
if (
$tokenCode === \T_FUNCTION
&& \strtolower($tokens[$stackPtr]['content']) !== 'function'
) {
// This is a function declared without the "function" keyword.
Expand Down Expand Up @@ -323,7 +327,8 @@ private static function findNames(File $phpcsFile, $stackPtr, $keyword, $allowed
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| isset($allowedFor[$tokens[$stackPtr]['code']]) === false
|| isset($tokens[$stackPtr]['scope_opener']) === false
) {
Expand Down
6 changes: 4 additions & 2 deletions Magento2/Helpers/PHPCSUtils/Utils/Parentheses.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public static function getOwner(File $phpcsFile, $stackPtr)
* {@internal As the 'parenthesis_owner' index is only set on parentheses, we didn't need to do any
* input validation before, but now we do.}
*/
if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| ($tokens[$stackPtr]['code'] !== \T_OPEN_PARENTHESIS
&& $tokens[$stackPtr]['code'] !== \T_CLOSE_PARENTHESIS)
) {
Expand All @@ -97,7 +98,8 @@ public static function getOwner(File $phpcsFile, $stackPtr)
$skip[\T_BITWISE_AND] = \T_BITWISE_AND;

$prevNonEmpty = $phpcsFile->findPrevious($skip, ($stackPtr - 1), null, true);
if ($prevNonEmpty !== false
if (
$prevNonEmpty !== false
&& (isset(self::$extraParenthesesOwners[$tokens[$prevNonEmpty]['code']])
// Possibly an arrow function.
|| FunctionDeclarations::isArrowFunction($phpcsFile, $prevNonEmpty) === true)
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Helpers/PHPCSUtils/Utils/TextStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static function getCompleteTextString(File $phpcsFile, $stackPtr, $stripQ
);
}

if ($tokens[$stackPtr]['code'] === \T_CONSTANT_ENCAPSED_STRING
if (
$tokens[$stackPtr]['code'] === \T_CONSTANT_ENCAPSED_STRING
|| $tokens[$stackPtr]['code'] === \T_DOUBLE_QUOTED_STRING
) {
$prev = $phpcsFile->findPrevious(\T_WHITESPACE, ($stackPtr - 1), null, true);
Expand Down
9 changes: 6 additions & 3 deletions Magento2/Helpers/PHPCSUtils/Utils/UseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public static function getType(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]) === false
if (
isset($tokens[$stackPtr]) === false
|| $tokens[$stackPtr]['code'] !== \T_USE
) {
throw new RuntimeException('$stackPtr must be of type T_USE');
Expand All @@ -59,7 +60,8 @@ public static function getType(File $phpcsFile, $stackPtr)
}

$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($prev !== false && $tokens[$prev]['code'] === \T_CLOSE_PARENTHESIS
if (
$prev !== false && $tokens[$prev]['code'] === \T_CLOSE_PARENTHESIS
&& Parentheses::isOwnerIn($phpcsFile, $prev, \T_CLOSURE) === true
) {
return 'closure';
Expand Down Expand Up @@ -245,7 +247,8 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
// Only when either at the start of the statement or at the start of a new sub within a group.
if ($start === true && $fixedType === false) {
$content = \strtolower($tokens[$i]['content']);
if ($content === 'function'
if (
$content === 'function'
|| $content === 'const'
) {
$type = $content;
Expand Down
27 changes: 18 additions & 9 deletions Magento2/Sniffs/Annotation/AnnotationFormatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ private function validateMultiLinesInShortDescription(
$commentEndPtr
);
$shortPtrEndContent = $tokens[$shortPtrEnd]['content'];
if (preg_match('/^[a-z]/', $shortPtrEndContent)
if (
preg_match('/^[a-z]/', $shortPtrEndContent)
&& $shortPtrEnd != $shortPtr
&& !preg_match('/\bSee\b/', $shortPtrEndContent)
&& $tokens[$shortPtr]['line'] + 1 === $tokens[$shortPtrEnd]['line']
Expand Down Expand Up @@ -89,7 +90,8 @@ private function validateSpacingBetweenShortAndLongDescriptions(
$commentEndPtr
);
$shortPtrEndContent = $tokens[$shortPtrEnd]['content'];
if (preg_match('/^[A-Z]/', $shortPtrEndContent)
if (
preg_match('/^[A-Z]/', $shortPtrEndContent)
&& !preg_match('/\bSee\b/', $shortPtrEndContent)
&& $tokens[$shortPtr]['line'] + 1 === $tokens[$shortPtrEnd]['line']
&& $tokens[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG
Expand Down Expand Up @@ -199,7 +201,8 @@ public function validateTagsSpacingFormat(File $phpcsFile, int $commentStartPtr,
$firstTagPtr = $tokens[$commentStartPtr]['comment_tags'][0];
$commentTagPtrContent = $tokens[$firstTagPtr]['content'];
$prevPtr = $phpcsFile->findPrevious($emptyTypeTokens, $firstTagPtr - 1, $commentStartPtr, true);
if ($tokens[$firstTagPtr]['line'] !== $tokens[$prevPtr]['line'] + 2
if (
$tokens[$firstTagPtr]['line'] !== $tokens[$prevPtr]['line'] + 2
&& strtolower($commentTagPtrContent) !== '@inheritdoc'
) {
$error = 'There must be exactly one blank line before tags';
Expand Down Expand Up @@ -237,8 +240,10 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr)
}

if (strtolower($tokens[$tag]['content']) === '@param') {
if ($paramGroupId !== null
&& $paramGroupId !== $groupId) {
if (
$paramGroupId !== null
&& $paramGroupId !== $groupId
) {
$error = 'Parameter tags must be grouped together';
$phpcsFile->addError($error, $tag, 'MethodAnnotation');
}
Expand Down Expand Up @@ -271,8 +276,10 @@ public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr)
}
}

if (!$this->allTagsAligned($actualPositions)
&& !$this->noneTagsAligned($actualPositions, $noAlignmentPositions)) {
if (
!$this->allTagsAligned($actualPositions)
&& !$this->noneTagsAligned($actualPositions, $noAlignmentPositions)
) {
$phpcsFile->addError(
'Tags visual alignment must be consistent',
$stackPtr,
Expand Down Expand Up @@ -343,11 +350,13 @@ public function validateDescriptionFormatStructure(
array $emptyTypeTokens
): void {
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$commentStartPtr]['comment_tags'][0])
if (
isset($tokens[$commentStartPtr]['comment_tags'][0])
) {
$commentTagPtr = $tokens[$commentStartPtr]['comment_tags'][0];
$commentTagPtrContent = $tokens[$commentTagPtr]['content'];
if ($tokens[$shortPtr]['code'] !== T_DOC_COMMENT_STRING
if (
$tokens[$shortPtr]['code'] !== T_DOC_COMMENT_STRING
&& strtolower($commentTagPtrContent) !== '@inheritdoc'
) {
$error = 'Missing short description';
Expand Down
21 changes: 14 additions & 7 deletions Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ private function validateParameterAnnotationForArgumentExists(
$stackPtr,
'InheritDoc'
);
} elseif ($this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)
} elseif (
$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)
&& !$inheritdocAnnotationWithoutBracesExists
) {
$phpcsFile->addError(
Expand Down Expand Up @@ -352,7 +353,8 @@ private function validateParameterOrderIsCorrect(
$parameterNames = $this->getMethodParameters($paramDefinitions);
$paramDefinitionsCount = count($paramDefinitions);
for ($ptr = 0; $ptr < $paramDefinitionsCount; $ptr++) {
if (isset($methodArguments[$ptr]) && isset($parameterNames[$ptr])
if (
isset($methodArguments[$ptr]) && isset($parameterNames[$ptr])
&& in_array($methodArguments[$ptr], $parameterNames)
) {
if ($methodArguments[$ptr] != $parameterNames[$ptr]) {
Expand Down Expand Up @@ -390,7 +392,8 @@ private function validateDuplicateAnnotationDoesnotExists(
if (isset($paramDefinition['paramName'])) {
$parameterContent = $paramDefinition['paramName'];
foreach (array_slice($paramDefinitions, $i + 1) as $nextParamDefinition) {
if (isset($nextParamDefinition['paramName'])
if (
isset($nextParamDefinition['paramName'])
&& $parameterContent === $nextParamDefinition['paramName']
) {
$duplicateParameters[] = $parameterContent;
Expand Down Expand Up @@ -647,8 +650,10 @@ private function validateFormattingConsistency(
? strrpos($paramContent, $paramDefinition['comment']) : null;
}
}
if (!$this->allParamsAligned($argumentPositions, $commentPositions)
&& !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)) {
if (
!$this->allParamsAligned($argumentPositions, $commentPositions)
&& !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)
) {
$phpcsFile->addError(
'Method arguments visual alignment must be consistent',
$paramPointers[0],
Expand Down Expand Up @@ -688,8 +693,10 @@ private function noneParamsAligned(array $argumentPositions, array $commentPosit
continue;
}
$paramName = $paramDefinitions[$index]['paramName'];
if (($argumentPosition !== strlen($type) + 1) ||
(isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1))) {
if (
($argumentPosition !== strlen($type) + 1) ||
(isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1))
) {
$flag = false;
break;
}
Expand Down
Loading