4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Psy\Exception\RuntimeException;
15 use Psy\Reflection\ReflectionClassConstant;
16 use Psy\Reflection\ReflectionConstant_;
19 * A utility class for getting Reflectors.
25 const STATIC_PROPERTY = 4;
29 * Get a Reflector for a function, class or instance, constant, method or property.
31 * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
32 * static properties and constants, pass:
34 * $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
36 * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value
37 * @throws \InvalidArgumentException if $value is something other than an object or class/function name
39 * @param mixed $value Class or function name, or variable instance
40 * @param string $member Optional: property, constant or method name (default: null)
41 * @param int $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
45 public static function get($value, $member = null, $filter = 15)
47 if ($member === null && \is_string($value)) {
48 if (\function_exists($value)) {
49 return new \ReflectionFunction($value);
50 } elseif (\defined($value) || ReflectionConstant_::isMagicConstant($value)) {
51 return new ReflectionConstant_($value);
55 $class = self::getClass($value);
57 if ($member === null) {
59 } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
60 return ReflectionClassConstant::create($value, $member);
61 } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
62 return $class->getMethod($member);
63 } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
64 return $class->getProperty($member);
65 } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
66 return $class->getProperty($member);
68 throw new RuntimeException(\sprintf(
69 'Unknown member %s on class %s',
71 \is_object($value) ? \get_class($value) : $value
77 * Get a ReflectionClass (or ReflectionObject) if possible.
79 * @throws \InvalidArgumentException if $value is not a class name or instance
83 * @return \ReflectionClass
85 private static function getClass($value)
87 if (\is_object($value)) {
88 return new \ReflectionObject($value);
91 if (!\is_string($value)) {
92 throw new \InvalidArgumentException('Mirror expects an object or class');
93 } elseif (!\class_exists($value) && !\interface_exists($value) && !\trait_exists($value)) {
94 throw new \InvalidArgumentException('Unknown class or function: ' . $value);
97 return new \ReflectionClass($value);