Skip to content

Commit

Permalink
Constructor phpDocs are inherited without remapping parameter names b…
Browse files Browse the repository at this point in the history
…y position
  • Loading branch information
ondrejmirtes committed Jan 12, 2020
1 parent 6bd522c commit ca37ebd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,18 @@ private static function resolvePhpDocBlockFromClass(
}
$methodVariants = $parentReflection->getVariants();
$positionalMethodParameterNames = [];
if (count($methodVariants) === 1) {
$lowercaseMethodName = strtolower($parentReflection->getName());
if (
count($methodVariants) === 1
&& $lowercaseMethodName !== '__construct'
&& $lowercaseMethodName !== strtolower($parentReflection->getDeclaringClass()->getName())
) {
$methodParameters = $methodVariants[0]->getParameters();
foreach ($methodParameters as $methodParameter) {
$positionalMethodParameterNames[] = $methodParameter->getName();
}
} else {
$positionalMethodParameterNames = $positionalParameterNames;
}
} else {
$traitReflection = null;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9742,6 +9742,11 @@ public function dataPhpDocInheritanceParameterRemapping(): array
return $this->gatherAssertTypes(__DIR__ . '/data/inheritdoc-parameter-remapping.php');
}

public function dataPhpDocInheritanceConstructors(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/inheritdoc-constructors.php');
}

public function dataListType(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/list-type.php');
Expand All @@ -9767,6 +9772,7 @@ public function dataListType(): array
* @dataProvider dataBug2648
* @dataProvider dataBug2740
* @dataProvider dataPhpDocInheritanceParameterRemapping
* @dataProvider dataPhpDocInheritanceConstructors
* @dataProvider dataListType
* @dataProvider dataBug2822
* @param ConstantStringType $expectedType
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/data/inheritdoc-constructors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace InheritDocConstructors;

use function PHPStan\Analyser\assertType;

class Foo
{

/**
* @param string[] $data
*/
public function __construct($data)
{
assertType('array<string>', $data);
}

}

class Bar extends Foo
{

public function __construct($name, $data)
{
parent::__construct($data);
assertType('mixed', $name);
assertType('array<string>', $data);
}

}

0 comments on commit ca37ebd

Please sign in to comment.