/** * Returns whether this is a user-defined method * * @return boolean True if this is a user-defined method */ public function isUserDefined() { if ($this->reflectionSource instanceof ReflectionMethod) { return $this->reflectionSource->isUserDefined(); } else { return parent::isUserDefined(); } }
function reflectMethod($class, $method) { $methodInfo = new ReflectionMethod($class, $method); echo "**********************************\n"; echo "Reflecting on method {$class}::{$method}()\n\n"; echo "\ngetName():\n"; var_dump($methodInfo->getName()); echo "\nisInternal():\n"; var_dump($methodInfo->isInternal()); echo "\nisUserDefined():\n"; var_dump($methodInfo->isUserDefined()); echo "\n**********************************\n"; }
/** * {@inheritdoc} */ public function callAction($action = '', array $parameters = []) { //Action should include prefix and be always specified $action = static::ACTION_PREFIX . (!empty($action) ? $action : $this->defaultAction); if (!method_exists($this, $action)) { throw new ControllerException("No such action '{$action}'.", ControllerException::BAD_ACTION); } $reflection = new \ReflectionMethod($this, $action); if ($reflection->getName() == SaturableInterface::SATURATE_METHOD || $reflection->isStatic() || !$reflection->isPublic() || !$reflection->isUserDefined() || $reflection->getDeclaringClass()->getName() == __CLASS__) { throw new ControllerException("Action '{$action}' can not be executed.", ControllerException::BAD_ACTION); } try { //Getting set of arguments should be sent to requested method $arguments = $this->container->resolveArguments($reflection, $parameters); } catch (ArgumentException $exception) { throw new ControllerException("Missing/invalid parameter '{$exception->getParameter()->name}'.", ControllerException::BAD_ARGUMENT); } //Executing our action return $this->executeAction($reflection, $arguments, $parameters); }
function methodData(ReflectionMethod $method) { $details = ""; $name = $method->getName(); if ($method->isUserDefined()) { $details .= "{$name} -- метод определён пользователем<br>"; } if ($method->isInternal()) { $details .= "{$name} -- внутренний метод<br>"; } if ($method->isAbstract()) { $details .= "{$name} -- абстрактный метод<br>"; } if ($method->isPublic()) { $details .= "{$name} -- публичный метод<br>"; } if ($method->isProtected()) { $details .= "{$name} -- защищенный метод<br>"; } if ($method->isPrivate()) { $details .= "{$name} -- закрытый метод метод<br>"; } if ($method->isStatic()) { $details .= "{$name} -- статичный метод<br>"; } if ($method->isFinal()) { $details .= "{$name} -- финальный метод<br>"; } if ($method->isConstructor()) { $details .= "{$name} -- метод конструктора<br>"; } if ($method->returnsReference()) { $details .= "{$name} -- метод возвращает ссылку а не значение<br>"; } return $details; }
function methodData(ReflectionMethod $method) { $details = ""; $name = $method->getName(); if ($method->isUserDefined()) { $details .= "{$name} is user defined\n"; } if ($method->isInternal()) { $details .= "{$name} is built-in\n"; } if ($method->isAbstract()) { $details .= "{$name} is abstract\n"; } if ($method->isPublic()) { $details .= "{$name} is public\n"; } if ($method->isProtected()) { $details .= "{$name} is protected\n"; } if ($method->isPrivate()) { $details .= "{$name} is private\n"; } if ($method->isStatic()) { $details .= "{$name} is static\n"; } if ($method->isFinal()) { $details .= "{$name} is final\n"; } if ($method->isConstructor()) { $details .= "{$name} is the constructor\n"; } if ($method->returnsReference()) { $details .= "{$name} returns a reference (as opposed to a value)\n"; } return $details; }
/** * Try and retrieve additional information about a shortcode using Reflection on the function/method * * @param array $info Shortcode info * @param string $shortcode Current shortcode * @return array Updated shortcode info */ public function retrieve_shortcode_info_from_file($info, $shortcode) { $shortcodes = $GLOBALS['shortcode_tags']; if (!isset($shortcodes[$shortcode])) { // Not a registered shortcode return $info; } $callback = $shortcodes[$shortcode]; if (!is_string($callback) && (!is_array($callback) || is_array($callback) && (!is_string($callback[0]) && !is_object($callback[0])))) { // Not a valid callback return $info; } /* Set up reflection */ if (is_string($callback) && strpos($callback, '::') === false) { $reflection = new ReflectionFunction($callback); } else { if (is_string($callback) && strpos($callback, '::') !== false) { $reflection = new ReflectionMethod($callback); } else { if (is_array($callback)) { $reflection = new ReflectionMethod($callback[0], $callback[1]); } } } if (!isset($reflection) || $reflection->isUserDefined() === false) { // Not a user defined callback, i.e. native PHP, nothing to find out about it (shouldn't ever happen) return $info; } $info['description'] = nl2br($this->strip_comment_markers($reflection->getDocComment())); $info['self_closing'] = true; if ($reflection->getNumberOfRequiredParameters() > 1) { $info['self_closing'] = false; } $info['info_url'] = $this->get_plugin_url_from_file($reflection->getFileName(), $shortcode); return $info; }
/** * Check if method is callable. * * @param \ReflectionMethod $method * @return bool */ protected function isExecutable(\ReflectionMethod $method) { if ($method->isStatic() || !$method->isUserDefined()) { return false; } //Place to implement custom logic return true; }
} catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } try { new ReflectionMethod('a', 'b', 'c'); } catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } class C { public function f() { } } $rm = new ReflectionMethod('C', 'f'); var_dump($rm->isFinal(1)); var_dump($rm->isAbstract(1)); var_dump($rm->isPrivate(1)); var_dump($rm->isProtected(1)); var_dump($rm->isPublic(1)); var_dump($rm->isStatic(1)); var_dump($rm->isConstructor(1)); var_dump($rm->isDestructor(1)); var_dump($rm->getModifiers(1)); var_dump($rm->isInternal(1)); var_dump($rm->isUserDefined(1)); var_dump($rm->getFileName(1)); var_dump($rm->getStartLine(1)); var_dump($rm->getEndLine(1)); var_dump($rm->getStaticVariables(1)); var_dump($rm->getName(1));
$right = strrpos($value, '.'); $left = strlen($dir) + 1; $name = substr($value, $left, $right - $left); if (stristr($name, 'Service')) { $tokens = split('_', $name); $str = ''; foreach ($tokens as $t => $token) { $str = $str . strtoupper(substr($token, 0, 1)) . substr($token, 1); } if ($str != '') { require_once $value; $instance = new $str(); $methods = get_class_methods($instance); foreach ($methods as $m => $method) { $rm = new ReflectionMethod($str, $method); if ($rm->isUserDefined() && !$rm->isConstructor() && !$rm->isDestructor() && $rm->getNumberOfParameters() == 2) { $comment = $rm->getDocComment(); $metadata = getServiceMetadata($comment); if ($metadata === FALSE) { // could not parse metadata continue; } $request = $metadata['request']; $adapter = new ServiceAdapter($instance, $rm, $metadata); registerService($request, $adapter, $services); } } } } } function getRequests($content_type, $input)
public static function callOriginal($callable, $args, $class = null) { if (is_array($callable)) { if (is_object($callable[0])) { $obj = $callable[0]; if (!$class) { $class = get_class($obj); } } else { $class = $callable[0]; } $method = $callable[1]; } else { if (is_scalar($callable) && strpos($callable, '::') !== false) { list($class, $method) = explode("::", $callable); } else { return call_user_func_array($callable, $args); } } try { $Rm = new \ReflectionMethod($class, $method); if ($Rm->isUserDefined()) { self::$temp_disable = true; // we can only mock and check for mocks for user defined methods } } catch (\ReflectionException $e) { // do nothing, it is ok in this case because it means that mock disabling is not needed } if (isset($obj)) { return self::callMethod($obj, $class, $method, $args); } else { return self::callStaticMethod($class, $method, $args); } }