Skip to content

Commit

Permalink
Do not require return type for renamed trait constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 5, 2023
1 parent 2ebbb53 commit f6ca9c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Rules/Methods/MissingMethodReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$methodReflection = $node->getMethodReflection();
if ($scope->isInTrait()) {
$methodNode = $node->getOriginalNode();
$originalMethodName = $methodNode->getAttribute('originalTraitMethodName');
if ($originalMethodName === '__construct') {
return [];
}
}
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ public function testBug4758(): void
$this->analyse([__DIR__ . '/data/bug-4758.php'], []);
}

public function testBug9571(): void
{
$this->analyse([__DIR__ . '/data/bug-9571.php'], []);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9571.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug9571;

trait Foo {
public function __construct() {}
}

class HelloWorld
{
use Foo {
__construct as baseConstructor;
}

public function __construct()
{
$this->baseConstructor();
}
}

0 comments on commit f6ca9c8

Please sign in to comment.