Skip to content

Commit d1a3748

Browse files
authored
Merge pull request #14 from widoz/12-missing-return-type
Fix wrong detection of void return type
2 parents 0a011d2 + 40ebfc0 commit d1a3748

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

Inpsyde/PhpcsHelpers.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,18 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
490490
$nextToReturn = $file->findNext([T_WHITESPACE], $returnPosition, null, true, null, true);
491491
$nextToReturnType = $tokens[$nextToReturn]['code'] ?? '';
492492

493-
return in_array($nextToReturnType, [T_SEMICOLON, T_NULL], true);
493+
if ($nextToReturnType === T_SEMICOLON) {
494+
return true;
495+
}
496+
497+
if ($nextToReturnType !== T_NULL) {
498+
return false;
499+
}
500+
501+
$returnPosition = $nextToReturn + 1;
502+
$followedBySemicolon = ($tokens[$returnPosition]['code'] ?? '') === T_SEMICOLON;
503+
504+
return $followedBySemicolon;
494505
}
495506

496507
/**
@@ -507,10 +518,9 @@ public static function isNullReturn(File $file, int $returnPosition): bool
507518
}
508519

509520
$returnPosition++;
510-
$nextToReturn = $file->findNext([T_WHITESPACE], $returnPosition, null, true, null, true);
511-
$nextToReturnType = $tokens[$nextToReturn]['code'] ?? '';
521+
$nextToReturn = $file->findNext([T_WHITESPACE, T_NULL], $returnPosition, null, true, null, true);
512522

513-
return $nextToReturnType === T_NULL;
523+
return ($tokens[$nextToReturn]['code'] ?? '') === T_SEMICOLON;
514524
}
515525

516526
/**

tests/fixtures/return-type-declaration.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,18 @@ function g(): bool
5757
return false;
5858
}
5959

60-
function gen(string $content): \Generator {
60+
// @phpcsErrorCodeOnNextLine IncorrectVoidReturnType
61+
function h(): void
62+
{
63+
return null === true;
64+
}
65+
66+
function hh(): void {
67+
return null;
68+
}
69+
70+
function gen(string $content): \Generator
71+
{
6172
$line = strtok($content, "\n");
6273
while ($line !== false) {
6374
$line = strtok("\n");
@@ -66,7 +77,8 @@ function gen(string $content): \Generator {
6677
}
6778

6879
// @phpcsErrorCodeOnNextLine GeneratorReturnTypeWithoutYield
69-
function genNoYield(string $content): \Generator {
80+
function genNoYield(string $content): \Generator
81+
{
7082
$line = strtok($content, "\n");
7183
while ($line !== false) {
7284
$line = strtok("\n");
@@ -77,7 +89,8 @@ function genNoYield(string $content): \Generator {
7789
}
7890

7991
// @phpcsWarningCodeOnNextLine NoGeneratorReturnType
80-
function yieldNoGen(string $content) {
92+
function yieldNoGen(string $content)
93+
{
8194
$line = strtok($content, "\n");
8295
while ($line !== false) {
8396
$line = strtok("\n");
@@ -86,7 +99,8 @@ function yieldNoGen(string $content) {
8699
}
87100

88101
// @phpcsErrorCodeOnNextLine IncorrectReturnTypeForGenerator
89-
function yieldWrongReturn(string $content): int {
102+
function yieldWrongReturn(string $content): int
103+
{
90104
$line = strtok($content, "\n");
91105
while ($line !== false) {
92106
$line = strtok("\n");
@@ -96,7 +110,8 @@ function yieldWrongReturn(string $content): int {
96110
return 1;
97111
}
98112

99-
function yieldIteratorReturn(string $content): \Iterator {
113+
function yieldIteratorReturn(string $content): \Iterator
114+
{
100115
$line = strtok($content, "\n");
101116
while ($line !== false) {
102117
$line = strtok("\n");
@@ -107,9 +122,10 @@ function yieldIteratorReturn(string $content): \Iterator {
107122
}
108123

109124

110-
function genFrom(): \Generator {
125+
function genFrom(): \Generator
126+
{
111127

112-
$gen = function(int $x): int {
128+
$gen = function (int $x): int {
113129
if ($x < 0) {
114130
return 0;
115131
}
@@ -126,12 +142,13 @@ function genFrom(): \Generator {
126142
}
127143

128144
// @phpcsWarningCodeOnNextLine InvalidGeneratorManyReturns
129-
function genMultiReturn(): \Generator {
145+
function genMultiReturn(): \Generator
146+
{
130147
if (defined('MEH_MEH')) {
131148
return 1;
132149
}
133150

134-
yield from [1,2];
151+
yield from [1, 2];
135152

136153
if (defined('MEH')) {
137154
return 1;
@@ -369,7 +386,7 @@ public function iReturnALotOfStuff() // @phpcsWarningCodeOnThisLine NoReturnType
369386
}
370387

371388
// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
372-
public function iReturnWrongNull() : \ArrayAccess
389+
public function iReturnWrongNull(): \ArrayAccess
373390
{
374391
if (rand(1, 4) > 2) {
375392
return null;

0 commit comments

Comments
 (0)