Skip to content

Commit c5d0308

Browse files
committed
Invalid Void Return Type
#12
1 parent c0c5dc6 commit c5d0308

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

Inpsyde/PhpcsHelpers.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,22 @@ 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+
if (!$followedBySemicolon) {
505+
return false;
506+
}
507+
508+
return true;
494509
}
495510

496511
/**
@@ -510,7 +525,18 @@ public static function isNullReturn(File $file, int $returnPosition): bool
510525
$nextToReturn = $file->findNext([T_WHITESPACE], $returnPosition, null, true, null, true);
511526
$nextToReturnType = $tokens[$nextToReturn]['code'] ?? '';
512527

513-
return $nextToReturnType === T_NULL;
528+
if ($nextToReturnType !== T_NULL) {
529+
return false;
530+
}
531+
532+
$returnPosition = $nextToReturn + 1;
533+
$followedBySemicolon = ($tokens[$returnPosition]['code'] ?? '') === T_SEMICOLON;
534+
535+
if (!$followedBySemicolon) {
536+
return false;
537+
}
538+
539+
return true;
514540
}
515541

516542
/**

tests/fixtures/return-type-declaration.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ 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 gen(string $content): \Generator
67+
{
6168
$line = strtok($content, "\n");
6269
while ($line !== false) {
6370
$line = strtok("\n");
@@ -66,7 +73,8 @@ function gen(string $content): \Generator {
6673
}
6774

6875
// @phpcsErrorCodeOnNextLine GeneratorReturnTypeWithoutYield
69-
function genNoYield(string $content): \Generator {
76+
function genNoYield(string $content): \Generator
77+
{
7078
$line = strtok($content, "\n");
7179
while ($line !== false) {
7280
$line = strtok("\n");
@@ -77,7 +85,8 @@ function genNoYield(string $content): \Generator {
7785
}
7886

7987
// @phpcsWarningCodeOnNextLine NoGeneratorReturnType
80-
function yieldNoGen(string $content) {
88+
function yieldNoGen(string $content)
89+
{
8190
$line = strtok($content, "\n");
8291
while ($line !== false) {
8392
$line = strtok("\n");
@@ -86,7 +95,8 @@ function yieldNoGen(string $content) {
8695
}
8796

8897
// @phpcsErrorCodeOnNextLine IncorrectReturnTypeForGenerator
89-
function yieldWrongReturn(string $content): int {
98+
function yieldWrongReturn(string $content): int
99+
{
90100
$line = strtok($content, "\n");
91101
while ($line !== false) {
92102
$line = strtok("\n");
@@ -96,7 +106,8 @@ function yieldWrongReturn(string $content): int {
96106
return 1;
97107
}
98108

99-
function yieldIteratorReturn(string $content): \Iterator {
109+
function yieldIteratorReturn(string $content): \Iterator
110+
{
100111
$line = strtok($content, "\n");
101112
while ($line !== false) {
102113
$line = strtok("\n");
@@ -107,9 +118,10 @@ function yieldIteratorReturn(string $content): \Iterator {
107118
}
108119

109120

110-
function genFrom(): \Generator {
121+
function genFrom(): \Generator
122+
{
111123

112-
$gen = function(int $x): int {
124+
$gen = function (int $x): int {
113125
if ($x < 0) {
114126
return 0;
115127
}
@@ -126,12 +138,13 @@ function genFrom(): \Generator {
126138
}
127139

128140
// @phpcsWarningCodeOnNextLine InvalidGeneratorManyReturns
129-
function genMultiReturn(): \Generator {
141+
function genMultiReturn(): \Generator
142+
{
130143
if (defined('MEH_MEH')) {
131144
return 1;
132145
}
133146

134-
yield from [1,2];
147+
yield from [1, 2];
135148

136149
if (defined('MEH')) {
137150
return 1;
@@ -369,7 +382,7 @@ public function iReturnALotOfStuff() // @phpcsWarningCodeOnThisLine NoReturnType
369382
}
370383

371384
// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
372-
public function iReturnWrongNull() : \ArrayAccess
385+
public function iReturnWrongNull(): \ArrayAccess
373386
{
374387
if (rand(1, 4) > 2) {
375388
return null;

0 commit comments

Comments
 (0)