Skip to content

PhancyPhanReturnTypes - added smell and fix for phancy returns #7

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

Closed
wants to merge 2 commits into from
Closed
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
85 changes: 81 additions & 4 deletions DatacodeStandard/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
* Parses and verifies the doc comments for functions.
*
* Same as the Squiz standard, but skips validation for @inheritDoc comments
* Also scans for phancy returns where they should be e.g. `@return array{0: string}`
*/
class FunctionCommentSniff extends SquizFunctionCommentSniff {

class FunctionCommentSniff extends SquizFunctionCommentSniff
{
/**
* Processes this test, when one of its tokens is encountered.
*
Expand All @@ -23,10 +24,11 @@ class FunctionCommentSniff extends SquizFunctionCommentSniff {
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr) {
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$find = Tokens::$methodPrefixes;
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;

$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
Expand All @@ -39,10 +41,85 @@ public function process(File $phpcsFile, $stackPtr) {

if (count($commentLines) === 3 && $commentLines[1] === '* @inheritDoc') {
return;
} else {
$this->checkFixReturnType($phpcsFile, $commentStart, $commentEnd);
}
}

parent::process($phpcsFile, $stackPtr);
}

/**
* Checks and fixes the return type in case its a fancy phan
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $commentStart The position of the comment start
* @param integer $commentEnd The position of the comment end
*
* @return void
*/
public function checkFixReturnType(File $phpcsFile, int $commentStart, int $commentEnd)
{
$tokens = $phpcsFile->getTokens();

$returnTag = $phpcsFile->findNext([T_DOC_COMMENT_TAG], $commentStart, $commentEnd, false, '@return');
$returnType = $phpcsFile->findNext([T_DOC_COMMENT_STRING], $returnTag, ($returnTag + 3));

$returnTypeText = $tokens[$returnType]['content'];

$isPhancyReturn = \str_contains($returnTypeText, 'array{') || \str_contains($returnTypeText, 'array<');

if ($isPhancyReturn) {
$this->fixPhancyType($phpcsFile, $commentStart, $commentEnd, $returnTag, $returnType);
}
}

/**
* Checks and fixes the tag type in case its a fancy phan
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $commentStart The position of the comment start
* @param integer $commentEnd The position of the comment end
* @param integer $stackPtrTag The position of the tag
* @param integer $strackPtrValue The position of the value
*
* @return void
*/
public function fixPhancyType(
File $phpcsFile,
int $commentStart,
int $commentEnd,
int $stackPtrTag,
int $strackPtrValue,
$type = 'return'
) {
$capitalisedType = \ucfirst($type);
$tokens = $phpcsFile->getTokens();
$fix = $phpcsFile->addFixableError(
"Fancy phan {$type} found at normal {$type} type in doc comment",
$strackPtrValue,
"FancyPhan{$capitalisedType}TypeFound"
);

if ($fix) {
$startStar = $phpcsFile->findPrevious([T_DOC_COMMENT_STAR], $stackPtrTag, $commentStart);
$lineSpace = $phpcsFile->findPrevious([T_DOC_COMMENT_WHITESPACE], $startStar, $startStar - 1);

$commentText = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
$commentLines = \array_map('trim', \explode("\n", $commentText));

$returnLineNo = $tokens[$strackPtrValue]['line'];
$commentStartNo = $tokens[$commentStart]['line'];

$indentCount = $tokens[$lineSpace]['length'];
$indent = \str_pad('', $indentCount, ' ');

$commentLine = $commentLines[$returnLineNo - $commentStartNo];

$phanCommentLine = \str_replace("@{$type}", "@phan-{$type}", $commentLine);

$phpcsFile->fixer->replaceToken($strackPtrValue, "array\n{$indent}{$phanCommentLine}");
}
}

}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datacodetech/phpcs-ruleset",
"version": "2.2.1",
"version": "2.2.2",
"type": "library",
"license": "MIT",
"description": "phpcs ruleset used for all PHP projects",
Expand Down