4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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\ReflectionConstant;
18 * A utility class for getting Reflectors.
24 const STATIC_PROPERTY = 4;
28 * Get a Reflector for a function, class or instance, constant, method or property.
30 * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
31 * static properties and constants, pass:
33 * $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
35 * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value
36 * @throws \InvalidArgumentException if $value is something other than an object or class/function name
38 * @param mixed $value Class or function name, or variable instance
39 * @param string $member Optional: property, constant or method name (default: null)
40 * @param int $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
44 public static function get($value, $member = null, $filter = 15)
46 if ($member === null && is_string($value) && function_exists($value)) {
47 return new \ReflectionFunction($value);
50 $class = self::getClass($value);
52 if ($member === null) {
54 } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
55 return new ReflectionConstant($class, $member);
56 } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
57 return $class->getMethod($member);
58 } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
59 return $class->getProperty($member);
60 } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
61 return $class->getProperty($member);
63 throw new RuntimeException(sprintf(
64 'Unknown member %s on class %s',
66 is_object($value) ? get_class($value) : $value
72 * Get a ReflectionClass (or ReflectionObject) if possible.
74 * @throws \InvalidArgumentException if $value is not a class name or instance
78 * @return \ReflectionClass
80 private static function getClass($value)
82 if (is_object($value)) {
83 return new \ReflectionObject($value);
86 if (!is_string($value)) {
87 throw new \InvalidArgumentException('Mirror expects an object or class');
88 } elseif (!class_exists($value) && !interface_exists($value) && !(function_exists('trait_exists') && trait_exists($value))) {
89 throw new \InvalidArgumentException('Unknown class or function: ' . $value);
92 return new \ReflectionClass($value);