Skip to content

DynamicReturnTypeExtensions: allow hooking on object #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/DynamicMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
interface DynamicMethodReturnTypeExtension
{

public function getClass(): string;
public function getClass(): ?string;

public function isMethodSupported(MethodReflection $methodReflection): bool;

Expand Down
16 changes: 12 additions & 4 deletions src/Type/DynamicReturnTypeExtensionRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getDynamicMethodReturnTypeExtensionsForClass(string $className):
if ($this->dynamicMethodReturnTypeExtensionsByClass === null) {
$byClass = [];
foreach ($this->dynamicMethodReturnTypeExtensions as $extension) {
$byClass[strtolower($extension->getClass())][] = $extension;
$byClass[$this->normalizeClassName($extension->getClass())][] = $extension;
}

$this->dynamicMethodReturnTypeExtensionsByClass = $byClass;
Expand All @@ -63,7 +63,7 @@ public function getDynamicStaticMethodReturnTypeExtensionsForClass(string $class
if ($this->dynamicStaticMethodReturnTypeExtensionsByClass === null) {
$byClass = [];
foreach ($this->dynamicStaticMethodReturnTypeExtensions as $extension) {
$byClass[strtolower($extension->getClass())][] = $extension;
$byClass[$this->normalizeClassName($extension->getClass())][] = $extension;
}

$this->dynamicStaticMethodReturnTypeExtensionsByClass = $byClass;
Expand All @@ -83,8 +83,8 @@ private function getDynamicExtensionsForType(array $extensions, string $classNam

$extensionsForClass = [[]];
$class = $this->reflectionProvider->getClass($className);
foreach (array_merge([$className], $class->getParentClassesNames(), $class->getNativeReflection()->getInterfaceNames()) as $extensionClassName) {
$extensionClassName = strtolower($extensionClassName);
foreach (array_merge([$className, $this->normalizeClassName(null)], $class->getParentClassesNames(), $class->getNativeReflection()->getInterfaceNames()) as $extensionClassName) {
$extensionClassName = $this->normalizeClassName($extensionClassName);
if (!isset($extensions[$extensionClassName])) {
continue;
}
Expand All @@ -103,4 +103,12 @@ public function getDynamicFunctionReturnTypeExtensions(): array
return $this->dynamicFunctionReturnTypeExtensions;
}

private function normalizeClassName(?string $className): string
{
if ($className === null) {
return '0_any_class'; // such classname cannot ever exist
}
return strtolower($className);
}

}
2 changes: 1 addition & 1 deletion src/Type/DynamicStaticMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
interface DynamicStaticMethodReturnTypeExtension
{

public function getClass(): string;
public function getClass(): ?string;

public function isStaticMethodSupported(MethodReflection $methodReflection): bool;

Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/data/TestDynamicReturnTypeExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Reflection\ResolvedMethodReflection;
use PHPStan\Reflection\Type\CalledOnTypeUnresolvedMethodPrototypeReflection;
use PHPStan\Reflection\Type\UnionTypeMethodReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
Expand Down Expand Up @@ -263,3 +264,23 @@ public function getTypeFromStaticMethodCall(
return $scope->getType(new New_($methodCall->class));
}
}


class ObjectHookedDynamicStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension {

public function getClass(): ?string
{
return null;
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'methodReturningBoolNoMatterTheCaller';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
return new BooleanType();
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/dynamic-method-return-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function doFoo()
assertType('DynamicMethodReturnTypesNamespace\Foo', $container[\DynamicMethodReturnTypesNamespace\Foo::class]);
assertType('object', new \DynamicMethodReturnTypesNamespace\Foo());
assertType('object', new \DynamicMethodReturnTypesNamespace\FooWithoutConstructor());
assertType('bool', \DynamicMethodReturnTypesNamespace\WhateverClass::methodReturningBoolNoMatterTheCaller());
assertType('bool', \DynamicMethodReturnTypesNamespace\WhateverClass2::methodReturningBoolNoMatterTheCaller());
}

}
Expand All @@ -96,3 +98,11 @@ class FooWithoutConstructor
{

}


class WhateverClass {
public static function methodReturningBoolNoMatterTheCaller(){}
}
class WhateverClass2 {
public static function methodReturningBoolNoMatterTheCaller(){}
}
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/dynamic-return-type.neon
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ services:
class: PHPStan\Tests\Bug7391BDynamicStaticMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: PHPStan\Tests\ObjectHookedDynamicStaticMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension