Skip to content
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

[PEAR\Commenting\FunctionComment] Improve check for special method and add an option #2925

Merged
merged 1 commit into from Apr 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Expand Up @@ -25,6 +25,16 @@ class FunctionCommentSniff implements Sniff
*/
public $minimumVisibility = 'private';

/**
* Array of methods which do not require a return type.
*
* @var array
*/
public $specialMethods = [
'__construct',
'__destruct',
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -135,7 +145,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)

// Skip constructor and destructor.
$methodName = $phpcsFile->getDeclarationName($stackPtr);
$isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct');
$isSpecialMethod = in_array($methodName, $this->specialMethods, true);

$return = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
Expand All @@ -150,17 +160,17 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
}
}

if ($isSpecialMethod === true) {
return;
}

if ($return !== null) {
$content = $tokens[($return + 2)]['content'];
if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Return type missing for @return tag in function comment';
$phpcsFile->addError($error, $return, 'MissingReturnType');
}
} else {
if ($isSpecialMethod === true) {
return;
}

$error = 'Missing @return tag in function comment';
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
}//end if
Expand Down
31 changes: 31 additions & 0 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc
Expand Up @@ -398,3 +398,34 @@ private function setTranslator4($a, &$b): void
{
$this->translator = $translator;
}

class Bar {
/**
* The PHP5 constructor
*
* @return
*/
public function __construct() {

}
}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[]
class Bar {
/**
* The PHP5 constructor
*/
public function __construct() {

}
}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] ignored
/**
* Should be ok
*/
public function ignored() {

}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] __construct,__destruct
Expand Up @@ -398,3 +398,34 @@ private function setTranslator4($a, &$b): void
{
$this->translator = $translator;
}

class Bar {
/**
* The PHP5 constructor
*
* @return
*/
public function __construct() {

}
}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[]
class Bar {
/**
* The PHP5 constructor
*/
public function __construct() {

}
}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] ignored
/**
* Should be ok
*/
public function ignored() {

}

// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] __construct,__destruct
Expand Up @@ -68,6 +68,8 @@ public function getErrorList()
361 => 1,
363 => 1,
364 => 1,
406 => 1,
417 => 1,
];

}//end getErrorList()
Expand Down
Expand Up @@ -67,10 +67,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)

// Skip constructor and destructor.
$methodName = $phpcsFile->getDeclarationName($stackPtr);
$isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct');
if ($isSpecialMethod === true) {
return;
}
$isSpecialMethod = in_array($methodName, $this->specialMethods, true);

if ($return !== null) {
$content = $tokens[($return + 2)]['content'];
Expand Down Expand Up @@ -181,6 +178,10 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
}//end if
}//end if
} else {
if ($isSpecialMethod === true) {
return;
}

$error = 'Missing @return tag in function comment';
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
}//end if
Expand Down
22 changes: 21 additions & 1 deletion src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc
Expand Up @@ -666,7 +666,7 @@ class Baz {
* Test
*
* @return void
* @throws E
* @throws E
*/
function myFunction() {}

Expand Down Expand Up @@ -1021,3 +1021,23 @@ public function foo($a, $b) {}
* @return mixed
*/
public function foo(mixed $a): mixed {}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[]
class Bar {
/**
* The PHP5 constructor
*/
public function __construct() {

}
}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] ignored
/**
* Should be ok
*/
public function ignored() {

}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct
Expand Up @@ -666,7 +666,7 @@ class Baz {
* Test
*
* @return void
* @throws E
* @throws E
*/
function myFunction() {}

Expand Down Expand Up @@ -1021,3 +1021,23 @@ public function foo($a, $b) {}
* @return mixed
*/
public function foo(mixed $a): mixed {}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[]
class Bar {
/**
* The PHP5 constructor
*/
public function __construct() {

}
}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] ignored
/**
* Should be ok
*/
public function ignored() {

}

// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct
Expand Up @@ -115,6 +115,7 @@ public function getErrorList()
997 => 1,
1004 => 2,
1006 => 1,
1029 => 1,
];

// Scalar type hints only work from PHP 7 onwards.
Expand Down