-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52969a1
commit 2ebbb53
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassMethodNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClassMethodNode> | ||
*/ | ||
class ConstructorReturnTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassMethodNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
$methodNode = $node->getOriginalNode(); | ||
if ($scope->isInTrait()) { | ||
$originalMethodName = $methodNode->getAttribute('originalTraitMethodName'); | ||
if ( | ||
$originalMethodName === '__construct' | ||
&& $methodNode->returnType !== null | ||
) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf('Original constructor of trait %s has a return type.', $scope->getTraitReflection()->getDisplayName())) | ||
->identifier('constructor.returnType') | ||
->nonIgnorable() | ||
->build(), | ||
]; | ||
} | ||
} | ||
if (!$classReflection->hasConstructor()) { | ||
return []; | ||
} | ||
|
||
$constructorReflection = $classReflection->getConstructor(); | ||
$methodReflection = $node->getMethodReflection(); | ||
if ($methodReflection->getName() !== $constructorReflection->getName()) { | ||
return []; | ||
} | ||
|
||
if ($methodNode->returnType === null) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf('Constructor of class %s has a return type.', $classReflection->getDisplayName())) | ||
->identifier('constructor.returnType') | ||
->nonIgnorable() | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
tests/PHPStan/Rules/Methods/ConstructorReturnTypeRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ConstructorReturnTypeRule> | ||
*/ | ||
class ConstructorReturnTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ConstructorReturnTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/constructor-return-type.php'], [ | ||
[ | ||
'Constructor of class ConstructorReturnType\Bar has a return type.', | ||
17, | ||
], | ||
[ | ||
'Constructor of class ConstructorReturnType\UsesFooTrait has a return type.', | ||
26, | ||
], | ||
[ | ||
'Original constructor of trait ConstructorReturnType\BarTrait has a return type.', | ||
35, | ||
], | ||
]); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
tests/PHPStan/Rules/Methods/data/constructor-return-type.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace ConstructorReturnType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
} | ||
|
||
class Bar | ||
{ | ||
|
||
public function __construct(): void | ||
{ | ||
} | ||
|
||
} | ||
|
||
trait FooTrait | ||
{ | ||
|
||
public function __construct(): void | ||
{ | ||
} | ||
|
||
} | ||
|
||
trait BarTrait | ||
{ | ||
|
||
public function __construct(): void | ||
{ | ||
} | ||
|
||
} | ||
|
||
class UsesFooTrait | ||
{ | ||
use FooTrait; | ||
} | ||
|
||
class RenamesBarTrait | ||
{ | ||
use BarTrait { | ||
__construct as baseConstructor; | ||
} | ||
} |