Skip to content

Commit 9805691

Browse files
committed
Fix bug in ReturnTypeDeclarationSniff
which caused wrong return type detection
1 parent 1e8d33e commit 9805691

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 0.7.2
4+
- Fix bug in `ReturnTypeDeclarationSniff` which caused wrong return type detection.
5+
36
## 0.7.1
47
- Exclude `NeutronStandard.MagicMethods.RiskyMagicMethod`
58
- Add `.gitattributes`

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public function process(File $file, $position)
4848

4949
list($hasNonVoidReturnType, $hasVoidReturnType, $hasNoReturnType) = $this->returnTypeInfo(
5050
$file,
51-
$position,
52-
$functionEnd
51+
$position
5352
);
5453

5554
list($nonVoidReturnCount, $voidReturnCount) = Helpers::countReturns($file, $position);
@@ -116,18 +115,20 @@ private function maybeErrors(
116115
/**
117116
* @param File $file
118117
* @param int $functionPosition
119-
* @param int $functionEnd
120118
* @return array
121119
*/
122-
private function returnTypeInfo(File $file, int $functionPosition, int $functionEnd): array
120+
private function returnTypeInfo(File $file, int $functionPosition): array
123121
{
122+
$tokens = $file->getTokens();
123+
$functionToken = $tokens[$functionPosition];
124+
124125
$returnTypeToken = $file->findNext(
125126
[T_RETURN_TYPE],
126127
$functionPosition + 3, // 3: open parenthesis, close parenthesis, colon
127-
$functionEnd - 1
128+
($functionToken['scope_opener'] ?? 0) - 1
128129
);
129130

130-
$returnType = $file->getTokens()[$returnTypeToken] ?? null;
131+
$returnType = $tokens[$returnTypeToken] ?? null;
131132
if ($returnType && $returnType['type'] !== "T_RETURN_TYPE") {
132133
$returnType = null;
133134
}

tests/fixtures/return-type-declaration.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,36 @@ function noHookCallback() {
121121

122122
class WrapperHookWrapper {
123123

124-
function filterWrapper(): bool {
124+
function filterWrapper(string $x, int $y): bool {
125125

126126
// @phpcsWarningCodeOnNextLine NoReturnType
127127
foo('x', function() {
128128
return '';
129129
});
130130

131-
add_filter('x', function() {
132-
return '';
131+
add_filter('x', function() use($x, $y) {
132+
return "$x, $y";
133133
});
134134

135135
return true;
136136
}
137137

138+
public function register()
139+
{
140+
add_filter('foo_bar', function (array $a): array {
141+
return array_merge($a, ['x' => 'y']);
142+
});
143+
144+
add_action(
145+
'foo_bar_baz',
146+
function ($x, $y) {
147+
$this->filterWrapper((string)$x, (int)$y);
148+
},
149+
10,
150+
2
151+
);
152+
}
153+
138154
// @phpcsWarningCodeOnNextLine NoReturnType
139155
function problematicMethod() {
140156
return 'x';

0 commit comments

Comments
 (0)