Skip to content

Commit ba1156d

Browse files
committed
Allo functions with nullable return type to return just null
1 parent 2a96e87 commit ba1156d

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
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+
## NOT RELEASED
4+
- Fix function with nullable type return and returning null (and nothing else) considered invalid.
5+
36
## 0.13.3
47
- Fixed false positive "missing argument type declaration" when param are passed by-reference.
58
- Make `ReturnTypeDeclarationSniff` compatible with PHPCS 3.3

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public function process(File $file, $position)
9292
return;
9393
}
9494

95-
$hasNullableReturn or $voidReturnCount += $nullReturnCount;
96-
9795
$this->maybeErrors(
9896
$hasNonVoidReturnType,
9997
$hasVoidReturnType,
10098
$hasNoReturnType,
99+
$hasNullableReturn,
101100
$nonVoidReturnCount,
101+
$nullReturnCount,
102102
$voidReturnCount,
103103
$file,
104104
$position
@@ -108,22 +108,31 @@ public function process(File $file, $position)
108108
/**
109109
* @param bool $hasNonVoidReturnType
110110
* @param bool $hasVoidReturnType
111+
* @param bool $hasNullableReturn
111112
* @param bool $hasNoReturnType
112113
* @param int $nonVoidReturnCount
113114
* @param int $voidReturnCount
115+
* @param int $nullReturnCount
114116
* @param File $file
115117
* @param int $position
118+
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
116119
*/
117120
private function maybeErrors(
118121
bool $hasNonVoidReturnType,
119122
bool $hasVoidReturnType,
120123
bool $hasNoReturnType,
124+
bool $hasNullableReturn,
121125
int $nonVoidReturnCount,
126+
int $nullReturnCount,
122127
int $voidReturnCount,
123128
File $file,
124129
int $position
125130
) {
126131

132+
$hasNullableReturn
133+
? $nonVoidReturnCount += $nullReturnCount
134+
: $voidReturnCount += $nullReturnCount;
135+
127136
if ($hasNonVoidReturnType && ($nonVoidReturnCount === 0 || $voidReturnCount > 0)) {
128137
$msg = 'Return type with';
129138
$file->addError(

tests/fixtures/return-type-declaration.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<?php
22
// @phpcsSniff CodeQuality.ReturnTypeDeclaration
33

4+
public function hooks(): ?array
5+
{
6+
return null;
7+
}
8+
9+
// @phpcsWarningCodeOnNextLine NoReturnType
10+
function a()
11+
{
12+
return 'x';
13+
}
14+
415
// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
516
function iReturnWrongNull(): \ArrayAccess
617
{
@@ -17,12 +28,6 @@ function c(): void
1728
return true;
1829
}
1930

20-
// @phpcsWarningCodeOnNextLine NoReturnType
21-
function a()
22-
{
23-
return 'x';
24-
}
25-
2631
function aa($foo)
2732
{
2833
return;

0 commit comments

Comments
 (0)