Skip to content

Refactor PHPDoc search #135

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,8 @@ public function process(File $phpcsFile, $stackPtr)

$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);

$commentStartPtr = $phpcsFile->findPrevious(
[
T_WHITESPACE,
T_DOC_COMMENT_STAR,
T_DOC_COMMENT_WHITESPACE,
T_DOC_COMMENT_TAG,
T_DOC_COMMENT_STRING,
T_DOC_COMMENT_CLOSE_TAG
],
$stackPtr - 1,
null,
true,
null,
true
);

if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
$commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile);
if ($commentStartPtr === -1) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function process(File $phpcsFile, $stackPtr)
true
);

$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true);
if ($commentStartPtr === false) {
$commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile);
if ($commentStartPtr === -1) {
return;
}

Expand Down
36 changes: 36 additions & 0 deletions Magento2/Sniffs/Commenting/PHPDocFormattingValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,47 @@
*/
namespace Magento2\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;

/**
* Helper class for common DocBlock validations
*/
class PHPDocFormattingValidator
{
/**
* Finds matching PHPDoc for current pointer
*
* @param int $startPtr
* @param File $phpcsFile
* @return int
*/
public function findPHPDoc($startPtr, $phpcsFile)
{
$tokens = $phpcsFile->getTokens();

$commentStartPtr = $phpcsFile->findPrevious(
[
T_WHITESPACE,
T_DOC_COMMENT_STAR,
T_DOC_COMMENT_WHITESPACE,
T_DOC_COMMENT_TAG,
T_DOC_COMMENT_STRING,
T_DOC_COMMENT_CLOSE_TAG
],
$startPtr - 1,
null,
true,
null,
true
);

if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
return -1;
}

return $commentStartPtr;
}

/**
* Determines if the comment identified by $commentStartPtr provides additional meaning to origin at $namePtr
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Profiler
*/
class Profiler
{
const PROFILER = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getErrorList()
*/
public function getWarningList($testFile = '')
{
if ($testFile === 'ConstantsPHPDocFormattingUnitTest.1.inc') {
if ($testFile !== 'ConstantsPHPDocFormattingUnitTest.2.inc') {
return [];
}

Expand Down