From f9697c6bcfc6c80d306e982ab8754e6605c7fddd Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 13 Mar 2019 11:15:14 -0500 Subject: [PATCH] Removed unstable Annotation sniffs --- .travis.yml | 1 + .../Annotation/AnnotationFormatValidator.php | 318 ---------- .../ClassAnnotationStructureSniff.php | 124 ---- .../MethodAnnotationStructureSniff.php | 80 --- .../Annotation/MethodArgumentsSniff.php | 587 ------------------ .../Classes/ObjectInstantiationSniff.php | 4 +- .../Sniffs/Exceptions/DirectThrowSniff.php | 3 +- .../NamingConvention/ReservedWordsSniff.php | 5 +- Magento/Sniffs/PHP/DateTimeSniff.php | 4 +- .../Security/LanguageConstructSniff.php | 4 +- .../Sniffs/Strings/ExecutableRegExSniff.php | 4 +- .../ClassAnnotationStructureUnitTest.inc | 31 - .../ClassAnnotationStructureUnitTest.php | 33 - .../MethodAnnotationStructureUnitTest.inc | 309 --------- .../MethodAnnotationStructureUnitTest.php | 54 -- Magento/ruleset.xml | 13 +- 16 files changed, 27 insertions(+), 1547 deletions(-) delete mode 100644 Magento/Sniffs/Annotation/AnnotationFormatValidator.php delete mode 100644 Magento/Sniffs/Annotation/ClassAnnotationStructureSniff.php delete mode 100644 Magento/Sniffs/Annotation/MethodAnnotationStructureSniff.php delete mode 100644 Magento/Sniffs/Annotation/MethodArgumentsSniff.php delete mode 100644 Magento/Tests/Annotation/ClassAnnotationStructureUnitTest.inc delete mode 100644 Magento/Tests/Annotation/ClassAnnotationStructureUnitTest.php delete mode 100644 Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.inc delete mode 100644 Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.php diff --git a/.travis.yml b/.travis.yml index bd7a6a1c..3e4b8af3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 install: composer install --no-interaction --prefer-source script: - vendor/bin/phpunit diff --git a/Magento/Sniffs/Annotation/AnnotationFormatValidator.php b/Magento/Sniffs/Annotation/AnnotationFormatValidator.php deleted file mode 100644 index 93679614..00000000 --- a/Magento/Sniffs/Annotation/AnnotationFormatValidator.php +++ /dev/null @@ -1,318 +0,0 @@ -getTokens(); - $shortPtrEnd = $shortPtr; - for ($i = ($shortPtr + 1); $i < $commentEndPtr; $i++) { - if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { - if ($tokens[$i]['line'] === $tokens[$shortPtrEnd]['line'] + 1) { - $shortPtrEnd = $i; - } else { - break; - } - } - } - return $shortPtrEnd; - } - - /** - * Validates whether the short description has multi lines in description - * - * @param File $phpcsFile - * @param int $shortPtr - * @param int $commentEndPtr - */ - private function validateMultiLinesInShortDescription( - File $phpcsFile, - $shortPtr, - $commentEndPtr - ) { - $tokens = $phpcsFile->getTokens(); - $shortPtrEnd = $this->getShortDescriptionEndPosition( - $phpcsFile, - (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[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG - ) { - $error = 'Short description should not be in multi lines'; - $phpcsFile->addWarning($error, $shortPtrEnd + 1, 'MethodAnnotation'); - } - } - - /** - * Validates whether the spacing between short and long descriptions - * - * @param File $phpcsFile - * @param int $shortPtr - * @param int $commentEndPtr - * @param array $emptyTypeTokens - */ - private function validateSpacingBetweenShortAndLongDescriptions( - File $phpcsFile, - $shortPtr, - $commentEndPtr, - array $emptyTypeTokens - ) { - $tokens = $phpcsFile->getTokens(); - $shortPtrEnd = $this->getShortDescriptionEndPosition( - $phpcsFile, - (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[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG - ) { - $error = 'There must be exactly one blank line between lines'; - $phpcsFile->addWarning($error, $shortPtrEnd + 1, 'MethodAnnotation'); - } - if ($shortPtrEnd != $shortPtr) { - $this->validateLongDescriptionFormat($phpcsFile, $shortPtrEnd, $commentEndPtr, $emptyTypeTokens); - } else { - $this->validateLongDescriptionFormat($phpcsFile, $shortPtr, $commentEndPtr, $emptyTypeTokens); - } - } - - /** - * Validates short description format - * - * @param File $phpcsFile - * @param int $shortPtr - * @param int $stackPtr - * @param int $commentEndPtr - * @param array $emptyTypeTokens - */ - private function validateShortDescriptionFormat( - File $phpcsFile, - $shortPtr, - $stackPtr, - $commentEndPtr, - array $emptyTypeTokens - ) { - $tokens = $phpcsFile->getTokens(); - if ($tokens[$shortPtr]['line'] !== $tokens[$stackPtr]['line'] + 1) { - $error = 'No blank lines are allowed before short description'; - $phpcsFile->addWarning($error, $shortPtr, 'MethodAnnotation'); - } - if (strtolower($tokens[$shortPtr]['content']) === '{@inheritdoc}') { - $error = 'If the @inheritdoc not inline it shouldn’t have braces'; - $phpcsFile->addWarning($error, $shortPtr, 'MethodAnnotation'); - } - $shortPtrContent = $tokens[$shortPtr]['content']; - if (preg_match('/^\p{Ll}/u', $shortPtrContent) === 1) { - $error = 'Short description must start with a capital letter'; - $phpcsFile->addWarning($error, $shortPtr, 'MethodAnnotation'); - } - $this->validateNoExtraNewLineBeforeShortDescription( - $phpcsFile, - $stackPtr, - $commentEndPtr, - $emptyTypeTokens - ); - $this->validateSpacingBetweenShortAndLongDescriptions( - $phpcsFile, - $shortPtr, - $commentEndPtr, - $emptyTypeTokens - ); - $this->validateMultiLinesInShortDescription( - $phpcsFile, - $shortPtr, - $commentEndPtr - ); - } - - /** - * Validates long description format - * - * @param File $phpcsFile - * @param int $shortPtrEnd - * @param int $commentEndPtr - * @param array $emptyTypeTokens - */ - private function validateLongDescriptionFormat( - File $phpcsFile, - $shortPtrEnd, - $commentEndPtr, - array $emptyTypeTokens - ) { - $tokens = $phpcsFile->getTokens(); - $longPtr = $phpcsFile->findNext($emptyTypeTokens, $shortPtrEnd + 1, $commentEndPtr - 1, true); - if (strtolower($tokens[$longPtr]['content']) === '@inheritdoc') { - $error = '@inheritdoc imports only short description, annotation must have long description'; - $phpcsFile->addWarning($error, $longPtr, 'MethodAnnotation'); - } - if ($longPtr !== false && $tokens[$longPtr]['code'] === T_DOC_COMMENT_STRING) { - if ($tokens[$longPtr]['line'] !== $tokens[$shortPtrEnd]['line'] + 2) { - $error = 'There must be exactly one blank line between descriptions'; - $phpcsFile->addWarning($error, $longPtr, 'MethodAnnotation'); - } - if (preg_match('/^\p{Ll}/u', $tokens[$longPtr]['content']) === 1) { - $error = 'Long description must start with a capital letter'; - $phpcsFile->addWarning($error, $longPtr, 'MethodAnnotation'); - } - } - } - - /** - * Validates tags spacing format - * - * @param File $phpcsFile - * @param int $commentStartPtr - * @param array $emptyTypeTokens - */ - public function validateTagsSpacingFormat(File $phpcsFile, $commentStartPtr, array $emptyTypeTokens) - { - $tokens = $phpcsFile->getTokens(); - if (isset($tokens[$commentStartPtr]['comment_tags'][0])) { - $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 - && strtolower($commentTagPtrContent) !== '@inheritdoc' - ) { - $error = 'There must be exactly one blank line before tags'; - $phpcsFile->addWarning($error, $firstTagPtr, 'MethodAnnotation'); - } - } - } - - /** - * Validates tag grouping format - * - * @param File $phpcsFile - * @param int $commentStartPtr - */ - public function validateTagGroupingFormat(File $phpcsFile, $commentStartPtr) - { - $tokens = $phpcsFile->getTokens(); - $tagGroups = []; - $groupId = 0; - $paramGroupId = null; - foreach ($tokens[$commentStartPtr]['comment_tags'] as $position => $tag) { - if ($position > 0) { - $prevPtr = $phpcsFile->findPrevious( - T_DOC_COMMENT_STRING, - $tag - 1, - $tokens[$commentStartPtr]['comment_tags'][$position - 1] - ); - if ($prevPtr === false) { - $prevPtr = $tokens[$commentStartPtr]['comment_tags'][$position - 1]; - } - - if ($tokens[$prevPtr]['line'] !== $tokens[$tag]['line'] - 1) { - $groupId++; - } - } - - if (strtolower($tokens[$tag]['content']) === '@param') { - if ($paramGroupId !== null - && $paramGroupId !== $groupId) { - $error = 'Parameter tags must be grouped together'; - $phpcsFile->addWarning($error, $tag, 'MethodAnnotation'); - } - if ($paramGroupId === null) { - $paramGroupId = $groupId; - } - } - $tagGroups[$groupId][] = $tag; - } - } - - /** - * Validates extra newline before short description - * - * @param File $phpcsFile - * @param int $commentStartPtr - * @param int $commentEndPtr - * @param array $emptyTypeTokens - */ - private function validateNoExtraNewLineBeforeShortDescription( - File $phpcsFile, - $commentStartPtr, - $commentEndPtr, - array $emptyTypeTokens - ) { - $tokens = $phpcsFile->getTokens(); - $prevPtr = $phpcsFile->findPrevious($emptyTypeTokens, $commentEndPtr - 1, $commentStartPtr, true); - if ($tokens[$prevPtr]['line'] < ($tokens[$commentEndPtr]['line'] - 1)) { - $error = 'Additional blank lines found at end of the annotation block'; - $phpcsFile->addWarning($error, $commentEndPtr, 'MethodAnnotation'); - } - } - - /** - * Validates structure description format - * - * @param File $phpcsFile - * @param int $commentStartPtr - * @param int $shortPtr - * @param int $commentEndPtr - * @param array $emptyTypeTokens - */ - public function validateDescriptionFormatStructure( - File $phpcsFile, - $commentStartPtr, - $shortPtr, - $commentEndPtr, - array $emptyTypeTokens - ) { - $tokens = $phpcsFile->getTokens(); - 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 - && strtolower($commentTagPtrContent) !== '@inheritdoc' - ) { - $error = 'Missing short description'; - $phpcsFile->addWarning($error, $commentStartPtr, 'MethodAnnotation'); - } else { - $this->validateShortDescriptionFormat( - $phpcsFile, - (int)$shortPtr, - $commentStartPtr, - $commentEndPtr, - $emptyTypeTokens - ); - } - } else { - $this->validateShortDescriptionFormat( - $phpcsFile, - (int)$shortPtr, - $commentStartPtr, - $commentEndPtr, - $emptyTypeTokens - ); - } - } -} diff --git a/Magento/Sniffs/Annotation/ClassAnnotationStructureSniff.php b/Magento/Sniffs/Annotation/ClassAnnotationStructureSniff.php deleted file mode 100644 index 548de767..00000000 --- a/Magento/Sniffs/Annotation/ClassAnnotationStructureSniff.php +++ /dev/null @@ -1,124 +0,0 @@ -annotationFormatValidator = new AnnotationFormatValidator(); - } - - /** - * Validates whether annotation block exists for interface, abstract or final classes - * - * @param File $phpcsFile - * @param int $previousCommentClosePtr - * @param int $stackPtr - */ - private function validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists( - File $phpcsFile, - $previousCommentClosePtr, - $stackPtr - ) { - $tokens = $phpcsFile->getTokens(); - if ($tokens[$stackPtr]['type'] === 'T_CLASS') { - if ($tokens[$stackPtr - 2]['type'] === 'T_ABSTRACT' && - $tokens[$stackPtr - 4]['content'] != $tokens[$previousCommentClosePtr]['content'] - ) { - $error = 'Interface or abstract class is missing annotation block'; - $phpcsFile->addWarning($error, $stackPtr, 'ClassAnnotation'); - } - if ($tokens[$stackPtr - 2]['type'] === 'T_FINAL' && - $tokens[$stackPtr - 4]['content'] != $tokens[$previousCommentClosePtr]['content'] - ) { - $error = 'Final class is missing annotation block'; - $phpcsFile->addWarning($error, $stackPtr, 'ClassAnnotation'); - } - } - } - - /** - * Validates whether annotation block exists - * - * @param File $phpcsFile - * @param int $previousCommentClosePtr - * @param int $stackPtr - */ - private function validateAnnotationBlockExists(File $phpcsFile, $previousCommentClosePtr, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - $this->validateInterfaceOrAbstractOrFinalClassAnnotationBlockExists( - $phpcsFile, - $previousCommentClosePtr, - $stackPtr - ); - if ($tokens[$stackPtr - 2]['content'] != 'class' && $tokens[$stackPtr - 2]['content'] != 'abstract' - && $tokens[$stackPtr - 2]['content'] != 'final' - && $tokens[$stackPtr - 2]['content'] !== $tokens[$previousCommentClosePtr]['content'] - ) { - $error = 'Class is missing annotation block'; - $phpcsFile->addWarning($error, $stackPtr, 'ClassAnnotation'); - } - } - - /** - * @inheritdoc - */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0); - $this->validateAnnotationBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr); - $commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0); - if ($commentStartPtr === false) { - return; - } - $commentCloserPtr = $tokens[$commentStartPtr]['comment_closer']; - $emptyTypeTokens = [ - T_DOC_COMMENT_WHITESPACE, - T_DOC_COMMENT_STAR - ]; - $shortPtr = $phpcsFile->findNext($emptyTypeTokens, $commentStartPtr +1, $commentCloserPtr, true); - if ($shortPtr === false) { - $error = 'Annotation block is empty'; - $phpcsFile->addWarning($error, $commentStartPtr, 'MethodAnnotation'); - } else { - $this->annotationFormatValidator->validateDescriptionFormatStructure( - $phpcsFile, - $commentStartPtr, - (int) $shortPtr, - $previousCommentClosePtr, - $emptyTypeTokens - ); - } - } -} diff --git a/Magento/Sniffs/Annotation/MethodAnnotationStructureSniff.php b/Magento/Sniffs/Annotation/MethodAnnotationStructureSniff.php deleted file mode 100644 index 73356005..00000000 --- a/Magento/Sniffs/Annotation/MethodAnnotationStructureSniff.php +++ /dev/null @@ -1,80 +0,0 @@ -annotationFormatValidator = new AnnotationFormatValidator(); - } - - /** - * @inheritdoc - */ - public function register() - { - return [ - T_FUNCTION - ]; - } - - /** - * @inheritdoc - */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - $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'] ; - if (preg_match('/(?i)__construct/', $functionPtrContent)) { - return; - } - $emptyTypeTokens = [ - T_DOC_COMMENT_WHITESPACE, - T_DOC_COMMENT_STAR - ]; - $shortPtr = $phpcsFile->findNext($emptyTypeTokens, $commentStartPtr + 1, $commentCloserPtr, true); - if ($shortPtr === false) { - $error = 'Annotation block is empty'; - $phpcsFile->addWarning($error, $commentStartPtr, 'MethodAnnotation'); - } else { - $this->annotationFormatValidator->validateDescriptionFormatStructure( - $phpcsFile, - $commentStartPtr, - (int) $shortPtr, - $commentEndPtr, - $emptyTypeTokens - ); - if (empty($tokens[$commentStartPtr]['comment_tags'])) { - return; - } - $this->annotationFormatValidator->validateTagsSpacingFormat( - $phpcsFile, - $commentStartPtr, - $emptyTypeTokens - ); - $this->annotationFormatValidator->validateTagGroupingFormat($phpcsFile, $commentStartPtr); - } - } -} diff --git a/Magento/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento/Sniffs/Annotation/MethodArgumentsSniff.php deleted file mode 100644 index ac450c23..00000000 --- a/Magento/Sniffs/Annotation/MethodArgumentsSniff.php +++ /dev/null @@ -1,587 +0,0 @@ -validTokensBeforeClosingCommentTag); - } - - /** - * Validates whether comment block exists - * - * @param File $phpcsFile - * @param int $previousCommentClosePtr - * @param int $stackPtr - * @return bool - */ - private function validateCommentBlockExists(File $phpcsFile, $previousCommentClosePtr, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - for ($tempPtr = $previousCommentClosePtr + 1; $tempPtr < $stackPtr; $tempPtr++) { - if (!$this->isTokenBeforeClosingCommentTagValid($tokens[$tempPtr]['type'])) { - return false; - } - } - return true; - } - - /** - * Checks whether the parameter type is invalid - * - * @param string $type - * @return bool - */ - private function isInvalidType($type) - { - return in_array(strtolower($type), $this->invalidTypes); - } - - /** - * Get arguments from method signature - * - * @param File $phpcsFile - * @param int $openParenthesisPtr - * @param int $closedParenthesisPtr - * @return array - */ - private function getMethodArguments(File $phpcsFile, $openParenthesisPtr, $closedParenthesisPtr) - { - $tokens = $phpcsFile->getTokens(); - $methodArguments = []; - for ($i = $openParenthesisPtr; $i < $closedParenthesisPtr; $i++) { - $argumentsPtr = $phpcsFile->findNext(T_VARIABLE, $i + 1, $closedParenthesisPtr); - if ($argumentsPtr === false) { - break; - } elseif ($argumentsPtr < $closedParenthesisPtr) { - $arguments = $tokens[$argumentsPtr]['content']; - $methodArguments[] = $arguments; - $i = $argumentsPtr - 1; - } - } - return $methodArguments; - } - - /** - * Get parameters from method annotation - * - * @param array $paramDefinitions - * @return array - */ - private function getMethodParameters(array $paramDefinitions) - { - $paramName = []; - $countParams = count($paramDefinitions); - for ($i = 0; $i < $countParams; $i++) { - if (isset($paramDefinitions[$i]['paramName'])) { - $paramName[] = $paramDefinitions[$i]['paramName']; - } - } - return $paramName; - } - - /** - * Validates whether @inheritdoc without braces [@inheritdoc] exists or not - * - * @param File $phpcsFile - * @param int $previousCommentOpenPtr - * @param int $previousCommentClosePtr - */ - private function validateInheritdocAnnotationWithoutBracesExists( - File $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr - ) { - return $this->validateInheritdocAnnotationExists( - $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr, - '@inheritdoc' - ); - } - - /** - * Validates whether @inheritdoc with braces [{@inheritdoc}] exists or not - * - * @param File $phpcsFile - * @param int $previousCommentOpenPtr - * @param int $previousCommentClosePtr - */ - private function validateInheritdocAnnotationWithBracesExists( - File $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr - ) { - return $this->validateInheritdocAnnotationExists( - $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr, - '{@inheritdoc}' - ); - } - - /** - * Validates inheritdoc annotation exists - * - * @param File $phpcsFile - * @param int $previousCommentOpenPtr - * @param int $previousCommentClosePtr - * @param string $inheritdocAnnotation - * @return bool - */ - private function validateInheritdocAnnotationExists( - File $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr, - $inheritdocAnnotation - ) { - $tokens = $phpcsFile->getTokens(); - for ($ptr = $previousCommentOpenPtr; $ptr < $previousCommentClosePtr; $ptr++) { - if (strtolower($tokens[$ptr]['content']) === $inheritdocAnnotation) { - return true; - } - } - return false; - } - - /** - * Validates if annotation exists for parameter in method annotation - * - * @param File $phpcsFile - * @param int $argumentsCount - * @param int $parametersCount - * @param int $previousCommentOpenPtr - * @param int $previousCommentClosePtr - * @param int $stackPtr - */ - private function validateParameterAnnotationForArgumentExists( - File $phpcsFile, - $argumentsCount, - $parametersCount, - $previousCommentOpenPtr, - $previousCommentClosePtr, - $stackPtr - ) { - if ($argumentsCount > 0 && $parametersCount === 0) { - $inheritdocAnnotationWithoutBracesExists = $this->validateInheritdocAnnotationWithoutBracesExists( - $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr - ); - $inheritdocAnnotationWithBracesExists = $this->validateInheritdocAnnotationWithBracesExists( - $phpcsFile, - $previousCommentOpenPtr, - $previousCommentClosePtr - ); - if ($inheritdocAnnotationWithBracesExists) { - $phpcsFile->addWarning( - '{@inheritdoc} does not import parameter annotation', - $stackPtr, - 'MethodArguments' - ); - } elseif ($this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) - && !$inheritdocAnnotationWithoutBracesExists - ) { - $phpcsFile->addWarning( - 'Missing @param for argument in method annotation', - $stackPtr, - 'MethodArguments' - ); - } - } - } - - /** - * Validates whether comment block have extra the parameters listed in method annotation - * - * @param File $phpcsFile - * @param int $argumentsCount - * @param int $parametersCount - * @param int $stackPtr - */ - private function validateCommentBlockDoesnotHaveExtraParameterAnnotation( - File $phpcsFile, - $argumentsCount, - $parametersCount, - $stackPtr - ) { - if ($argumentsCount < $parametersCount && $argumentsCount > 0) { - $phpcsFile->addWarning( - 'Extra @param found in method annotation', - $stackPtr, - 'MethodArguments' - ); - } elseif ($argumentsCount > 0 && $argumentsCount != $parametersCount && $parametersCount != 0) { - $phpcsFile->addWarning( - '@param is not found for one or more params in method annotation', - $stackPtr, - 'MethodArguments' - ); - } - } - - /** - * Validates whether the argument name exists in method parameter annotation - * - * @param int $stackPtr - * @param int $ptr - * @param File $phpcsFile - * @param array $methodArguments - * @param array $paramDefinitions - */ - private function validateArgumentNameInParameterAnnotationExists( - $stackPtr, - $ptr, - File $phpcsFile, - array $methodArguments, - array $paramDefinitions - ) { - $parameterNames = $this->getMethodParameters($paramDefinitions); - if (!in_array($methodArguments[$ptr], $parameterNames)) { - $error = $methodArguments[$ptr]. ' parameter is missing in method annotation'; - $phpcsFile->addWarning($error, $stackPtr, 'MethodArguments'); - } - } - - /** - * Validates whether parameter present in method signature - * - * @param int $ptr - * @param int $paramDefinitionsArguments - * @param array $methodArguments - * @param File $phpcsFile - * @param array $paramPointers - */ - private function validateParameterPresentInMethodSignature( - $ptr, - $paramDefinitionsArguments, - array $methodArguments, - File $phpcsFile, - array $paramPointers - ) { - if (!in_array($paramDefinitionsArguments, $methodArguments)) { - $phpcsFile->addWarning( - $paramDefinitionsArguments . ' parameter is missing in method arguments signature', - $paramPointers[$ptr], - 'MethodArguments' - ); - } - } - - /** - * Validates whether the parameters are in order or not in method annotation - * - * @param array $paramDefinitions - * @param array $methodArguments - * @param File $phpcsFile - * @param array $paramPointers - */ - private function validateParameterOrderIsCorrect( - array $paramDefinitions, - array $methodArguments, - File $phpcsFile, - array $paramPointers - ) { - $parameterNames = $this->getMethodParameters($paramDefinitions); - $paramDefinitionsCount = count($paramDefinitions); - for ($ptr = 0; $ptr < $paramDefinitionsCount; $ptr++) { - if (isset($methodArguments[$ptr]) && isset($parameterNames[$ptr]) - && in_array($methodArguments[$ptr], $parameterNames) - ) { - if ($methodArguments[$ptr] != $parameterNames[$ptr]) { - $phpcsFile->addWarning( - $methodArguments[$ptr].' parameter is not in order', - $paramPointers[$ptr], - 'MethodArguments' - ); - } - } - } - } - - /** - * Validate whether duplicate annotation present in method annotation - * - * @param int $stackPtr - * @param array $paramDefinitions - * @param array $paramPointers - * @param File $phpcsFile - * @param array $methodArguments - */ - private function validateDuplicateAnnotationDoesnotExists( - $stackPtr, - array $paramDefinitions, - array $paramPointers, - File $phpcsFile, - array $methodArguments - ) { - $argumentsCount = count($methodArguments); - $parametersCount = count($paramPointers); - if ($argumentsCount <= $parametersCount && $argumentsCount > 0) { - $duplicateParameters = []; - $countParams = count($paramDefinitions); - for ($i = 0; $i < $countParams; $i++) { - if (isset($paramDefinitions[$i]['paramName'])) { - $parameterContent = $paramDefinitions[$i]['paramName']; - for ($j = $i + 1; $j < $countParams; $j++) { - if (isset($paramDefinitions[$j]['paramName']) - && $parameterContent === $paramDefinitions[$j]['paramName'] - ) { - $duplicateParameters[] = $parameterContent; - } - } - } - } - foreach ($duplicateParameters as $value) { - $phpcsFile->addWarning( - $value . ' duplicate found in method annotation', - $stackPtr, - 'MethodArguments' - ); - } - } - } - - /** - * Validate parameter annotation format is correct or not - * - * @param int $ptr - * @param File $phpcsFile - * @param array $methodArguments - * @param array $paramDefinitions - * @param array $paramPointers - */ - private function validateParameterAnnotationFormatIsCorrect( - $ptr, - File $phpcsFile, - array $methodArguments, - array $paramDefinitions, - array $paramPointers - ) { - switch (count($paramDefinitions)) { - case 0: - $phpcsFile->addWarning( - 'Missing both type and parameter', - $paramPointers[$ptr], - 'MethodArguments' - ); - break; - case 1: - if (preg_match('/^\$.*/', $paramDefinitions[0])) { - $phpcsFile->addWarning( - 'Type is not specified', - $paramPointers[$ptr], - 'MethodArguments' - ); - } - break; - case 2: - if ($this->isInvalidType($paramDefinitions[0])) { - $phpcsFile->addWarning( - $paramDefinitions[0].' is not a valid PHP type', - $paramPointers[$ptr], - 'MethodArguments' - ); - } - $this->validateParameterPresentInMethodSignature( - $ptr, - ltrim($paramDefinitions[1], '&'), - $methodArguments, - $phpcsFile, - $paramPointers - ); - break; - default: - if (preg_match('/^\$.*/', $paramDefinitions[0])) { - $phpcsFile->addWarning( - 'Type is not specified', - $paramPointers[$ptr], - 'MethodArguments' - ); - if ($this->isInvalidType($paramDefinitions[0])) { - $phpcsFile->addWarning( - $paramDefinitions[0].' is not a valid PHP type', - $paramPointers[$ptr], - 'MethodArguments' - ); - } - } - break; - } - } - - /** - * Validate method parameter annotations - * - * @param int $stackPtr - * @param array $paramDefinitions - * @param array $paramPointers - * @param File $phpcsFile - * @param array $methodArguments - * @param int $previousCommentOpenPtr - * @param int $previousCommentClosePtr - */ - private function validateMethodParameterAnnotations( - $stackPtr, - array $paramDefinitions, - array $paramPointers, - File $phpcsFile, - array $methodArguments, - $previousCommentOpenPtr, - $previousCommentClosePtr - ) { - $argumentCount = count($methodArguments); - $paramCount = count($paramPointers); - $this->validateParameterAnnotationForArgumentExists( - $phpcsFile, - $argumentCount, - $paramCount, - $previousCommentOpenPtr, - $previousCommentClosePtr, - $stackPtr - ); - $this->validateCommentBlockDoesnotHaveExtraParameterAnnotation( - $phpcsFile, - $argumentCount, - $paramCount, - $stackPtr - ); - $this->validateDuplicateAnnotationDoesnotExists( - $stackPtr, - $paramDefinitions, - $paramPointers, - $phpcsFile, - $methodArguments - ); - $this->validateParameterOrderIsCorrect( - $paramDefinitions, - $methodArguments, - $phpcsFile, - $paramPointers - ); - $countArguments = count($methodArguments); - for ($ptr = 0; $ptr < $countArguments; $ptr++) { - $tokens = $phpcsFile->getTokens(); - if (isset($paramPointers[$ptr])) { - $this->validateArgumentNameInParameterAnnotationExists( - $stackPtr, - $ptr, - $phpcsFile, - $methodArguments, - $paramDefinitions - ); - $paramContent = $tokens[$paramPointers[$ptr]+2]['content']; - $paramContentExplode = explode(' ', $paramContent); - $this->validateParameterAnnotationFormatIsCorrect( - $ptr, - $phpcsFile, - $methodArguments, - $paramContentExplode, - $paramPointers - ); - } - } - } - - /** - * @inheritdoc - */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - $numTokens = count($tokens); - $previousCommentOpenPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr-1, 0); - $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr-1, 0); - if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)) { - $phpcsFile->addWarning('Comment block is missing', $stackPtr, 'MethodArguments'); - 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); - $paramPointers = $paramDefinitions = []; - for ($tempPtr = $previousCommentOpenPtr; $tempPtr < $previousCommentClosePtr; $tempPtr++) { - if (strtolower($tokens[$tempPtr]['content']) === '@param') { - $paramPointers[] = $tempPtr; - $paramAnnotationParts = explode(' ', $tokens[$tempPtr+2]['content']); - if (count($paramAnnotationParts) === 1) { - if ((preg_match('/^\$.*/', $paramAnnotationParts[0]))) { - $paramDefinitions[] = [ - 'type' => null, - 'paramName' => rtrim(ltrim($tokens[$tempPtr+2]['content'], '&'), ',') - ]; - } else { - $paramDefinitions[] = [ - 'type' => $tokens[$tempPtr+2]['content'], - 'paramName' => null - ]; - } - } else { - $paramDefinitions[] = [ - 'type' => $paramAnnotationParts[0], - 'paramName' => rtrim(ltrim($paramAnnotationParts[1], '&'), ',') - ]; - } - } - } - $this->validateMethodParameterAnnotations( - $stackPtr, - $paramDefinitions, - $paramPointers, - $phpcsFile, - $methodArguments, - $previousCommentOpenPtr, - $previousCommentClosePtr - ); - } -} diff --git a/Magento/Sniffs/Classes/ObjectInstantiationSniff.php b/Magento/Sniffs/Classes/ObjectInstantiationSniff.php index fbdb4993..a4ee57e2 100644 --- a/Magento/Sniffs/Classes/ObjectInstantiationSniff.php +++ b/Magento/Sniffs/Classes/ObjectInstantiationSniff.php @@ -16,10 +16,12 @@ class ObjectInstantiationSniff implements Sniff /** * String representation of warning. * + * phpcs:disable Magento.Files.LineLength.MaxExceeded + * * @var string */ - // phpcs:ignore Magento.Files.LineLength.MaxExceeded protected $warningMessage = 'Direct %s object instantiation is discouraged in Magento. Use dependency injection or factories instead.'; + //phpcs:enable /** * Warning violation code. diff --git a/Magento/Sniffs/Exceptions/DirectThrowSniff.php b/Magento/Sniffs/Exceptions/DirectThrowSniff.php index 0491ad32..96b4ee54 100644 --- a/Magento/Sniffs/Exceptions/DirectThrowSniff.php +++ b/Magento/Sniffs/Exceptions/DirectThrowSniff.php @@ -15,9 +15,10 @@ class DirectThrowSniff implements Sniff { /** * String representation of warning. + * phpcs:disable Generic.Files.LineLength.TooLong */ - // phpcs:ignore Generic.Files.LineLength.TooLong protected $warningMessage = 'Direct throw of generic Exception is discouraged. Use context specific instead.'; + //phpcs:enable /** * Warning violation code. diff --git a/Magento/Sniffs/NamingConvention/ReservedWordsSniff.php b/Magento/Sniffs/NamingConvention/ReservedWordsSniff.php index 69c9c793..9e72bb85 100644 --- a/Magento/Sniffs/NamingConvention/ReservedWordsSniff.php +++ b/Magento/Sniffs/NamingConvention/ReservedWordsSniff.php @@ -15,9 +15,8 @@ class ReservedWordsSniff implements Sniff { /** * The following words cannot be used to name a class, interface or trait, - * and they are also prohibited from being used in namespaces. - * - * @link http://php.net/manual/en/reserved.other-reserved-words.php + * and they are also prohibited from being used in namespaces + * http://php.net/manual/en/reserved.other-reserved-words.php * * @var string[] */ diff --git a/Magento/Sniffs/PHP/DateTimeSniff.php b/Magento/Sniffs/PHP/DateTimeSniff.php index 0292f91f..497327e0 100644 --- a/Magento/Sniffs/PHP/DateTimeSniff.php +++ b/Magento/Sniffs/PHP/DateTimeSniff.php @@ -16,10 +16,12 @@ class DateTimeSniff implements Sniff /** * String representation of warning. * + * phpcs:disable Magento.Files.LineLength.MaxExceeded + * * @var string */ - // phpcs:ignore Magento.Files.LineLength.MaxExceeded protected $warningMessage = 'Overcomplicated Date/Time handling. Use \Magento\Framework\Stdlib\DateTime\TimezoneInterface instead.'; + // phpcs:enable /** * Warning violation code. diff --git a/Magento/Sniffs/Security/LanguageConstructSniff.php b/Magento/Sniffs/Security/LanguageConstructSniff.php index ddadce96..38496dc5 100644 --- a/Magento/Sniffs/Security/LanguageConstructSniff.php +++ b/Magento/Sniffs/Security/LanguageConstructSniff.php @@ -23,10 +23,12 @@ class LanguageConstructSniff implements Sniff /** * String representation of backtick error. * + * phpcs:disable Magento.Files.LineLength.MaxExceeded + * * @var string */ - // phpcs:ignore Magento.Files.LineLength.MaxExceeded protected $errorMessageBacktick = 'Incorrect usage of back quote string constant. Back quotes should be always inside strings.'; + //phpcs:enable /** * Backtick violation code. diff --git a/Magento/Sniffs/Strings/ExecutableRegExSniff.php b/Magento/Sniffs/Strings/ExecutableRegExSniff.php index 5df0e8f2..18e5fef7 100644 --- a/Magento/Sniffs/Strings/ExecutableRegExSniff.php +++ b/Magento/Sniffs/Strings/ExecutableRegExSniff.php @@ -17,10 +17,12 @@ class ExecutableRegExSniff implements Sniff /** * String representation of error. * + * phpcs:disable Magento.Files.LineLength.MaxExceeded + * * @var string */ - // phpcs:ignore Magento.Files.LineLength.MaxExceeded protected $errorMessage = "Possible executable regular expression in %s. Make sure that the pattern doesn't contain 'e' modifier"; + //phpcs:enable /** * Error violation code. diff --git a/Magento/Tests/Annotation/ClassAnnotationStructureUnitTest.inc b/Magento/Tests/Annotation/ClassAnnotationStructureUnitTest.inc deleted file mode 100644 index 46b46037..00000000 --- a/Magento/Tests/Annotation/ClassAnnotationStructureUnitTest.inc +++ /dev/null @@ -1,31 +0,0 @@ - 1, - 26 => 1, - ]; - } -} diff --git a/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.inc b/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.inc deleted file mode 100644 index 6d8f0d60..00000000 --- a/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.inc +++ /dev/null @@ -1,309 +0,0 @@ -productVisibility = $productVisibility; - } - - /** - * block description - * - * {@inheritdoc} - * - * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection - * @return void - */ - public function construct(AbstractDb $collection) - { - /** @var */ - $collection->setVisibility($this->productVisibility->getVisibleInCatalogIds()); - } - - /** - * Move category - * - * - * @param int $parentId new parent category id - * - * @return $this - * @throws \Magento\Framework\Exception\LocalizedException|\Exception - */ - public function move($parentId) - { - /** - * Validate new parent category id. (category model is used for backward - * compatibility in event params) - */ - try { - $this->categoryRepository->get($parentId, $this->getStoreId()); - } catch (NoSuchEntityException $e) { - throw new \Magento\Framework\Exception\LocalizedException( - __('Sorry, but we can\'t find the new parent category you selected.'), - $e - ); - } - return true; - } - - /** - * Block for short description - * - * This a long description {@inheritdoc} consists more lines as part of the long description - * on multi line. - * - * @param int $store - * - * - */ - public function getProductListDefaultSortBy26032($store) - { - return $store; - } - - /** - * - * - * - */ - public function getProductListDefaultSortBy2632() - { - } - - /** - * Block for short description - * - * This a long description {@inheritdoc} consists more lines as part of the long description - * on multi line. - * - * @param int $store - * - * - * - */ - public function getProductListDefaultSortBy2002($store) - { - return $store; - } - - /** - * - * block for short description - * - * @param int $store - * @return int - */ - public function getProductListDefaultSortBy3002($store) - { - return $store; - } - - /** - * Block for short description - * - * @see consists more lines as part of the long description - * on multi line. - * - * @param string $store - * @param string $foo - */ - public function getProductListDefaultSortBy12($store, $foo) - { - return $store === $foo; - } - - /** - * Block for short description - * - * {@inheritdoc} - * - * @param string $store - * @param string $foo - */ - public function getProductListDefaultSort2($store, $foo) - { - return $store === $foo; - } - - /** - * Block for short description - * - * a long description {@inheritdoc} consists more lines as part of the long description - * on multi line. - * - * @param string $store - * @param string $foo - */ - public function getProductListDefault($store, $foo) - { - return $store === $foo; - } - - /** - * Retrieve custom options - * - * @param ProductOptionInterface $productOption - * - * @return array - */ - protected function getCustomOptions(ProductOptionInterface $productOption) - { - if ($productOption - && $productOption->getExtensionAttributes() - && $productOption->getExtensionAttributes()->getCustomOptions() - ) { - return $productOption->getExtensionAttributes() - ->getCustomOptions(); - } - return []; - } - - /** - * This is the summary for a DocBlock. - * - * This is the description for a DocBlock. This text may contain - * multiple lines and even some _markdown_. - * * Markdown style lists function too - * * Just try this out once - * The section after the description contains the tags; which provide - * structured meta-data concerning the given element. - * - * @param int $example This is an example function/method parameter description. - * @param string $example2 This is a second example. - * - */ - public function getProductListDefaultSortBy2($example, $example2) - { - return $example === $example2; - } - - /** - * Returns the content of the tokens from the specified start position in - * the token stack for the specified length. - * - * @param int $start - * @param int $length - * - * @return string The token contents. - */ - public function getProductListDefaultSortBy($start, $length) - { - return $start === $length; - } - - /** - * Some text about this step/method returns the content of the tokens the token stack for the specified length - * - * @param string $name - * @param string $folder - * - * @see this file - * @When I create a file called :name in :folder - */ - public function getProductListDefaultSortBy222($name, $folder) - { - return $name === $folder; - } - - public function setExtensionAs(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) - { - return $this->_setExtensionAttributes($extensionAttributes); - } - - /** - * - * short description - * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes - * @return mixed - */ - public function setEn(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) - { - return $this->_setExtensionAttributes($extensionAttributes); - } - - /** - * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes - * @return mixed - */ - public function setExtenw(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) - { - return $this->_setExtensionAttributes($extensionAttributes); - } - - /** - * - * Short description - * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes - * @return mixed - */ - public function setExff(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) - { - return $this->_setExtensionAttributes($extensionAttributes); - } - - /** - * {@inheritdoc} - * - * @param int $start - * @param int $length - * - * @return string The token contents. - */ - public function getProductSortBy($start, $length) - { - return $start === $length; - } -} diff --git a/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.php b/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.php deleted file mode 100644 index 3744ec6c..00000000 --- a/Magento/Tests/Annotation/MethodAnnotationStructureUnitTest.php +++ /dev/null @@ -1,54 +0,0 @@ - 1, - 18 => 1, - 30 => 1, - 36 => 1, - 45 => 2, - 47 => 1, - 55 => 1, - 63 => 1, - 80 => 1, - 111 => 1, - 117 => 1, - 136 => 1, - 144 => 2, - 184 => 1, - 227 => 1, - 235 => 1, - 268 => 2, - 269 => 1, - 277 => 1, - 278 => 1, - 288 => 1, - 289 => 1, - 298 => 1, - ]; - } -} diff --git a/Magento/ruleset.xml b/Magento/ruleset.xml index 6c659ec2..107006d2 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -434,17 +434,24 @@ - + 5 warning - + + 5 + warning + + + 0 + + 5 warning - + 5 warning