Skip to content

Commit

Permalink
[8.x] Improves Support\Reflector to support checking interfaces (#4…
Browse files Browse the repository at this point in the history
…0822)

* Update Reflector.php

* Update SupportReflectorTest.php
  • Loading branch information
hassanhe authored Feb 5, 2022
1 parent c7f4c23 commit d47153d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Support/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public static function isParameterSubclassOf($parameter, $className)
$paramClassName = static::getParameterClassName($parameter);

return $paramClassName
&& class_exists($paramClassName)
&& (class_exists($paramClassName) ||
interface_exists($paramClassName))
&& (new ReflectionClass($paramClassName))->isSubclassOf($className);
}
}
26 changes: 24 additions & 2 deletions tests/Support/SupportReflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function testParentClassName()
$this->assertSame(A::class, Reflector::getParameterClassName($method->getParameters()[0]));
}

public function testParameterSubclassOfInterface()
{
$method = (new ReflectionClass(TestClassWithInterfaceSubclassParameter::class))->getMethod('f');

$this->assertTrue(Reflector::isParameterSubclassOf($method->getParameters()[0], IA::class));
}

/**
* @requires PHP >= 8
*/
Expand Down Expand Up @@ -95,8 +102,7 @@ public function f(A|Model $x)
{
//
}
}'
);
}');
}

class TestClassWithCall
Expand All @@ -114,3 +120,19 @@ public static function __callStatic($method, $parameters)
//
}
}

interface IA
{
}

interface IB extends IA
{
}

class TestClassWithInterfaceSubclassParameter
{
public function f(IB $x)
{
//
}
}

0 comments on commit d47153d

Please sign in to comment.