public function __construct(PropertyAnnotation $annotation, \ReflectionMethod $reflection) { $this->reflection = $reflection; $this->annotation = $annotation; if ($this->reflection->isPrivate() || $this->reflection->isProtected()) { $this->reflection->setAccessible(true); } }
/** * @param Factory $factory * @param string $class * @param string $name * @param array|Argument[] $arguments * @param array|History[] $collected * @throws \ReflectionException If the method cannot be stubbed */ function __construct(Factory $factory, $class, $name, array $arguments = [], array $collected = []) { $this->class = $class; $this->name = $name; $this->arguments = $arguments; $this->reflection = new \ReflectionMethod($class, $name); $this->typeHint = new ReturnTypeInferer($this->reflection, $factory); $this->history = new History($collected); if ($this->reflection->isPrivate()) { throw new \ReflectionException("Cannot stub private methods [{$this->class}::{$name}()]"); } else { if ($this->reflection->isStatic()) { throw new \ReflectionException("Cannot stub static methods [{$this->class}::{$name}()]"); } } }
protected function _checkProtection($protectionType) { // Check type if (!is_string($protectionType) || $protectionType != self::PROTECTION_CSRF && $protectionType != self::PROTECTION_CAPTCHA && $protectionType != self::PROTECTION_BRUTEFORCE && $protectionType != self::PROTECTION_XSS) { throw new \Exception('Invalid protection type : "' . $protectionType . '"'); } // Check class if (class_exists('framework\\security\\form\\' . ucfirst($protectionType))) { $className = 'framework\\security\\form\\' . ucfirst($protectionType); } else { $className = $protectionType; } $class = new \ReflectionClass($className); if (!in_array('framework\\security\\IForm', $class->getInterfaceNames())) { throw new \Exception('Form protection drivers must be implement framework\\security\\IForm'); } if ($class->isAbstract()) { throw new \Exception('Form protection drivers must be not abstract class'); } if ($class->isInterface()) { throw new \Exception('Form protection drivers must be not interface'); } $classInstance = $class->newInstanceWithoutConstructor(); $constuctor = new \ReflectionMethod($classInstance, '__construct'); if ($constuctor->isPrivate() || $constuctor->isProtected()) { throw new \Exception('Protection constructor must be public'); } return $className; }
private function _invoke($clz = '', $act = '', $param, $namespace = 'Home\\Controller\\') { $clz = ucwords($clz); $clz_name = $namespace . $clz . 'Controller'; try { $ref_clz = new \ReflectionClass($clz_name); if ($ref_clz->hasMethod($act)) { $ref_fun = new \ReflectionMethod($clz_name, $act); if ($ref_fun->isPrivate() || $ref_fun->isProtected()) { $ref_fun->setAccessible(true); } if ($ref_fun->isStatic()) { $ref_fun->invoke(null); } else { $ref_fun_par = $ref_fun->getParameters(); if (!empty($param) && is_array($param)) { if (is_array($ref_fun_par) && count($ref_fun_par) == count($param)) { $ref_fun->invokeArgs(new $clz_name(), $param); } else { $ref_fun->invoke(new $clz_name()); } } else { $ref_fun->invoke(new $clz_name()); } } } } catch (\LogicException $le) { $this->ajaxReturn(array('status' => '500', 'info' => '服务器内部发生严重错误')); } catch (\ReflectionException $re) { $this->ajaxReturn(array('status' => '404', 'info' => '访问' . $clz . '控制器下的非法操作', 'data' => array('code' => $re->getCode()))); } }
/** * Prints warning about any hooked method with bad visibility (that are either protected or private) * @return void */ function appthemes_bad_method_visibility() { global $wp_filter; $arguments = func_get_args(); $tag = array_shift($arguments); $errors = new WP_Error(); if (!isset($wp_filter[$tag])) { return; } foreach ($wp_filter[$tag] as $prioritized_callbacks) { foreach ($prioritized_callbacks as $callback) { $function = $callback['function']; if (is_array($function)) { try { $method = new ReflectionMethod($function[0], $function[1]); if ($method->isPrivate() || $method->isProtected()) { $class = get_class($function[0]); if (!$class) { $class = $function[0]; } $errors->add('visiblity', $class . '::' . $function[1] . ' was hooked into "' . $tag . '", but is either protected or private.'); } } catch (Exception $e) { // Failure to replicate method. Might be magic method. Fail silently } } } } if ($errors->get_error_messages()) { foreach ($errors->get_error_messages() as $message) { echo $message; } } }
private static function reflector($method_and_params) { $class = get_called_class(); $method = $method_and_params[0]; $args = $method_and_params[1]; $reflectionMethod = new \ReflectionMethod($class, $method); if (!$reflectionMethod->isStatic()) { throw new \Exception("The method: {$class}::{$method} should be static!!"); } if (!$reflectionMethod->isPrivate()) { throw new \Exception("The static method: {$class}::{$method} should be private!!"); } $params = array(); foreach ($reflectionMethod->getParameters() as $param) { if (isset($args[$param->getName()])) { $params[] = $args[$param->getName()]; } else { if ($param->isOptional()) { $params[] = $param->getDefaultValue(); } else { throw new \Exception("The method: {$class}::{$method} signature requires a \"\${$param->getName()}\" argument!!"); } } } $reflectionMethod->setAccessible(true); return $reflectionMethod->invokeArgs(null, $params); }
/** * Returns whether this method is private * * @return boolean TRUE if this method is private */ public function isPrivate() { if ($this->reflectionSource instanceof ReflectionMethod) { return $this->reflectionSource->isPrivate(); } else { return parent::isPrivate(); } }
public function testPublicize_with_private_static_method() { $className = __FUNCTION__ . md5(uniqid()); eval(sprintf('class %s { private static function foo() { return true; } }', $className)); $reflectionMethod = new ReflectionMethod($className, 'foo'); $this->assertTrue($reflectionMethod->isPrivate()); $this->assertTrue($reflectionMethod->isStatic()); $this->assertSame($reflectionMethod, $reflectionMethod->publicize()); $this->assertSame(true, $reflectionMethod->invoke($className)); }
/** * @param \ReflectionMethod $reflection */ public function setVisibilityFromReflection(\ReflectionMethod $reflection) { if ($reflection->isPublic()) { $this->setVisibility('public'); } if ($reflection->isProtected()) { $this->setVisibility('protected'); } if ($reflection->isPrivate()) { $this->setVisibility('private'); } }
/** * @param object $object * @param string $method * @param array $args * * @return mixed */ protected function callPrivateMethod($object, $method, $args = array()) { $reflectionMethod = new \ReflectionMethod($object, $method); if ($reflectionMethod->isPrivate() || $reflectionMethod->isProtected()) { $reflectionMethod->setAccessible(true); $result = $reflectionMethod->invokeArgs($reflectionMethod->isStatic() ? null : $object, $args); $reflectionMethod->setAccessible(false); return $result; } else { return call_user_func_array(array($object, $method), $args); } }
/** * {@inheritdoc} */ public function execute(MetaModel $metaModel) { $object = $this->subNode->execute($metaModel); if (is_null($object)) { throw new ExecutionException("Calling method '{$this->method}' on null"); } $reflectionMethod = new \ReflectionMethod($object, $this->method); // God mode if ($reflectionMethod->isPrivate() || $reflectionMethod->isProtected()) { $reflectionMethod->setAccessible(true); } return $reflectionMethod->invoke($object); }
/** * @param \ReflectionMethod $reflectionMethod * @param \Donquixote\HastyReflectionCommon\Reflection\FunctionLike\MethodReflectionInterface $methodReflection */ private function assertEqualMethods(\ReflectionMethod $reflectionMethod, MethodReflectionInterface $methodReflection) { $this->assertEquals($reflectionMethod->isAbstract(), $methodReflection->isAbstract()); $this->assertEquals($reflectionMethod->getDeclaringClass()->getName(), $methodReflection->getDeclaringClassName()); $this->assertEquals($reflectionMethod->getDocComment(), $methodReflection->getDocComment()); $this->assertEquals($reflectionMethod->getShortName(), $methodReflection->getName()); $this->assertEquals($reflectionMethod->getName(), $methodReflection->getName()); $this->assertEquals($reflectionMethod->class . '::' . $reflectionMethod->getName(), $methodReflection->getQualifiedName()); $this->assertEquals($reflectionMethod->returnsReference(), $methodReflection->isByReference()); $this->assertEquals($reflectionMethod->isPrivate(), $methodReflection->isPrivate()); $this->assertEquals($reflectionMethod->isProtected(), $methodReflection->isProtected()); $this->assertEquals($reflectionMethod->isPublic(), $methodReflection->isPublic()); $this->assertEquals($reflectionMethod->isStatic(), $methodReflection->isStatic()); }
protected function assertIsSingleton($class, $instance = null) { foreach (array('__construct', '__clone') as $methodName) { $method = new \ReflectionMethod($class, $methodName); $this->assertTrue($method->isProtected() || $method->isPrivate()); } $getInstance = function () use($class) { return call_user_func(array($class, 'getInstance')); }; if ($instance) { $this->assertEquals(spl_object_hash($instance), spl_object_hash($getInstance())); } $this->assertEquals(spl_object_hash($getInstance()), spl_object_hash($getInstance())); }
public static function getRunkitMethodFlags($className, $methodName) { $ref = new ReflectionMethod($className, $methodName); $flags = 0; if ($ref->isPrivate()) { $flags |= RUNKIT_ACC_PRIVATE; } elseif ($ref->isProtected()) { $flags |= RUNKIT_ACC_PROTECTED; } else { $flags |= RUNKIT_ACC_PUBLIC; } if ($ref->isStatic()) { $flags |= RUNKIT_ACC_STATIC; } return $flags; }
public static function reload_method($classname, $methodname) { $method = new ReflectionMethod($classname, $methodname); $visibility = RUNKIT_ACC_PUBLIC; if ($method->isProtected()) { $visibility = RUNKIT_ACC_PROTECTED; } else { if ($method->isPrivate()) { $visibility = RUNKIT_ACC_PRIVATE; } } if ($method->isStatic()) { $visibility = $visibility | RUNKIT_ACC_STATIC; } return runkit_method_redefine($classname, $methodname, self::getMethodArguments($classname, $methodname), self::getMethodCode($classname, $methodname), $visibility); }
public final function getMethodHeader(ReflectionMethod $method) { $param_sources = array(); foreach ($method->getParameters() as $param) { $param_sources[] = $this->generateParameterCode($param); } if ($method->isProtected()) { $modifier = 'protected'; } else { if ($method->isPrivate()) { $modifier = 'private'; } else { $modifier = 'public'; } } return sprintf('%s %sfunction %s(%s)', $modifier, $method->isStatic() ? 'static ' : '', $method->getName(), implode(', ', $param_sources)); }
function __construct($q) { // echo('<br />q3:' . $q.'<br />'); $data = explode("/", $q); $this->action = $data[0]; // /home/par1/par2 global $user; $menu_obj = new Menu(); //$this->params = @$data[1]; $new_params = ""; for ($i = 1; $i < count($data); $i++) { $new_params .= $data[$i] . "/"; } $new_params = substr(@$new_params, 0, strlen(@$new_params) - 1); $this->params = @$new_params; $function = $this->action; if (empty($this->action)) { $function = "home"; $this->action = $function; } elseif (!method_exists($this, $function)) { $function = "page_not_found"; $this->action = $function; } $reflection = new ReflectionMethod($this, $this->action); if ($reflection->isPublic()) { //echo "Public method"; } if ($reflection->isPrivate()) { //echo "Private method"; $function = "page_not_found"; } if ($reflection->isProtected()) { //echo "Protected method"; $function = "page_not_found"; } if (MAINTENANCE) { $function = "maintenance"; } // echo('function:' . $function); $this->{$function}($this->params); if ($user['id']) { //pr($user->id); $this->menu = $menu_obj->build_menu_by_client($user['cid']); } }
public function checkMethod(\ReflectionMethod $method) { if ($method->isPrivate() || $method->isProtected()) { throw new \Exception(sprintf("%s with @(%s) must be public", \Dev::getMethodDeclaring($method), __CLASS__)); } if ($method->isStatic()) { throw new \Exception(sprintf("%s with @(%s) can not be static", \Dev::getMethodDeclaring($method), __CLASS__)); } if ($method->isAbstract()) { throw new \Exception(sprintf("%s with @(%s) can not be abstract", \Dev::getMethodDeclaring($method), __CLASS__)); } if ($method->isConstructor()) { throw new \Exception(sprintf("%s with @(%s) can not be constructor", \Dev::getMethodDeclaring($method), __CLASS__)); } if ($method->isDestructor()) { throw new \Exception(sprintf("%s with @(%s) can not be destructor", \Dev::getMethodDeclaring($method), __CLASS__)); } $class = $method->getDeclaringClass(); if (!$class->isSubclassOf(self::BASE_CLASS_NAME)) { throw new \Exception(sprintf("%s with @(%s) must extends from class(%s)", \Dev::getMethodDeclaring($method), __CLASS__, self::BASE_CLASS_NAME)); } $parent = $class->getParentClass(); $fn = $method->getName(); if ($parent->hasMethod($fn)) { throw new \Exception(sprintf("%s with @(%s) can not use with parent class method(%s)", \Dev::getMethodDeclaring($method), __CLASS__, $fn)); } if ($this->method) { if (!preg_match('/^[a-z][\\w\\_]+$/', $this->method)) { throw new \Exception(sprintf("%s with @(%s) method(%s) invalid", \Dev::getMethodDeclaring($method), __CLASS__, $this->method)); } } else { $this->method = $fn; } if (!$this->type) { $this->type = 'common'; } $exists = array('common' => array('spacecp_credit_extra', 'faq_extra', 'global_footer', 'global_footerlink', 'global_cpnav_top', 'global_cpnav_extra1', 'global_cpnav_extra2', 'global_usernav_extra1', 'global_usernav_extra2', 'global_usernav_extra3', 'global_usernav_extra4', 'global_nav_extra', 'global_header', 'global_userabout_top', 'userapp_menu_top', 'userapp_menu_middle', 'global_userabout_bottom'), 'forum' => array('collection_index_top', 'collection_index_bottom', 'collection_nav_extra', 'collection_index_top', 'collection_index_bottom', 'collection_index_top', 'collection_index_bottom', 'collection_nav_extra', 'collection_viewoptions', 'collection_view_top', 'collection_threadlistbottom', 'collection_relatedop', 'collection_view_bottom', 'collection_side_bottom', 'index_status_extra', 'index_nav_extra', 'index_top', 'index_catlist_top', 'index_favforum_extra', 'index_catlist', 'index_forum_extra', 'index_forum_extra', 'index_middle', 'index_bottom', 'index_side_top', 'index_side_bottom', 'viewthread_attach_extra', 'post_image_btn_extra', 'post_image_tab_extra', 'post_attach_btn_extra', 'post_attach_tab_extra', 'forumdisplay_leftside_top', 'forumdisplay_leftside_bottom', 'forumdisplay_forumaction', 'forumdisplay_modlink', 'forumdisplay_top', 'forumdisplay_middle', 'forumdisplay_postbutton_top', 'forumdisplay_threadtype_inner', 'forumdisplay_filter_extra', 'forumdisplay_threadtype_extra', 'forumdisplay_bottom', 'forumdisplay_side_top', 'forumdisplay_side_bottom', 'forumdisplay_fastpost_content', 'forumdisplay_fastpost_func_extra', 'forumdisplay_fastpost_ctrl_extra', 'global_login_text', 'forumdisplay_fastpost_btn_extra', 'forumdisplay_fastpost_sync_method', 'forumdisplay_filter_extra', 'forumdisplay_thread', 'forumdisplay_thread_subject', 'forumdisplay_author', 'forumdisplay_thread', 'forumdisplay_author', 'forumdisplay_threadlist_bottom', 'forumdisplay_postbutton_bottom', 'forumdisplay_postbutton_bottom', 'forumdisplay_subforum_extra', 'forumdisplay_subforum_extra', 'guide_nav_extra', 'guide_top', 'guide_bottom', 'forumdisplay_thread', 'index_navbar', 'post_top', 'post_middle', 'post_btn_extra', 'post_sync_method', 'post_bottom', 'post_activity_extra', 'post_debate_extra', 'post_editorctrl_right', 'post_editorctrl_left', 'post_editorctrl_top', 'post_editorctrl_bottom', 'post_side_top', 'post_side_bottom', 'post_infloat_top', 'post_infloat_middle', 'post_infloat_btn_extra', 'post_poll_extra', 'post_reward_extra', 'post_trade_extra', 'forumdisplay_modlayer', 'modcp_modlayer', 'viewthread_tradeinfo_extra', 'viewthread_top', 'viewthread_postbutton_top', 'viewthread_modoption', 'viewthread_beginline', 'viewthread_title_extra', 'viewthread_title_row', 'viewthread_middle', 'viewthread_bottom', 'viewthread_activity_extra1', 'viewthread_activity_extra2', 'viewthread_fastpost_side', 'viewthread_fastpost_content', 'viewthread_fastpost_func_extra', 'viewthread_fastpost_ctrl_extra', 'global_login_text', 'viewthread_fastpost_btn_extra', 'viewthread_postheader', 'viewthread_postheader', 'viewthread_postheader', 'viewthread_endline', 'viewthread_profileside', 'viewthread_imicons', 'viewthread_magic_user', 'viewthread_avatar', 'viewthread_sidetop', 'viewthread_sidebottom', 'viewthread_postheader', 'viewthread_modaction', 'viewthread_share_method', 'viewthread_useraction', 'viewthread_postsightmlafter', 'viewthread_postfooter', 'viewthread_postaction', 'viewthread_magic_thread', 'viewthread_magic_post', 'viewthread_endline', 'viewthread_posttop', 'global_login_text', 'viewthread_postbottom', 'viewthread_poll_top', 'viewthread_poll_bottom', 'viewthread_useraction_prefix', 'viewthread_useraction', 'viewthread_side_bottom', 'viewthread_trade_extra'), 'group' => array('group_navlink', 'forumdisplay_navlink', 'group_navlink', 'forumdisplay_navlink', 'group_top', 'forumdisplay_top', 'group_nav_extra', 'forumdisplay_nav_extra', 'group_bottom', 'forumdisplay_bottom', 'group_side_bottom', 'forumdisplay_side_bottom', 'forumdisplay_postbutton_top', 'forumdisplay_filter_extra', 'forumdisplay_thread', 'forumdisplay_postbutton_bottom', 'my_header', 'my_bottom', 'my_side_top', 'my_side_bottom', 'group_index_side', 'group_side_top', 'forumdisplay_side_top', 'index_header', 'index_top', 'index_bottom', 'index_side_top', 'index_side_bottom', 'index_top', 'index_grouplist', 'index_bottom', 'index_side_top', 'index_side_bottom'), 'home' => array('follow_nav_extra', 'follow_top', 'spacecp_avatar_top', 'spacecp_avatar_bottom', 'spacecp_blog_top', 'spacecp_blog_middle', 'spacecp_blog_bottom', 'spacecp_credit_top', 'spacecp_credit_extra', 'spacecp_credit_bottom', 'spacecp_credit_top', 'spacecp_credit_bottom', 'spacecp_privacy_top', 'spacecp_privacy_base_extra', 'spacecp_privacy_feed_extra', 'spacecp_privacy_bottom', 'spacecp_profile_top', 'spacecp_profile_extra', 'spacecp_profile_bottom', 'spacecp_promotion_top', 'spacecp_promotion_bottom', 'spacecp_usergroup_top', 'spacecp_usergroup_bottom', 'spacecp_usergroup_top', 'spacecp_usergroup_bottom', 'spacecp_usergroup_top', 'spacecp_usergroup_bottom', 'space_album_pic_top', 'space_album_pic_op_extra', 'space_album_pic_bottom', 'space_album_pic_face_extra', 'space_album_op_extra', 'space_blog_list_status', 'space_blog_title', 'space_blog_share_method', 'space_blog_op_extra', 'space_blog_face_extra', 'space_card_top', 'space_card_baseinfo_middle', 'space_card_baseinfo_bottom', 'space_card_option', 'space_card_magic_user', 'space_card_bottom', 'space_blog_comment_op', 'space_blog_comment_bottom', 'space_doing_top', 'space_doing_bottom', 'space_favorite_nav_extra', 'space_interaction_extra', 'global_usernav_extra1', 'global_usernav_extra2', 'space_home_side_top', 'space_home_side_bottom', 'space_home_top', 'space_home_navlink', 'space_home_bottom', 'magic_nav_extra', 'medal_nav_extra', 'space_menu_extra', 'space_profile_baseinfo_top', 'follow_profile_baseinfo_top', 'space_profile_baseinfo_middle', 'follow_profile_baseinfo_middle', 'space_profile_baseinfo_bottom', 'follow_profile_baseinfo_bottom', 'space_profile_extrainfo', 'follow_profile_extrainfo', 'space_share_comment_op', 'space_home_doing_sync_method', 'space_wall_face_extra'), 'member' => array('logging_side_top', 'logging_top', 'logging_input', 'logging_method', 'global_login_extra', 'register_side_top', 'register_top', 'register_input', 'register_logging_method', 'register_bottom'), 'portal' => array('portalcp_top', 'portalcp_extend', 'portalcp_middle', 'portalcp_bottom', 'view_article_top', 'view_article_subtitle', 'view_article_summary', 'view_article_content', 'view_share_method', 'view_article_op_extra', 'view_article_side_top', 'view_article_side_bottom'), 'ranklist' => array('ranklist_nav_extra'), 'search' => array('album_top', 'album_bottom', 'blog_top', 'blog_bottom', 'global_footer', 'global_footerlink', 'forum_top', 'forum_bottom', 'group_top', 'group_bottom', 'global_usernav_extra1', 'global_usernav_extra2', 'portal_top', 'portal_bottom'), 'userapp' => array('userapp_app_top', 'userapp_app_bottom', 'userapp_index_top', 'userapp_index_bottom', 'userapp_menu_top', 'userapp_menu_middle', 'userapp_menu_bottom'), 'mobile_common' => array('global_footer_mobile', 'global_header_mobile'), 'mobile_forum' => array('index_top_mobile', 'index_middle_mobile', 'index_bottom_mobile', 'forumdisplay_top_mobile', 'forumdisplay_thread_mobile', 'forumdisplay_bottom_mobile', 'viewthread_top_mobile', 'viewthread_posttop_mobile', 'viewthread_postbottom_mobile', 'viewthread_bottom_mobile')); if (!isset($exists[$this->type])) { throw new \Exception(sprintf("%s with @(%s) type(%s) must be one of(%s)", \Dev::getMethodDeclaring($method), __CLASS__, json_encode($this->type), join(', ', array_keys($exists)))); } }
function methodCheck($className, $methodName) { if (method_exists($className, $methodName)) { $refl = new ReflectionMethod($className, $methodName); if ($refl->isPrivate()) { throw new Exception("This method is not callable. Because this is private function."); } else { if ($refl->isProtected()) { throw new Exception("This method is not direct callable. Because this is protected function."); } else { try { $return = $refl->invoke(new $className()); } catch (Exception $e) { die($e->getMessage()); } } } return $return; } else { return false; } }
/** * @param ReflectionMethod * @return callable(object|NULL $instance, array $args) */ public function accessMethod(ReflectionMethod $method) { if (PHP_VERSION_ID >= 50302 or $method->isPublic()) { if (PHP_VERSION_ID >= 50302) { $method->setAccessible(true); } return $this->callback('$instance, array $args', ' return call_user_func_array(array($method, "invoke"), array_merge(array($instance), $args)); ', array('method' => $method)); } else { if ($method->isProtected()) { return $this->callback('$instance, array $args', ' if (!$instance) $instance = $className; // static return call_user_func(array($helperClassName, "__AccessAccessor_php52__invoke"), $instance, $methodName, $args); ', array('helperClassName' => $this->getHelperClass($method->getDeclaringClass()), 'className' => $method->getDeclaringClass()->getName(), 'methodName' => $method->getName())); } else { if ($method->isPrivate()) { throw new Exception('AccessMethod needs PHP 5.3.2 or newer to call private method.'); } } } }
function reflectMethod($class, $method) { $methodInfo = new ReflectionMethod($class, $method); echo "**********************************\n"; echo "Reflecting on method {$class}::{$method}()\n\n"; echo "\nisFinal():\n"; var_dump($methodInfo->isFinal()); echo "\nisAbstract():\n"; var_dump($methodInfo->isAbstract()); echo "\nisPublic():\n"; var_dump($methodInfo->isPublic()); echo "\nisPrivate():\n"; var_dump($methodInfo->isPrivate()); echo "\nisProtected():\n"; var_dump($methodInfo->isProtected()); echo "\nisStatic():\n"; var_dump($methodInfo->isStatic()); echo "\nisConstructor():\n"; var_dump($methodInfo->isConstructor()); echo "\nisDestructor():\n"; var_dump($methodInfo->isDestructor()); echo "\n**********************************\n"; }
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; }
/** * Imports a method that is obtained via reflection. * * @param \ReflectionMethod $method Method that is to be imported. * * @see self::reflectInternalClass for a complete description. * * @return \DOMElement|null */ protected function importReflectedMethod(\ReflectionMethod $method) { if ($method->isPrivate()) { return null; } $class_name = $method->getDeclaringClass()->getName(); $methods = $this->getMethods(); if (in_array($method->getName(), array_keys($methods))) { $methods[$method->getName()]->getNode()->appendChild(new \DOMElement('overrides-from', $class_name)); return $methods[$method->getName()]->getNode(); } $method_node = new \DOMElement('method'); $this->node->appendChild($method_node); $node_name = new \DOMElement('name', $method->getName()); $method_node->appendChild($node_name); $method_node->setAttribute('final', $method->isFinal() ? 'true' : 'false'); $method_node->setAttribute('abstract', $method->isAbstract() ? 'true' : 'false'); $method_node->setAttribute('static', $method->isStatic() ? 'true' : 'false'); $method_node->setAttribute('visibility', $method->isPublic() ? 'public' : 'protected'); $method_obj = new MethodNode($method_node, $this->nodes, $this); $inherited_from = new \DOMElement('tag'); $method_obj->getDocBlock()->getNode()->appendChild($inherited_from); $inherited_from->setAttribute('name', 'inherited_from'); $inherited_from->setAttribute('refers', $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()'); $inherited_from->setAttribute('description', $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()'); return $method_node; }
/** * Returns the signature of a method. * * @param ReflectionMethod $method * @return string * @since Method available since Release 3.2.0 */ public static function getMethodSignature(ReflectionMethod $method) { if ($method->isPrivate()) { $modifier = 'private'; } else { if ($method->isProtected()) { $modifier = 'protected'; } else { $modifier = 'public'; } } if ($method->isStatic()) { $modifier .= ' static'; } if ($method->returnsReference()) { $reference = '&'; } else { $reference = ''; } return sprintf('%s function %s%s(%s)', $modifier, $reference, $method->getName(), self::getMethodParameters($method)); }
/** * Given a user-defined PHP class or php object, map its methods onto a list of * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server * object and called from remote clients (as well as their corresponding signature info). * * @param string|object $className the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class * @param array $extraOptions see the docs for wrapPhpMethod for basic options, plus * - string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on whether $className is a class name or object instance * - string method_filter a regexp used to filter methods to wrap based on their names * - string prefix used for the names of the xmlrpc methods created * * @return array|false false on failure */ public function wrapPhpClass($className, $extraOptions = array()) { $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; $methodType = isset($extraOptions['method_type']) ? $extraOptions['method_type'] : 'auto'; $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : ''; $results = array(); $mList = get_class_methods($className); foreach ($mList as $mName) { if ($methodFilter == '' || preg_match($methodFilter, $mName)) { $func = new \ReflectionMethod($className, $mName); if (!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) { if ($func->isStatic() && ($methodType == 'all' || $methodType == 'static' || $methodType == 'auto' && is_string($className)) || !$func->isStatic() && ($methodType == 'all' || $methodType == 'nonstatic' || $methodType == 'auto' && is_object($className))) { $methodWrap = $this->wrapPhpFunction(array($className, $mName), '', $extraOptions); if ($methodWrap) { if (is_object($className)) { $realClassName = get_class($className); } else { $realClassName = $className; } $results[$prefix . "{$realClassName}.{$mName}"] = $methodWrap; } } } } } return $results; }
/** * Clone method parameters and comments using ReflectionMethod. * * @param \ReflectionMethod $method */ public function cloneSchema(\ReflectionMethod $method) { $this->setComment($method->getDocComment()); $this->static = $method->isStatic(); if ($method->isPrivate()) { $this->setAccess(self::ACCESS_PRIVATE); } elseif ($method->isProtected()) { $this->setAccess(self::ACCESS_PROTECTED); } foreach ($method->getParameters() as $reflection) { $parameter = $this->parameter($reflection->getName()); if ($reflection->isOptional()) { $parameter->setOptional(true, $reflection->getDefaultValue()); } $reflection->isArray() && $parameter->setType('array'); if (!empty($reflection->getClass())) { $parameter->setType('\\' . ltrim($reflection->getClass()->getName(), '\\')); } $parameter->setPBR($reflection->isPassedByReference()); } }
/** * @param string $templateDir * @param ReflectionMethod $method * @param boolean $cloneArguments * @param boolean $callOriginalMethods * * @return string */ protected function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method, $cloneArguments, $callOriginalMethods) { if ($method->isPrivate()) { $modifier = 'private'; } elseif ($method->isProtected()) { $modifier = 'protected'; } else { $modifier = 'public'; } if ($method->isStatic()) { $modifier .= ' static'; } if ($method->returnsReference()) { $reference = '&'; } else { $reference = ''; } return $this->generateMockedMethodDefinition($templateDir, $method->getDeclaringClass()->getName(), $method->getName(), $cloneArguments, $modifier, $this->getMethodParameters($method), $this->getMethodParameters($method, true), $reference, $callOriginalMethods, $method->isStatic()); }
/** * @param string $templateDir * @param ReflectionMethod $method * @return string */ protected static function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method) { if ($method->isPrivate()) { $modifier = 'private'; } else { if ($method->isProtected()) { $modifier = 'protected'; } else { $modifier = 'public'; } } if ($method->isStatic()) { $static = TRUE; } else { $static = FALSE; } if ($method->returnsReference()) { $reference = '&'; } else { $reference = ''; } return self::generateMockedMethodDefinition($templateDir, $method->getDeclaringClass()->getName(), $method->getName(), $modifier, PHPUnit_Util_Class::getMethodParameters($method), PHPUnit_Util_Class::getMethodParameters($method, TRUE), $reference, $static); }
function is_class_method($type = "public", $method, $class) { $refl = new ReflectionMethod($class, $method); switch ($type) { case "static": return $refl->isStatic(); break; case "public": return $refl->isPublic(); break; case "private": return $refl->isPrivate(); break; } }
/** * @param ReflectionMethod $method * @return bool */ protected function canMockMethod(ReflectionMethod $method) { if ($method->isConstructor() || $method->isFinal() || $method->isPrivate() || isset($this->blacklistedMethodNames[$method->getName()])) { return false; } return true; }