Skip to content

Commit 0face91

Browse files
committed
new: add new method for get reflect method object
1 parent b8ea26b commit 0face91

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/Helper/PhpHelper.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
use Generator;
1414
use ReflectionClass;
1515
use ReflectionException;
16+
use ReflectionMethod;
1617
use RuntimeException;
1718
use Toolkit\Stdlib\Obj\ObjectHelper;
1819
use function array_sum;
1920
use function explode;
2021
use function fopen;
2122
use function ftok;
2223
use function function_exists;
24+
use function get_class;
2325
use function is_array;
2426
use function is_callable;
2527
use function is_object;
@@ -53,18 +55,44 @@ class PhpHelper
5355
private static $reflects = [];
5456

5557
/**
56-
* @param string $class
58+
* @var ReflectionMethod[]
59+
*/
60+
private static $reflectMths = [];
61+
62+
/**
63+
* @param string|object $classOrObj
5764
*
5865
* @return ReflectionClass
5966
* @throws ReflectionException
6067
*/
61-
public static function reflectClass(string $class): ReflectionClass
68+
public static function reflectClass($classOrObj): ReflectionClass
69+
{
70+
$id = is_string($classOrObj) ? $classOrObj : get_class($classOrObj);
71+
72+
if (!isset(self::$reflects[$id])) {
73+
self::$reflects[$id] = new ReflectionClass($classOrObj);
74+
}
75+
76+
return self::$reflects[$id];
77+
}
78+
79+
/**
80+
* @param string|object $classOrObj
81+
* @param string $method
82+
*
83+
* @return ReflectionMethod
84+
* @throws ReflectionException
85+
*/
86+
public static function reflectMethod($classOrObj, string $method): ReflectionMethod
6287
{
63-
if (!isset(self::$reflects[$class])) {
64-
self::$reflects[$class] = new ReflectionClass($class);
88+
$id = is_string($classOrObj) ? $classOrObj : get_class($classOrObj);
89+
$id .= '.' . $method;
90+
91+
if (!isset(self::$reflectMths[$id])) {
92+
self::$reflectMths[$id] = new ReflectionMethod($classOrObj, $method);
6593
}
6694

67-
return self::$reflects[$class];
95+
return self::$reflectMths[$id];
6896
}
6997

7098
/**
@@ -74,7 +102,7 @@ public static function reflectClass(string $class): ReflectionClass
74102
*
75103
* @return Generator
76104
*/
77-
public static function reflectMethods(ReflectionClass $reflectClass, int $flags = 0, Closure $nameFilter = null): ?Generator
105+
public static function getReflectMethods(ReflectionClass $reflectClass, int $flags = 0, Closure $nameFilter = null): ?Generator
78106
{
79107
foreach ($reflectClass->getMethods($flags) as $m) {
80108
$mName = $m->getName();

0 commit comments

Comments
 (0)