-
Notifications
You must be signed in to change notification settings - Fork 159
#58 : Implement sniff for class properties PHPDoc formatting #140
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
Merged
magento-devops-reposync-svc
merged 16 commits into
magento:develop
from
konarshankar07:implement-sniff-for-class-properties--task-58
Sep 7, 2021
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5fd88b5
New rule added for class property PHP Doc block
konarshankar07 740410b
Fixed false positive finding
konarshankar07 97cc661
added correct formatted examples
konarshankar07 519fc72
Warning raised if class member already have meaningful description
konarshankar07 93ceb18
use of PHPDocFormattingValidator
konarshankar07 7f3a89f
Fixed static build tests
konarshankar07 705cac5
Feedback changes
konarshankar07 65d11c1
Merge branch 'develop' into implement-sniff-for-class-properties--tas…
konarshankar07 785f0c3
Fixed PHPDocFormattingValidator class not found issue
konarshankar07 17e993b
Feedback suggestions
konarshankar07 88a5e75
Feedback changes
konarshankar07 e9bbb1e
Merge branch 'develop' into implement-sniff-for-class-properties--tas…
diazwatson 7e83297
Merge branch 'develop' of github.com:magento-commerce/magento-coding-…
sivaschenko 146bfb0
Resolved test failure
konarshankar07 91418fb
Merge branch 'develop' into implement-sniff-for-class-properties--tas…
konarshankar07 83f7609
Resolved test failure
konarshankar07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
Magento2/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
namespace Magento2\Sniffs\Commenting; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
use Magento2\Helpers\Commenting\PHPDocFormattingValidator; | ||
|
||
/** | ||
* Class ClassPropertyPHPDocFormattingSniff | ||
*/ | ||
class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $ignoreTokens = [ | ||
T_PUBLIC, | ||
T_PRIVATE, | ||
T_PROTECTED, | ||
T_VAR, | ||
T_STATIC, | ||
T_WHITESPACE, | ||
]; | ||
|
||
/** | ||
* @var PHPDocFormattingValidator | ||
*/ | ||
private $PHPDocFormattingValidator; | ||
|
||
/** | ||
* Constructs an ClassPropertyPHPDocFormattingSniff. | ||
*/ | ||
public function __construct() | ||
{ | ||
$scopes = Tokens::$ooScopeTokens; | ||
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator(); | ||
$listen = [ | ||
T_VARIABLE, | ||
T_DOUBLE_QUOTED_STRING, | ||
T_HEREDOC, | ||
]; | ||
|
||
parent::__construct($scopes, $listen, true); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function processMemberVar(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($stackPtr - 1), null, true); | ||
if ($commentEnd === false | ||
|| ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG | ||
&& $tokens[$commentEnd]['code'] !== T_COMMENT) | ||
) { | ||
$phpcsFile->addWarning('Missing PHP DocBlock for class property.', $stackPtr, 'Missing'); | ||
return; | ||
} | ||
$commentStart = $tokens[$commentEnd]['comment_opener']; | ||
$foundVar = null; | ||
foreach ($tokens[$commentStart]['comment_tags'] as $tag) { | ||
if ($tokens[$tag]['content'] === '@var') { | ||
if ($foundVar !== null) { | ||
$error = 'Only one @var tag is allowed for class property declaration.'; | ||
$phpcsFile->addWarning($error, $tag, 'DuplicateVar'); | ||
} else { | ||
$foundVar = $tag; | ||
} | ||
} | ||
} | ||
|
||
if ($foundVar === null) { | ||
$error = 'Class properties must have type declaration using @var tag.'; | ||
$phpcsFile->addWarning($error, $stackPtr, 'MissingVar'); | ||
return; | ||
} | ||
|
||
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd); | ||
if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) { | ||
$error = 'Content missing for @var tag in class property declaration.'; | ||
$phpcsFile->addWarning($error, $foundVar, 'EmptyVar'); | ||
return; | ||
} | ||
|
||
// Check if class has already have meaningful description after @var tag | ||
$isShortDescriptionAfterVar = $phpcsFile->findNext( | ||
T_DOC_COMMENT_STRING, | ||
$foundVar + 4, | ||
$commentEnd, | ||
false, | ||
null, | ||
false | ||
); | ||
if ($this->PHPDocFormattingValidator->providesMeaning( | ||
$isShortDescriptionAfterVar, | ||
$commentStart, | ||
$tokens | ||
) !== true) { | ||
preg_match( | ||
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', | ||
$tokens[($foundVar + 2)]['content'], | ||
$varParts | ||
); | ||
if ($varParts[1]) { | ||
return; | ||
} | ||
$error = 'Short description duplicates class property name.'; | ||
$phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct camel casing: |
||
return; | ||
} | ||
// Check if class has already have meaningful description before @var tag | ||
$isShortDescriptionPreviousVar = $phpcsFile->findPrevious( | ||
T_DOC_COMMENT_STRING, | ||
$foundVar, | ||
$commentStart, | ||
false, | ||
null, | ||
false | ||
); | ||
if ($this->PHPDocFormattingValidator->providesMeaning( | ||
$isShortDescriptionPreviousVar, | ||
$commentStart, | ||
$tokens | ||
) !== true) { | ||
preg_match( | ||
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', | ||
$tokens[($foundVar + 2)]['content'], | ||
$varParts | ||
); | ||
if ($varParts[1]) { | ||
return; | ||
} | ||
$error = 'Short description duplicates class property name.'; | ||
$phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct camel casing: |
||
return; | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock | ||
*/ | ||
protected function processVariable(File $phpcsFile, $stackPtr) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock | ||
*/ | ||
protected function processVariableInString(File $phpcsFile, $stackPtr) | ||
{ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
if
block can be extracted into a separate method, as it basically repeats is the same as the check done for checking description before@var
tag. This way, we reduce repetition and also function size, asprocessMemeberSize()
is quite big, helping readability along the way