Skip to content

✨ Require @return void #120

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 1 commit into from
Apr 1, 2020
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
3 changes: 3 additions & 0 deletions SymfonyCustom/Helpers/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
abstract class AbstractHelper
{
/**
* @return void
*/
private function __construct()
{
}
Expand Down
8 changes: 8 additions & 0 deletions SymfonyCustom/Helpers/FixerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class FixerHelper extends AbstractHelper
* @param File $phpcsFile
* @param int $fromPtr
* @param int $toPtr
*
* @return void
*/
public static function removeAll(File $phpcsFile, int $fromPtr, int $toPtr): void
{
Expand All @@ -37,6 +39,8 @@ public static function removeAll(File $phpcsFile, int $fromPtr, int $toPtr): voi
* @param int $fromPtr
* @param int $fromLine
* @param int $toLine
*
* @return void
*/
public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine, int $toLine): void
{
Expand All @@ -61,6 +65,8 @@ public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine,
* @param int $stackPtr
* @param int $expected
* @param int|string $found
*
* @return void
*/
public static function fixWhitespaceAfter(
File $phpcsFile,
Expand Down Expand Up @@ -92,6 +98,8 @@ public static function fixWhitespaceAfter(
* @param int $stackPtr
* @param int $expected
* @param int|string $found
*
* @return void
*/
public static function fixWhitespaceBefore(
File $phpcsFile,
Expand Down
8 changes: 7 additions & 1 deletion SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public function register(): array
}

/**
* @param File $phpcsFile The current file being checked.
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down Expand Up @@ -92,6 +94,8 @@ public function process(File $phpcsFile, $stackPtr): void
* @param int $stackPtr
* @param int $start
* @param int $end
*
* @return void
*/
public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $start, int $end): void
{
Expand Down Expand Up @@ -216,6 +220,8 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star
* @param int $stackPtr
* @param int $start
* @param int $end
*
* @return void
*/
public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start, int $end): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
107 changes: 38 additions & 69 deletions SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FunctionCommentSniff extends PEARFunctionCommentSniff
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down Expand Up @@ -68,10 +70,7 @@ public function process(File $phpcsFile, $stackPtr): void
}
} else {
// No comment but maybe a method prefix
$methodPrefixes = $phpcsFile->findFirstOnLine(
Tokens::$methodPrefixes,
$stackPtr
);
$methodPrefixes = $phpcsFile->findFirstOnLine(Tokens::$methodPrefixes, $stackPtr);

if (false !== $methodPrefixes) {
$commentStart = $methodPrefixes;
Expand Down Expand Up @@ -107,58 +106,56 @@ public function process(File $phpcsFile, $stackPtr): void
* @param int $stackPtr
* @param int|null $commentStart
* @param bool $hasComment
*
* @return void
*/
protected function processReturn(File $phpcsFile, $stackPtr, $commentStart, $hasComment = true): void
{
// Check for inheritDoc if there is comment
if ($hasComment && $this->isInheritDoc($phpcsFile, $stackPtr)) {
if (!$hasComment) {
$phpcsFile->addError('Missing @return tag in function comment', $stackPtr, 'MissingReturn');

return;
}

// Check for inheritDoc
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}

$tokens = $phpcsFile->getTokens();

// Only check for a return comment if a non-void return statement exists
if (isset($tokens[$stackPtr]['scope_opener'])) {
// Start inside the function
$start = $phpcsFile->findNext(
T_OPEN_CURLY_BRACKET,
$stackPtr,
$tokens[$stackPtr]['scope_closer']
);

for ($i = $start; $i < $tokens[$stackPtr]['scope_closer']; ++$i) {
// Skip closures
if (T_CLOSURE === $tokens[$i]['code']) {
$i = $tokens[$i]['scope_closer'];
continue;
$return = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ('@return' === $tokens[$tag]['content']) {
if (null !== $return) {
$error = 'Only 1 @return tag is allowed in a function comment';
$phpcsFile->addError($error, $tag, 'DuplicateReturn');

return;
}

// Found a return not in a closure statement
// Run the check on the first which is not only 'return;'
if (T_RETURN === $tokens[$i]['code']
&& $this->isMatchingReturn($tokens, $i)
) {
if ($hasComment) {
parent::processReturn($phpcsFile, $stackPtr, $commentStart);
} else {
// There is no doc and we need one with @return
$phpcsFile->addError(
'Missing @return tag in function comment',
$stackPtr,
'MissingReturn'
);
}
$return = $tag;
}
}

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

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int $commentStart
*
* @return void
*/
protected function processThrows(File $phpcsFile, $stackPtr, $commentStart): void
{
Expand All @@ -181,28 +178,15 @@ protected function processThrows(File $phpcsFile, $stackPtr, $commentStart): voi
}
}

if (null !== $throw) {
$exception = null;
if (T_DOC_COMMENT_STRING === $tokens[$throw + 2]['code']) {
$matches = [];
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[$throw + 2]['content'], $matches);
$exception = $matches[1];
}

if (null === $exception) {
$phpcsFile->addError(
'Exception type missing for @throws tag in function comment',
$throw,
'InvalidThrows'
);
}
}
parent::processThrows($phpcsFile, $stackPtr, $commentStart);
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int $commentStart
*
* @return void
*/
protected function processParams(File $phpcsFile, $stackPtr, $commentStart): void
{
Expand All @@ -226,21 +210,6 @@ private function isInheritDoc(File $phpcsFile, int $stackPtr): bool

$content = $phpcsFile->getTokensAsString($start, ($end - $start));

return preg_match('#{@inheritdoc}#i', $content) === 1;
}

/**
* @param array $tokens
* @param int $returnPos
*
* @return bool
*/
private function isMatchingReturn(array $tokens, int $returnPos): bool
{
do {
$returnPos++;
} while (T_WHITESPACE === $tokens[$returnPos]['code']);

return T_SEMICOLON !== $tokens[$returnPos]['code'];
return preg_match('#@inheritdoc|{@inheritdoc}#i', $content) === 1;
}
}
6 changes: 6 additions & 0 deletions SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class VariableCommentSniff extends AbstractVariableSniff
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function processMemberVar(File $phpcsFile, $stackPtr): void
{
Expand Down Expand Up @@ -115,6 +117,8 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
protected function processVariable(File $phpcsFile, $stackPtr): void
{
Expand All @@ -123,6 +127,8 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
protected function processVariableInString(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Errors/ExceptionMessageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Errors/UserDeprecatedSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Formatting/StrictComparisonSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down
4 changes: 4 additions & 0 deletions SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function register(): array
/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr): void
{
Expand Down Expand Up @@ -180,6 +182,8 @@ public function process(File $phpcsFile, $stackPtr): void
* @param File $phpcsFile
* @param int $from
* @param int $to
*
* @return void
*/
private function removeUse(File $phpcsFile, int $from, int $to): void
{
Expand Down
Loading