Skip to content

Commit a963969

Browse files
committed
Considr comments when checking func return
1 parent d1a3748 commit a963969

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

Inpsyde/PhpcsHelpers.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,11 @@ public static function countReturns(File $file, int $functionPosition): array
465465
}
466466

467467
if ($tokens[$i]['code'] === T_RETURN && !$scopeClosers->count()) {
468-
PhpcsHelpers::isVoidReturn($file, $i) ? $voidReturnCount++ : $nonVoidReturnCount++;
469-
PhpcsHelpers::isNullReturn($file, $i) and $nullReturnCount++;
468+
$void = PhpcsHelpers::isVoidReturn($file, $i);
469+
$null = PhpcsHelpers::isNullReturn($file, $i);
470+
$void and $voidReturnCount++;
471+
$null and $nullReturnCount++;
472+
(!$void && !$null) and $nonVoidReturnCount++;
470473
}
471474
}
472475

@@ -476,9 +479,10 @@ public static function countReturns(File $file, int $functionPosition): array
476479
/**
477480
* @param File $file
478481
* @param int $returnPosition
482+
* @param bool $includeNull
479483
* @return bool
480484
*/
481-
public static function isVoidReturn(File $file, int $returnPosition): bool
485+
public static function isVoidReturn(File $file, int $returnPosition, $includeNull = false): bool
482486
{
483487
$tokens = $file->getTokens();
484488

@@ -487,21 +491,12 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
487491
}
488492

489493
$returnPosition++;
490-
$nextToReturn = $file->findNext([T_WHITESPACE], $returnPosition, null, true, null, true);
491-
$nextToReturnType = $tokens[$nextToReturn]['code'] ?? '';
492-
493-
if ($nextToReturnType === T_SEMICOLON) {
494-
return true;
495-
}
496-
497-
if ($nextToReturnType !== T_NULL) {
498-
return false;
499-
}
494+
$exclude = Tokens::$emptyTokens;
495+
$includeNull and $exclude[] = T_NULL;
500496

501-
$returnPosition = $nextToReturn + 1;
502-
$followedBySemicolon = ($tokens[$returnPosition]['code'] ?? '') === T_SEMICOLON;
497+
$nextToReturn = $file->findNext($exclude, $returnPosition, null, true, null, true);
503498

504-
return $followedBySemicolon;
499+
return ($tokens[$nextToReturn]['code'] ?? '') === T_SEMICOLON;
505500
}
506501

507502
/**
@@ -511,16 +506,9 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
511506
*/
512507
public static function isNullReturn(File $file, int $returnPosition): bool
513508
{
514-
$tokens = $file->getTokens();
515-
516-
if (($tokens[$returnPosition]['code'] ?? '') !== T_RETURN) {
517-
return false;
518-
}
519-
520-
$returnPosition++;
521-
$nextToReturn = $file->findNext([T_WHITESPACE, T_NULL], $returnPosition, null, true, null, true);
522-
523-
return ($tokens[$nextToReturn]['code'] ?? '') === T_SEMICOLON;
509+
return
510+
!self::isVoidReturn($file, $returnPosition, false)
511+
&& self::isVoidReturn($file, $returnPosition, true);
524512
}
525513

526514
/**

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ public function process(File $file, $position)
7979
return;
8080
}
8181

82-
if ($hasNullableReturn) {
83-
$voidReturnCount -= $nullReturnCount;
84-
}
82+
$hasNullableReturn or $voidReturnCount += $nullReturnCount;
8583

8684
$this->maybeErrors(
8785
$hasNonVoidReturnType,

tests/fixtures/return-type-declaration.php

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

4+
// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
5+
function iReturnWrongNull(): \ArrayAccess
6+
{
7+
if (rand(1, 4) > 2) {
8+
return null;
9+
}
10+
11+
return new \ArrayObject();
12+
}
13+
414
// @phpcsErrorCodeOnNextLine IncorrectVoidReturnType
515
function c(): void
616
{
@@ -67,6 +77,10 @@ function hh(): void {
6777
return null;
6878
}
6979

80+
function hhComment(): void {
81+
return /* I return void */ ;
82+
}
83+
7084
function gen(string $content): \Generator
7185
{
7286
$line = strtok($content, "\n");

0 commit comments

Comments
 (0)