<commit_before><?php

/*
 * This file is part of PsySH
 *
 * (c) 2012 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Psy\Util;

use Psy\Exception\RuntimeException;
use Psy\Reflection\ReflectionConstant;

/**
 * Utility class for getting Reflectors.
 */
class Mirror
{
    const CONSTANT        = 1;
    const METHOD          = 2;
    const STATIC_PROPERTY = 4;
    const PROPERTY        = 8;

    /**
     * Get a Reflector for a function, class or instance, constant, method or property.
     *
     * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
     * static properties and constants, pass:
     *
     *    $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
     *
     * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value.
     * @throws \InvalidArgumentException if $value is something other than an object or class/function name.
     *
     * @param mixed  $value  Class or function name, or variable instance.
     * @param string $member Optional: property, constant or method name (default: null)
     * @param int    $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
     *
     * @return Reflector
     */
    public static function get($value, $member = null, $filter = 15)
    {
        if ($member === null && is_string($value) && function_exists($value)) {
            return self::getFunction($value);
        }

        $class = self::getClass($value);

        if ($member === null) {
            return $class;
        } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
            return self::getConstant($class, $member);
        } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
            return self::getMethod($class, $member);
        } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
            return self::getProperty($class, $member);
        } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
            return self::getProperty($class, $member);
        } else {
            throw new RuntimeException(sprintf(
                'Unknown member %s on class %s',
                $member,
                is_object($value) ? get_class($value) : $value
            ));
        }
    }

    public static function getFunction($value)
    {
        return new \ReflectionFunction($value);
    }

    public static function getClass($value)
    {
        if (!is_object($value)) {
            if (!is_string($value)) {
                throw new \InvalidArgumentException('Mirror expects an object or class');
            } elseif (!class_exists($value) && !interface_exists($value)) {
                throw new \InvalidArgumentException('Unknown class: '.$value);
            }
        }

        return new \ReflectionClass($value);
    }

    public static function getConstant(\ReflectionClass $class, $member)
    {
        return new ReflectionConstant($class, $member);<commit_msg>Better message when class/method not found.<commit_after><?php

/*
 * This file is part of PsySH
 *
 * (c) 2012 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Psy\Util;

use Psy\Exception\RuntimeException;
use Psy\Reflection\ReflectionConstant;

/**
 * Utility class for getting Reflectors.
 */
class Mirror
{
    const CONSTANT        = 1;
    const METHOD          = 2;
    const STATIC_PROPERTY = 4;
    const PROPERTY        = 8;

    /**
     * Get a Reflector for a function, class or instance, constant, method or property.
     *
     * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
     * static properties and constants, pass:
     *
     *    $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
     *
     * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value.
     * @throws \InvalidArgumentException if $value is something other than an object or class/function name.
     *
     * @param mixed  $value  Class or function name, or variable instance.
     * @param string $member Optional: property, constant or method name (default: null)
     * @param int    $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
     *
     * @return Reflector
     */
    public static function get($value, $member = null, $filter = 15)
    {
        if ($member === null && is_string($value) && function_exists($value)) {
            return self::getFunction($value);
        }

        $class = self::getClass($value);

        if ($member === null) {
            return $class;
        } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
            return self::getConstant($class, $member);
        } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
            return self::getMethod($class, $member);
        } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
            return self::getProperty($class, $member);
        } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
            return self::getProperty($class, $member);
        } else {
            throw new RuntimeException(sprintf(
                'Unknown member %s on class %s',
                $member,
                is_object($value) ? get_class($value) : $value
            ));
        }
    }

    public static function getFunction($value)
    {
        return new \ReflectionFunction($value);
    }

    public static function getClass($value)
    {
        if (!is_object($value)) {
            if (!is_string($value)) {
                throw new \InvalidArgumentException('Mirror expects an object or class');
            } elseif (!class_exists($value) && !interface_exists($value)) {
                throw new \InvalidArgumentException('Unknown class or function: '.$value);
            }
        }

        return new \ReflectionClass($value);
    }

    public static function getConstant(\ReflectionClass $class, $member)
    {
        return new ReflectionConstant($class, $member);
{'fraction_non_alphanumeric': 0.1181077694235589, 'fraction_numerical': 0.003132832080200501, 'mean_word_length': 2.515969162995595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788290', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1671, 'n_words': 520}