/** * @return IReflectionReflection */ public function getClass() { if ($this->reflectionReflection === null) { $this->reflectionReflection = new ReflectionReflection($this->reflectionMethod->getDeclaringClass()); } return $this->reflectionReflection; }
/** * @return ClassParser */ private function getOriginalClass() { if (is_null($this->originalClass)) { $this->originalClass = new ClassParser($this->reflector->getDeclaringClass()->name); } return $this->originalClass; }
function methodData(ReflectionMethod $method) { $cname = strtolower($method->getDeclaringClass()->name); $mname = trim(str_replace("_", "-", strtolower($method->name)), "-"); $ename = strtolower($method->getDeclaringClass()->getExtension()->name); $fname = DOCBOOK . "/{$ename}/{$cname}/{$mname}.xml"; if (!file_exists($fname)) { //echo "No file $fname\n"; return ""; } $data = file_get_contents($fname); $data = preg_replace("/&[\\w.]+;/", "", $data); $xml = simplexml_load_string($data); $comment = (string) $xml->refnamediv->refpurpose; $return = (string) $xml->refsect1->methodsynopsis->type; $out = <<<END /** * {$comment} END; $args = array(); foreach ($xml->refsect1->methodsynopsis->methodparam as $param) { $out .= " * @param {$param->type} \${$param->parameter}\n"; $args[] = "\$" . $param->parameter; } if ($return) { $out .= " * @return {$return}\n"; } $out .= " */\n"; return array($out, $args); }
public function execute($object) { $handler = $this->factory->getInstance($this->method->getDeclaringClass()->getName()); $properties = $this->getProperties($object); $args = []; foreach ($this->method->getParameters() as $parameter) { $args[] = $properties[$parameter->getName()]->get($object); } return call_user_func_array([$handler, $this->method->getName()], $args); }
private function getAnnotationType($annotation) { $matches = array(); $found = preg_match('/@' . $annotation . '\\s+(\\S+)/', $this->reflection->getDocComment(), $matches); if (!$found) { return new UnknownType(); } $type = new TypeFactory($this->reflection->getDeclaringClass()); return $type->fromTypeHints(explode("|", $matches[1])); }
/** * Handle method annotations * * @param mixed $targetObj * @param \ReflectionMethod $reflection * @param array $annotations * * @return mixed */ public function handleMethodAnnotations(\ReflectionMethod $reflection, array $annotations, $targetObj = null) { if (isset($annotations['Route'])) { foreach ($annotations['Route'] as $route) { if ($route instanceof \stdClass && isset($route->path)) { $symfonyRoute = new Route($route->path, isset($route->defaults) ? $route->defaults : [], isset($route->requirements) ? $route->requirements : [], isset($route->options) ? $route->options : [], isset($route->host) ? $route->host : '', isset($route->schemes) ? $route->schemes : [], isset($route->methods) ? $route->methods : []); $symfonyRoute->setOption('karmaController', $reflection->getDeclaringClass()->getName()); $symfonyRoute->setOption('karmaAction', $reflection->getName()); $this->routeCollection->add(isset($route->name) ? $route->name : $reflection->getDeclaringClass()->getName() . ':' . $reflection->getName(), $symfonyRoute); } } } }
function __call($func, $args) { $di = property_exists($this, 'di') && $this->di instanceof Di ? $this->di : Di::getInstance(); if (substr($func, 0, 4) == 'call' && ctype_upper(substr($func, 4, 1)) && (method_exists($this, $m = lcfirst(substr($func, 4))) || method_exists($this, $m = '_' . $m))) { $params = $di->methodGetParams($this, $m, $args); $closure = function () use($m, $params) { return call_user_func_array([$this, $m], $params); }; $closure->bindTo($this); return $closure(); } $method = '_' . $func; if (method_exists($this, $method)) { if (!(new \ReflectionMethod($this, $method))->isPublic()) { throw new \RuntimeException("The called method is not public."); } return $di->method($this, $method, $args); } if (($c = get_parent_class($this)) && method_exists($c, __FUNCTION__)) { $m = new \ReflectionMethod($c, __FUNCTION__); $dc1 = $m->getDeclaringClass()->name; $dc2 = (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->name; $dc3 = get_class($this); if ($dc1 != $dc2 || $dc2 != $dc3 && $dc1 != $dc3) { return parent::__call($func, $args); } } throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '->' . $func); }
protected function addMethod(\ReflectionMethod $refMethod) { $class = $refMethod->getDeclaringClass(); $params = $this->getParamsString($refMethod); $module = $class->getName(); $body = ''; $doc = $this->addDoc($class, $refMethod); $doc = str_replace('/**', '', $doc); $doc = trim(str_replace('*/', '', $doc)); if (!$doc) { $doc = "*"; } $conditionalDoc = $doc . "\n * Conditional Assertion: Test won't be stopped on fail"; $methodTemplate = (new Template($this->methodTemplate))->place('module', $module)->place('method', $refMethod->name)->place('params', $params); // generate conditional assertions if (0 === strpos($refMethod->name, 'see')) { $type = 'Assertion'; $body .= $methodTemplate->place('doc', $conditionalDoc)->place('action', 'can' . ucfirst($refMethod->name))->place('step', 'ConditionalAssertion')->produce(); // generate negative assertion } elseif (0 === strpos($refMethod->name, 'dontSee')) { $type = 'Assertion'; $body .= $methodTemplate->place('doc', $conditionalDoc)->place('action', str_replace('dont', 'cant', $refMethod->name))->place('step', 'ConditionalAssertion')->produce(); } elseif (0 === strpos($refMethod->name, 'am')) { $type = 'Condition'; } else { $type = 'Action'; } $body .= $methodTemplate->place('doc', $doc)->place('action', $refMethod->name)->place('step', $type)->produce(); return $body; }
public function checkMethod(\ReflectionMethod $method) { $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)); } if ($method->isPrivate() || $method->isPublic()) { throw new \Exception(sprintf("%s with @(%s) must be protected", \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__)); } $ps = $method->getParameters(); if (count($ps) !== 0) { throw new \Exception(sprintf("%s with @(%s) can not has parameters", \Dev::getMethodDeclaring($method), __CLASS__)); } }
/** * Get reflection of declaring class * * @return ClassReflection */ public function getDeclaringClass() { $phpReflection = parent::getDeclaringClass(); $zendReflection = new ClassReflection($phpReflection->getName()); unset($phpReflection); return $zendReflection; }
/** * Checks if the value is typehinted with a class and if the current value can be coerced into that type * * It can either convert to datetime or attempt to fetched from the db by id * * @param mixed $obj instance or class name * @param string $method * @param string $value * @param integer $pNum * @return mixed */ public function check($obj, $method, $value, $pNum = 0) { if (!is_numeric($value) && !is_string($value)) { return $value; } $reflection = new \ReflectionMethod($obj, $method); $params = $reflection->getParameters(); if (!$params[$pNum]->getClass()) { return $value; } $hintedClass = $params[$pNum]->getClass()->getName(); if ($hintedClass === 'DateTime') { try { if (preg_match('{^[0-9]+$}', $value)) { $value = '@' . $value; } return new \DateTime($value); } catch (\Exception $e) { throw new \UnexpectedValueException('Could not convert ' . $value . ' to DateTime for ' . $reflection->getDeclaringClass()->getName() . '::' . $method, 0, $e); } } if ($hintedClass) { if (!$this->manager) { throw new \LogicException('To reference objects by id you must first set a Nelmio\\Alice\\PersisterInterface object on this instance'); } $value = $this->manager->find($hintedClass, $value); } return $value; }
/** * Iteratively check all given attributes by calling isGranted. * * This method terminates as soon as it is able to return ACCESS_GRANTED * If at least one attribute is supported, but access not granted, then ACCESS_DENIED is returned * Otherwise it will return ACCESS_ABSTAIN * * @param TokenInterface $token A TokenInterface instance * @param object $object The object to secure * @param array $attributes An array of attributes associated with the method being invoked * * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED */ public function vote(TokenInterface $token, $object, array $attributes) { if (!$object || !$this->supportsClass(get_class($object))) { return self::ACCESS_ABSTAIN; } // abstain vote by default in case none of the attributes are supported $vote = self::ACCESS_ABSTAIN; $reflector = new \ReflectionMethod($this, 'voteOnAttribute'); $isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter'; if (!$isNewOverwritten) { @trigger_error(sprintf("The AbstractVoter::isGranted method is deprecated since 2.8 and won't be called anymore in 3.0. Override voteOnAttribute() instead.", $reflector->class), E_USER_DEPRECATED); } foreach ($attributes as $attribute) { if (!$this->supportsAttribute($attribute)) { continue; } // as soon as at least one attribute is supported, default is to deny access $vote = self::ACCESS_DENIED; if ($isNewOverwritten) { if ($this->voteOnAttribute($attribute, $object, $token)) { // grant access as soon as at least one voter returns a positive response return self::ACCESS_GRANTED; } } else { if ($this->isGranted($attribute, $object, $token->getUser())) { // grant access as soon as at least one voter returns a positive response return self::ACCESS_GRANTED; } } } return $vote; }
public function convertMethodAnnotations(\ReflectionMethod $method, array $annotations) { $parameters = array(); foreach ($method->getParameters() as $index => $parameter) { $parameters[$parameter->getName()] = $index; } $methodMetadata = new MethodMetadata($method->getDeclaringClass()->getName(), $method->getName()); foreach ($annotations as $annotation) { if ($annotation instanceof Secure) { $methodMetadata->roles = $annotation->roles; } else { if ($annotation instanceof SecureParam) { if (!isset($parameters[$annotation->name])) { throw new \InvalidArgumentException(sprintf('The parameter "%s" does not exist for method "%s".', $annotation->name, $method->getName())); } $methodMetadata->addParamPermissions($parameters[$annotation->name], $annotation->permissions); } else { if ($annotation instanceof SecureReturn) { $methodMetadata->returnPermissions = $annotation->permissions; } else { if ($annotation instanceof SatisfiesParentSecurityPolicy) { $methodMetadata->satisfiesParentSecurityPolicy = true; } else { if ($annotation instanceof RunAs) { $methodMetadata->runAsRoles = $annotation->roles; } } } } } } return $methodMetadata; }
/** * Store ReflectionMethod object with computed key, the key is returned. * Method also stores the declaring class into class reflection collection. * * @param ReflectionMethod $reflection * @return string the key */ public function storeMethod(ReflectionMethod $reflection) { $classReflection = $reflection->getDeclaringClass(); if (!$this->classes->hasClass($classReflection->getName())) { $this->classes->storeClass($classReflection); } return $this->methods->storeMethod($reflection); }
function it_generates_correct_message_based_on_function_and_parameter(\ReflectionParameter $parameter, \ReflectionMethod $function, \ReflectionClass $class) { $parameter->getPosition()->willReturn(2); $function->getDeclaringClass()->willReturn($class); $class->getName()->willReturn('Acme\\Foo'); $function->getName()->willReturn('bar'); $this->getMessage()->shouldStartWith('Collaborator must be an object: argument 2 defined in Acme\\Foo::bar.'); }
public function getParameters() { $me = array(parent::getDeclaringClass()->getName(), $this->getName()); foreach ($res = parent::getParameters() as $key => $val) { $res[$key] = new Parameter($me, $val->getName()); } return $res; }
public static function callMethod(\ReflectionMethod $method, array $params = null) { if (null === $params) { $params = array_map(function ($p) { return '$' . $p->name; }, $method->getParameters()); } return '\\' . $method->getDeclaringClass()->name . '::' . $method->name . '(' . implode(', ', $params) . ')'; }
/** * @param \ReflectionMethod $method * @param array $args * @param callable $parameterFilter * @return array Of the injected arguments * @throws InjectionException */ public function injectMethodArguments(\ReflectionMethod $method, array $args, $parameterFilter) { $analyzer = new MethodAnalyzer($method); try { return $analyzer->fillParameters($args, $this->injector, $parameterFilter); } catch (\InvalidArgumentException $e) { throw new InjectionException("Cannot inject method [{$method->getDeclaringClass()->getName()}" . "::{$method->getName()}]: " . $e->getMessage(), 0, $e); } }
public function indexAction() { $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('seo_admin_main', array(), 'seo_admin_main_manage'); $items = array(); foreach (Engine_Api::_()->getItemTypes() as $itemType) { $class = Engine_Api::_()->getItemClass($itemType); if (@class_exists($class)) { $reflector = new ReflectionMethod($class, 'getHref'); if ($reflector->getDeclaringClass()->getName() == $class) { $items[$itemType] = ucfirst(Engine_Api::typeToShort($itemType, Engine_Api::_()->getItemModule($itemType))) . ' (' . $itemType . ')'; } } } $this->view->formFilter = $formFilter = new Seo_Form_Admin_Manage_Filter(); // Populate types foreach ($items as $k => $v) { $formFilter->type->addMultiOption($k, $v); } // Process form $values = array(); if ($formFilter->isValid($this->_getAllParams())) { $values = $formFilter->getValues(); } foreach ($values as $key => $value) { if (null === $value) { unset($values[$key]); } } $this->view->assign($values); if (isset($values['type'])) { try { $table = Engine_Api::_()->getItemTable($values['type']); $select = $table->select(); } catch (Engine_Api_Exception $e) { } } $this->view->paginator = $paginator = Zend_Paginator::factory(isset($select) ? $select : array()); $paginator->setItemCountPerPage(25); $paginator->setCurrentPageNumber($this->_getParam('page', 1)); if (!isset($select)) { return; } // Set up select info $primary = array_shift($table->info('primary')); $values = array_merge(array('order' => $primary, 'order_direction' => 'DESC'), $values); $select->order((!empty($values['order']) ? $values['order'] == 'id' ? $primary : (in_array($values['order'], $table->info('cols')) ? $values['order'] : $primary) : $primary) . ' ' . (!empty($values['order_direction']) ? $values['order_direction'] : 'DESC')); if (in_array('title', $table->info('cols')) && !empty($values['title'])) { $select->where('title LIKE ?', '%' . $values['title'] . '%'); } // Filter out junk $valuesCopy = array_filter($values); // Reset enabled bit if (isset($values['enabled']) && $values['enabled'] == 0) { $valuesCopy['enabled'] = 0; } }
/** * @param TypeFactory $factory * @param \ReflectionMethod $method */ public function __construct(TypeFactory $factory, \ReflectionMethod $method) { $start = substr($method->getName(), 0, 2) == 'is' ? 2 : 3; parent::__construct($factory, $method->getDeclaringClass(), lcfirst(substr($method->getName(), $start))); if (substr($method->getName(), 0, 3) == 'set') { $this->setter = $method; } else { $this->getter = $method; } }
/** * Get reflection of declaring class * * @param string $reflectionClass Name of reflection class to use * @return \Zend\Reflection\ReflectionClass */ public function getDeclaringClass($reflectionClass = '\\Zend\\Reflection\\ReflectionClass') { $phpReflection = parent::getDeclaringClass(); $zendReflection = new $reflectionClass($phpReflection->getName()); if (!$zendReflection instanceof ReflectionClass) { throw new Exception('Invalid reflection class provided; must extend Zend\\Reflection\\ReflectionClass'); } unset($phpReflection); return $zendReflection; }
/** * {@inheritdoc} */ public function matchesMethod(\ReflectionMethod $method) { $metadata = $this->metadataFactory->getMetadataForClass($method->getDeclaringClass()->getName()); foreach ($metadata->methodMetadata as $methodMetadata) { if ($methodMetadata instanceof MethodMetadata && $methodMetadata->name === $method->name) { return true; } } return false; }
/** * Get import statements and aliases from the class containing this method * * @return array */ protected function getUses() { if (null !== $this->uses) { return $this->uses; } $rClass = $this->reflection->getDeclaringClass(); $rFile = $rClass->getDeclaringFile(); $this->uses = $rFile->getUses(); return $this->uses; }
/** * @param object|NULL * @return AccessBase $this */ public function asInstance($object) { if (is_object($object)) { if ($this->reflection instanceof ReflectionClass) { $class = $this->reflection->getName(); } else { $class = $this->reflection->getDeclaringClass()->getName(); } if (!$object instanceof $class) { throw new Exception('Must be instance of accessible class.'); } } else { if ($object !== NULL) { throw new Exception('Instance must be object or NULL.'); } } $this->instance = $object; return $this; }
/** * Get reflection of declaring class * * @param string $reflectionClass Name of reflection class to use * @return Zend_Reflection_Class */ public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class') { $phpReflection = parent::getDeclaringClass(); $zendReflection = new $reflectionClass($phpReflection->getName()); if (!$zendReflection instanceof Zend_Reflection_Class) { require_once 'Zend/Reflection/Exception.php'; throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class'); } unset($phpReflection); return $zendReflection; }
/** * @param $callable * * @return string */ public function getCallableName($callable) { $name = '(Unknown)'; if (is_array($callable)) { $method = new \ReflectionMethod($callable[0], $callable[1]); $className = $method->getDeclaringClass()->getName(); $className = str_replace('Ciconia\\Extension\\', '', $className); $name = $className . ':' . $method->getShortName(); } return $name; }
/** * @param \ReflectionMethod $reflectionMethod * @param int $methodStartLine * @param int $methodEndLine * * @return \ReflectionClass */ private function getMethodOwner(\ReflectionMethod $reflectionMethod, $methodStartLine, $methodEndLine) { $reflectionClass = $reflectionMethod->getDeclaringClass(); // PHP <=5.3 does not handle traits if (version_compare(PHP_VERSION, '5.4.0', '<')) { return $reflectionClass; } $fileName = $reflectionMethod->getFileName(); $trait = $this->getDeclaringTrait($reflectionClass->getTraits(), $fileName, $methodStartLine, $methodEndLine); return $trait === null ? $reflectionClass : $trait; }
/** * * @param ReflectionMethod $method * @return string */ private function getDocComment(ReflectionMethod $method) { $comment = ''; //$comment = $method->getDocComment(); if (strlen($comment) > 0) { return $comment; } $fileName = $method->getDeclaringClass()->getFileName(); $commentParser = new Gpf_Rpc_Annotation_CommentParser(new Gpf_Io_File($fileName)); return $commentParser->getMethodComment($method->getName()); }
/** * @param \ReflectionMethod $method * @return bool */ private function isNotImplementedInPhp(\ReflectionMethod $method) { $filename = $method->getDeclaringClass()->getFileName(); if (false === $filename) { return true; } // HHVM <=3.2.0 does not return FALSE correctly if (preg_match('#^/([:/]systemlib.|/$)#', $filename)) { return true; } return false; }
public function getMethodAnnotations(\ReflectionMethod $method) { if (isset($this->loadedAnnotations[$key = $method->getDeclaringClass()->getName() . '#' . $method->getName()])) { return $this->loadedAnnotations[$key]; } if (null !== ($annots = $this->cache->getMethodAnnotationsFromCache($method))) { return $this->loadedAnnotations[$key] = $annots; } $annots = $this->delegate->getMethodAnnotations($method); $this->cache->putMethodAnnotationsInCache($method, $annots); return $this->loadedAnnotations[$key] = $annots; }