Skip to content

Commit b06b533

Browse files
committed
Allow filters callbacks to return null on purpose
1 parent a963969 commit b06b533

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Inpsyde/Sniffs/CodeQuality/HookClosureReturnSniff.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ public function process(File $file, $position)
3434
}
3535

3636
list($functionStart, $functionEnd) = PhpcsHelpers::functionBoundaries($file, $position);
37-
if (!$functionStart < 0 || $functionEnd <= 0) {
37+
if ($functionStart < 0 || $functionEnd <= 0) {
3838
return;
3939
}
4040

41-
list($nonVoidReturnCount, $voidReturnCount) = PhpcsHelpers::countReturns($file, $position);
41+
list(
42+
$nonVoidReturnCount,
43+
$voidReturnCount,
44+
$nullReturnsCount
45+
) = PhpcsHelpers::countReturns($file, $position);
46+
47+
// Allow a filter to return null on purpose
48+
$nonVoidReturnCount += $nullReturnsCount;
4249

4350
$isFilterClosure = PhpcsHelpers::isHookClosure($file, $position, true, false);
4451

tests/fixtures/hook-closure-return.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ function() {
6969
}
7070
);
7171

72+
add_filter(
73+
'hook',
74+
function () {
75+
return null;
76+
}
77+
);
78+
7279
return true;
7380
}
7481

@@ -107,4 +114,4 @@ function() {
107114
}
108115
);
109116
}
110-
}
117+
}

0 commit comments

Comments
 (0)