Skip to content

Commit d47153d

Browse files
authored
[8.x] Improves Support\Reflector to support checking interfaces (#40822)
* Update Reflector.php * Update SupportReflectorTest.php
1 parent c7f4c23 commit d47153d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Illuminate/Support/Reflector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public static function isParameterSubclassOf($parameter, $className)
136136
$paramClassName = static::getParameterClassName($parameter);
137137

138138
return $paramClassName
139-
&& class_exists($paramClassName)
139+
&& (class_exists($paramClassName) ||
140+
interface_exists($paramClassName))
140141
&& (new ReflectionClass($paramClassName))->isSubclassOf($className);
141142
}
142143
}

tests/Support/SupportReflectorTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public function testParentClassName()
4848
$this->assertSame(A::class, Reflector::getParameterClassName($method->getParameters()[0]));
4949
}
5050

51+
public function testParameterSubclassOfInterface()
52+
{
53+
$method = (new ReflectionClass(TestClassWithInterfaceSubclassParameter::class))->getMethod('f');
54+
55+
$this->assertTrue(Reflector::isParameterSubclassOf($method->getParameters()[0], IA::class));
56+
}
57+
5158
/**
5259
* @requires PHP >= 8
5360
*/
@@ -95,8 +102,7 @@ public function f(A|Model $x)
95102
{
96103
//
97104
}
98-
}'
99-
);
105+
}');
100106
}
101107

102108
class TestClassWithCall
@@ -114,3 +120,19 @@ public static function __callStatic($method, $parameters)
114120
//
115121
}
116122
}
123+
124+
interface IA
125+
{
126+
}
127+
128+
interface IB extends IA
129+
{
130+
}
131+
132+
class TestClassWithInterfaceSubclassParameter
133+
{
134+
public function f(IB $x)
135+
{
136+
//
137+
}
138+
}

0 commit comments

Comments
 (0)