4a3b061e48218447e95baae286c76b6a743e8144
[yaffs-website] / vendor / psy / psysh / src / Psy / Util / Mirror.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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\ReflectionConstant;
16
17 /**
18  * A utility class for getting Reflectors.
19  */
20 class Mirror
21 {
22     const CONSTANT        = 1;
23     const METHOD          = 2;
24     const STATIC_PROPERTY = 4;
25     const PROPERTY        = 8;
26
27     /**
28      * Get a Reflector for a function, class or instance, constant, method or property.
29      *
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:
32      *
33      *    $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
34      *
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
37      *
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)
41      *
42      * @return Reflector
43      */
44     public static function get($value, $member = null, $filter = 15)
45     {
46         if ($member === null && is_string($value) && function_exists($value)) {
47             return new \ReflectionFunction($value);
48         }
49
50         $class = self::getClass($value);
51
52         if ($member === null) {
53             return $class;
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);
62         } else {
63             throw new RuntimeException(sprintf(
64                 'Unknown member %s on class %s',
65                 $member,
66                 is_object($value) ? get_class($value) : $value
67             ));
68         }
69     }
70
71     /**
72      * Get a ReflectionClass (or ReflectionObject) if possible.
73      *
74      * @throws \InvalidArgumentException if $value is not a class name or instance
75      *
76      * @param mixed $value
77      *
78      * @return \ReflectionClass
79      */
80     private static function getClass($value)
81     {
82         if (is_object($value)) {
83             return new \ReflectionObject($value);
84         }
85
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);
90         }
91
92         return new \ReflectionClass($value);
93     }
94 }