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
9 changes: 7 additions & 2 deletions Magento2/Helpers/Assert.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright 2023 Adobe
* All Rights Reserved.
*/

declare(strict_types=1);

namespace Magento2\Helpers;
Expand All @@ -21,13 +23,15 @@ class Assert
*
* @param File $phpcsFile
* @param int $stackPtr
*
* @return bool
*/
public static function isBuiltinFunctionCall(File $phpcsFile, int $stackPtr): bool
{
$tokens = $phpcsFile->getTokens();
$nextPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextPtr === false
if (
$nextPtr === false
|| $tokens[$nextPtr]['code'] !== \T_OPEN_PARENTHESIS
|| isset($tokens[$nextPtr]['parenthesis_owner'])
) {
Expand All @@ -36,7 +40,8 @@ public static function isBuiltinFunctionCall(File $phpcsFile, int $stackPtr): bo

$prevPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($prevPtr !== false) {
if (isset(Collections::objectOperators()[$tokens[$prevPtr]['code']])
if (
isset(Collections::objectOperators()[$tokens[$prevPtr]['code']])
|| $tokens[$prevPtr]['code'] === \T_NEW
) {
return false;
Expand Down
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function process(File $phpcsFile, $stackPtr)

do {
$commentEndPtr = $phpcsFile->findPrevious($tokensToFind, $commentEndPtr - 1);
if ($commentEndPtr !== false
if (
$commentEndPtr !== false
&& $tokens[$commentEndPtr]['code'] === \T_ATTRIBUTE_END
&& isset($tokens[$commentEndPtr]['attribute_opener'])
) {
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
12 changes: 8 additions & 4 deletions Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_FUNCTION &&
strpos($phpcsFile->getDeclarationName($stackPtr), 'around') === 0) {
if (
$tokens[$stackPtr]['code'] === T_FUNCTION &&
strpos($phpcsFile->getDeclarationName($stackPtr), 'around') === 0
) {
return;
}

// Ignore empty constructor function blocks when using property promotion
if ($tokens[$stackPtr]['code'] === T_FUNCTION &&
if (
$tokens[$stackPtr]['code'] === T_FUNCTION &&
strpos($phpcsFile->getDeclarationName($stackPtr), '__construct') === 0 &&
count($phpcsFile->getMethodParameters($stackPtr)) > 0 &&
array_reduce($phpcsFile->getMethodParameters($stackPtr), static function ($result, $methodParam) {
return $result && isset($methodParam['property_visibility']);
}, true)) {
}, true)
) {

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken($i, '');
}

if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
if (
$tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
) {
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
false
);

if ($this->PHPDocFormattingValidator->providesMeaning(
$shortDescriptionAfterVarPosition,
$commentStart,
$tokens
) !== true) {
if (
$this->PHPDocFormattingValidator->providesMeaning(
$shortDescriptionAfterVarPosition,
$commentStart,
$tokens
) !== true
) {
preg_match(
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i',
$tokens[($varAnnotationPosition + 2)]['content'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr]['code'] !== T_CONST
if (
$tokens[$stackPtr]['code'] !== T_CONST
&& !($tokens[$stackPtr]['content'] === 'define' && $tokens[$stackPtr + 1]['code'] === T_OPEN_PARENTHESIS)
) {
return;
Expand Down
6 changes: 4 additions & 2 deletions Magento2/Sniffs/Exceptions/DirectThrowSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function process(File $phpcsFile, $stackPtr)
$useStatementValue = $this->getFullClassNameAndAlias($tokens, $key, $endOfUse);
//we safely consider use statement has alias will not be a direct exception class
if (empty($useStatementValue['alias'])) {
if (substr($useStatementValue['name'], 0, strlen($exceptionString)) !== $exceptionString
if (
substr($useStatementValue['name'], 0, strlen($exceptionString)) !== $exceptionString
&& substr($useStatementValue['name'], -strlen($exceptionString)) === $exceptionString
&& $useStatementValue['name'] !== $exceptionString
) {
Expand All @@ -65,7 +66,8 @@ public function process(File $phpcsFile, $stackPtr)
}
}
}
if (($tokens[$posOfException]['content'] === 'Exception' && !$customExceptionFound)
if (
($tokens[$posOfException]['content'] === 'Exception' && !$customExceptionFound)
|| $fullExceptionString['name'] === '\Exception'
) {
$phpcsFile->addWarning(
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Sniffs/Html/HtmlBindingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function process(File $phpcsFile, $stackPtr)
$htmlBindings[] = trim($magentoBinding);
}
foreach ($htmlBindings as $htmlBinding) {
if (!preg_match('/^[0-9\\\'\"]/ims', $htmlBinding)
if (
!preg_match('/^[0-9\\\'\"]/ims', $htmlBinding)
&& !preg_match('/UnsanitizedHtml(\(.*?\))*?$/', $htmlBinding)
) {
$phpcsFile->addError(
Expand Down
6 changes: 4 additions & 2 deletions Magento2/Sniffs/Legacy/EscapeMethodsOnBlockClassSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function process(File $phpcsFile, $stackPtr)
}
}

if ($tokens[$objectPtr]['code'] !== T_VARIABLE
if (
$tokens[$objectPtr]['code'] !== T_VARIABLE
|| $tokens[$objectPtr]['content'] !== '$block'
) {
return;
Expand All @@ -70,7 +71,8 @@ public function process(File $phpcsFile, $stackPtr)
}
}

if ($tokens[$methodPtr]['code'] !== T_STRING
if (
$tokens[$methodPtr]['code'] !== T_STRING
|| !isset(self::ESCAPER_METHODS[$tokens[$methodPtr]['content']])
) {
return;
Expand Down
7 changes: 5 additions & 2 deletions Magento2/Sniffs/Less/ClassNamingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (T_WHITESPACE !== $tokens[$stackPtr - 1]['code']
if (
T_WHITESPACE !== $tokens[$stackPtr - 1]['code']
&& !in_array(
$tokens[$stackPtr - 1]['content'],
[
Expand All @@ -67,7 +68,9 @@ public function process(File $phpcsFile, $stackPtr)
);
}

if (strlen($className) > 1 && strpos($className, self::STRING_HELPER_CLASSES_PREFIX, 2) !== false
if (
strlen($className) > 1
&& strpos($className, self::STRING_HELPER_CLASSES_PREFIX, 2) !== false
&& !str_starts_with($className, 'admin__')
) {
$phpcsFile->addError(
Expand Down
15 changes: 10 additions & 5 deletions Magento2/Sniffs/Less/CommentLevelsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ((T_STRING !== $tokens[$stackPtr]['code'])
if (
(T_STRING !== $tokens[$stackPtr]['code'])
|| (self::COMMENT_STRING !== $tokens[$stackPtr]['content'])
|| (1 === $tokens[$stackPtr]['line'])
) {
Expand All @@ -65,7 +66,8 @@ public function process(File $phpcsFile, $stackPtr)
$textInSameLine = $phpcsFile->findPrevious([T_STRING, T_STYLE], $stackPtr - 1);

// is inline comment
if ((false !== $textInSameLine)
if (
(false !== $textInSameLine)
&& ($tokens[$textInSameLine]['line'] === $tokens[$stackPtr]['line'])
) {
$this->validateInlineComment($phpcsFile, $stackPtr, $tokens);
Expand Down Expand Up @@ -164,7 +166,8 @@ private function checkNthLevelComment(File $phpcsFile, $stackPtr, array $tokens)
return $correct;
}

if (($tokens[$nextLine]['content'] !== TokenizerSymbolsInterface::NEW_LINE)
if (
($tokens[$nextLine]['content'] !== TokenizerSymbolsInterface::NEW_LINE)
|| ($tokens[$nextLine + 1]['content'] !== TokenizerSymbolsInterface::NEW_LINE)
) {
return $correct;
Expand All @@ -178,7 +181,8 @@ private function checkNthLevelComment(File $phpcsFile, $stackPtr, array $tokens)
continue;
}

if (($tokens[$commentLinePtr - 1]['content'] === TokenizerSymbolsInterface::NEW_LINE)
if (
($tokens[$commentLinePtr - 1]['content'] === TokenizerSymbolsInterface::NEW_LINE)
&& ($tokens[$commentLinePtr - 2]['content'] === TokenizerSymbolsInterface::NEW_LINE)
) {
$correct = true;
Expand All @@ -198,7 +202,8 @@ private function checkNthLevelComment(File $phpcsFile, $stackPtr, array $tokens)
*/
private function validateCommentLevel(File $phpcsFile, int $stackPtr, array $tokens): void
{
if ($tokens[$stackPtr + 2]['content'] !== 'magento_import' &&
if (
$tokens[$stackPtr + 2]['content'] !== 'magento_import' &&
!in_array(
$tokens[$stackPtr + 1]['content'],
[
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Sniffs/Less/ImportantPropertySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function process(File $phpcsFile, $stackPtr)
// Will be implemented in MAGETWO-49778
//$phpcsFile->addWarning('!important is used', $stackPtr, '!ImportantIsUsed');

if (($tokens[$stackPtr + 1]['content'] === 'important')
if (
($tokens[$stackPtr + 1]['content'] === 'important')
&& ($tokens[$stackPtr - 1]['content'] !== TokenizerSymbolsInterface::WHITESPACE)
) {
$phpcsFile->addError('Space before !important is missing', $stackPtr, 'NoSpace');
Expand Down
3 changes: 2 additions & 1 deletion Magento2/Sniffs/Less/IndentationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function process(File $phpcsFile, $stackPtr)
}

$expectedIndent = ($indentLevel * $this->indent);
if (!($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false)
if (
!($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false)
&& ($foundIndent !== $expectedIndent)
&& (!in_array($tokens[$i + 1]['code'], $this->styleCodesToSkip))
) {
Expand Down
Loading
Loading