09c0b5b9aff7241cb3ebd91bafa81d87e3c8c6aa
[yaffs-website] / vendor / psy / psysh / src / Util / Mirror.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Util;
13
14 use Psy\Exception\RuntimeException;
15 use Psy\Reflection\ReflectionClassConstant;
16 use Psy\Reflection\ReflectionConstant_;
17
18 /**
19  * A utility class for getting Reflectors.
20  */
21 class Mirror
22 {
23     const CONSTANT        = 1;
24     const METHOD          = 2;
25     const STATIC_PROPERTY = 4;
26     const PROPERTY        = 8;
27
28     /**
29      * Get a Reflector for a function, class or instance, constant, method or property.
30      *
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:
33      *
34      *    $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
35      *
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
38      *
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)
42      *
43      * @return \Reflector
44      */
45     public static function get($value, $member = null, $filter = 15)
46     {
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);
52             }
53         }
54
55         $class = self::getClass($value);
56
57         if ($member === null) {
58             return $class;
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);
67         } else {
68             throw new RuntimeException(\sprintf(
69                 'Unknown member %s on class %s',
70                 $member,
71                 \is_object($value) ? \get_class($value) : $value
72             ));
73         }
74     }
75
76     /**
77      * Get a ReflectionClass (or ReflectionObject) if possible.
78      *
79      * @throws \InvalidArgumentException if $value is not a class name or instance
80      *
81      * @param mixed $value
82      *
83      * @return \ReflectionClass
84      */
85     private static function getClass($value)
86     {
87         if (\is_object($value)) {
88             return new \ReflectionObject($value);
89         }
90
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);
95         }
96
97         return new \ReflectionClass($value);
98     }
99 }