Skip to content

Commit aaeef68

Browse files
committed
Fix return type of constructors being checked
1 parent 6edffc8 commit aaeef68

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PHP_CodeSniffer\Sniffs\Sniff;
3737
use PHP_CodeSniffer\Files\File;
3838
use PHPCSUtils\Utils\FunctionDeclarations;
39+
use PHPCSUtils\Utils\ObjectDeclarations;
3940
use PHPCSUtils\Utils\Scopes;
4041

4142
class ReturnTypeDeclarationSniff implements Sniff
@@ -79,6 +80,14 @@ public function process(File $phpcsFile, $stackPtr): void
7980
/** @var array<int, array<string, mixed>> $tokens */
8081
$tokens = $phpcsFile->getTokens();
8182

83+
// Do not check return type for constructors.
84+
if (
85+
Scopes::isOOMethod($phpcsFile, $stackPtr)
86+
&& (ObjectDeclarations::getName($phpcsFile, $stackPtr) === '__construct')
87+
) {
88+
return;
89+
}
90+
8291
$data = FunctionDeclarations::getProperties($phpcsFile, $stackPtr);
8392
if (!$data['has_body']) {
8493
return;

tests/fixtures/return-type-declaration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ public function test3(): bool;
373373

374374
class FooAccess implements ArrayAccess
375375
{
376+
private string $x;
377+
378+
public function __construct(string $x)
379+
{
380+
if ($x === '') {
381+
return;
382+
}
383+
$this->x = $x;
384+
}
376385

377386
/**
378387
* @inheritdoc

0 commit comments

Comments
 (0)