Skip to content

Commit af1cad0

Browse files
fix return types
1 parent ce7b33b commit af1cad0

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Travelopia-WordPress/Sniffs/Functions/FunctionReturnTypeSniff.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use PHP_CodeSniffer\Files\File;
1111
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use PHP_CodeSniffer\Util\Tokens;
1213

1314
/**
1415
* Sniff to check if functions have a return type.
@@ -45,10 +46,26 @@ public function process( File $phpcsFile, $stackPtr ): void
4546
return;
4647
}
4748

48-
// Get return type.
49-
$next_return_type = $phpcsFile->findNext( [ T_COLON ], $stackPtr );
49+
// Find the closing parenthesis of the function parameters.
50+
$open_parenthesis = $phpcsFile->findNext( [ T_OPEN_PARENTHESIS ], $stackPtr );
5051

51-
if ( ! is_int( $next_return_type ) || $tokens[ $next_return_type ]['line'] !== $tokens[ $stackPtr ]['line'] ) {
52+
if ( false === $open_parenthesis || ! isset( $tokens[ $open_parenthesis ]['parenthesis_closer'] ) ) {
53+
return;
54+
}
55+
56+
$close_parenthesis = $tokens[ $open_parenthesis ]['parenthesis_closer'];
57+
58+
// Find the opening brace of the function body (or semicolon for abstract/interface methods).
59+
$scope_opener = $phpcsFile->findNext( array_merge( Tokens::$scopeOpeners, [ T_SEMICOLON ] ), $close_parenthesis );
60+
61+
if ( false === $scope_opener ) {
62+
return;
63+
}
64+
65+
// Check if there's a colon between the closing parenthesis and the scope opener.
66+
$return_type_colon = $phpcsFile->findNext( [ T_COLON ], $close_parenthesis, $scope_opener );
67+
68+
if ( false === $return_type_colon ) {
5269
$phpcsFile->addWarningOnLine(
5370
'Functions must have a return type.',
5471
$tokens[ $stackPtr ]['line'],

php-cs-fixer/Fixers/CommentPunctuationFixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private function fixDocBlockComment( Tokens $tokens, int $index ): void
120120
// Check if this DocBlock contains WordPress template headers.
121121
if ( preg_match( '/\* (Template Name|Template Post Type|Plugin Name|Theme Name|Author|Version|Description|Text Domain|Domain Path|Requires at least|Requires PHP|License|License URI|Tags):[^\n]+\.\s*$/im', $content ) ) {
122122
// Remove trailing period from WordPress headers.
123-
$newContent = preg_replace( '/(\* (?:Template Name|Template Post Type|Plugin Name|Theme Name|Author|Version|Description|Text Domain|Domain Path|Requires at least|Requires PHP|License|License URI|Tags):[^\n]+)\.\s*$/im', '$1', $content );
123+
$newContent = preg_replace( '/(\* (?:Template Name|Template Post Type|Plugin Name|Theme Name|Author|Version|Description|Text Domain|Domain Path|Requires at least|Requires PHP|License|License URI|Tags):[^\n]+)\.\s*$/im', '$1', $content );
124124
$tokens[ $index ] = new Token( [ T_DOC_COMMENT, $newContent ] );
125125
}
126126
}

php-cs-fixer/TravelopiaFixersConfig.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public static function getRules(): array
9595
// Nullable type declaration.
9696
'nullable_type_declaration_for_default_null_value' => true,
9797

98+
// Add void return type to functions that don't return anything.
99+
'void_return' => true,
100+
98101
// Array syntax.
99102
'array_syntax' => [
100103
'syntax' => 'short',

0 commit comments

Comments
 (0)