-
Notifications
You must be signed in to change notification settings - Fork 160
#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 2 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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
<?php | ||
namespace Magento2\Sniffs\Commenting; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; | ||
|
||
/** | ||
* Class ClassPropertyPHPDocFormattingSniff | ||
*/ | ||
class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $ignoreTokens = [ | ||
T_PUBLIC, | ||
T_PRIVATE, | ||
T_PROTECTED, | ||
T_VAR, | ||
T_STATIC, | ||
T_WHITESPACE, | ||
]; | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return int|void | ||
*/ | ||
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 class property doc comment', $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 in a class property comment'; | ||
konarshankar07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$phpcsFile->addWarning($error, $tag, 'DuplicateVar'); | ||
} else { | ||
$foundVar = $tag; | ||
} | ||
} | ||
} | ||
|
||
if ($foundVar === null) { | ||
$error = 'Missing @var tag in class property comment'; | ||
konarshankar07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$phpcsFile->addWarning($error, $commentEnd, '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 comment'; | ||
konarshankar07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$phpcsFile->addWarning($error, $foundVar, 'EmptyVar'); | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return int|void | ||
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock | ||
*/ | ||
protected function processVariable(File $phpcsFile, $stackPtr) | ||
{ | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return int|void | ||
* 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
35 changes: 35 additions & 0 deletions
35
Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc
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,35 @@ | ||
<?php | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @var Foo | ||
*/ | ||
private $_classAttribute = ''; | ||
|
||
/** | ||
* Foo | ||
*/ | ||
private $_withoutClassAttribute = ''; | ||
|
||
/** | ||
* @var Test | ||
* | ||
* Short Description | ||
*/ | ||
private $_classAttributeWithShortDescription = ''; | ||
|
||
/** | ||
* @var | ||
*/ | ||
private $_emptyClassAttributeContent = ''; | ||
|
||
|
||
/** | ||
* @var Foo | ||
* @var Bar | ||
*/ | ||
private $_multipleClassAttribute = ''; | ||
|
||
private $_missingDocBlockClassAttribute = ''; | ||
} | ||
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. Please add here positive scenarios as well. Some examples with correctly formatted blocks. |
32 changes: 32 additions & 0 deletions
32
Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.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,32 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Tests\Commenting; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ClassPropertyPHPDocFormattingUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return [ | ||
12 => 1, | ||
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. It should detect on line 13. |
||
23 => 1, | ||
30 => 1, | ||
34 => 1 | ||
]; | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.