Skip to content

Fix issue in method return type override annotation on same class or from trait#173

Merged
ondrejmirtes merged 1 commit intophpstan:masterfrom
Korbeil:fix/trait-method-typehint-override-with-method-annotation
Apr 6, 2020
Merged

Fix issue in method return type override annotation on same class or from trait#173
ondrejmirtes merged 1 commit intophpstan:masterfrom
Korbeil:fix/trait-method-typehint-override-with-method-annotation

Conversation

@Korbeil
Copy link
Copy Markdown
Contributor

@Korbeil Korbeil commented Apr 2, 2020

Related phpstan/phpstan#1301 and phpstan/phpstan#3142

Trying to reproduce & fix theses issues:

The idea here is to override method return type when a method tag exists on same class.

@ondrejmirtes
Copy link
Copy Markdown
Member

Hi, it's not a regression and the fix isn't going to be trivial I think.

@ondrejmirtes
Copy link
Copy Markdown
Member

BTW this is the piece of code that allows overriding method signature with @method above classes and interfaces:

if ($includingAnnotations && $this->annotationsMethodsClassReflectionExtension->hasMethod($classReflection, $methodReflection->getName())) {
$hierarchyDistances = $classReflection->getClassHierarchyDistances();
$annotationMethod = $this->annotationsMethodsClassReflectionExtension->getMethod($classReflection, $methodReflection->getName());
if (!isset($hierarchyDistances[$annotationMethod->getDeclaringClass()->getName()])) {
throw new \PHPStan\ShouldNotHappenException();
}
if (!isset($hierarchyDistances[$methodReflection->getDeclaringClass()->getName()])) {
throw new \PHPStan\ShouldNotHappenException();
}
if ($hierarchyDistances[$annotationMethod->getDeclaringClass()->getName()] < $hierarchyDistances[$methodReflection->getDeclaringClass()->getName()]) {
return $annotationMethod;
}
}

@Korbeil Korbeil force-pushed the fix/trait-method-typehint-override-with-method-annotation branch 9 times, most recently from 309ffc4 to ce95727 Compare April 2, 2020 19:03
@Korbeil Korbeil changed the title Fix regression in method annotation with traits Fix issue in method return type override annotation on same class or from trait Apr 2, 2020
@Korbeil Korbeil marked this pull request as ready for review April 2, 2020 19:32
@Korbeil Korbeil force-pushed the fix/trait-method-typehint-override-with-method-annotation branch 2 times, most recently from e003a9c to 42f09d9 Compare April 6, 2020 09:12
@Korbeil Korbeil force-pushed the fix/trait-method-typehint-override-with-method-annotation branch from 42f09d9 to 6d502ac Compare April 6, 2020 09:20
@ondrejmirtes ondrejmirtes merged commit c1d0ced into phpstan:master Apr 6, 2020
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants