/** * Returns an exception description detail. * * @return string */ public function getDetail() { if (0 === $this->getStream()->count()) { return parent::getDetail() . 'The token stream was empty.'; } elseif (empty($this->token)) { return parent::getDetail() . 'The token stream was read out of its bounds.'; } else { return parent::getDetail() . sprintf("\nThe cause of the exception was the %s token (line %s) in following part of %s source code:\n\n%s", $this->tokenName, $this->token[2], $this->sender && $this->sender->getName() ? $this->sender->getPrettyName() : 'the', $this->getSourcePart(true)); } }
/** * {@inheritdoc} */ public function getTraitMethods() { $methods = []; foreach ($this->originalReflection->getTraitMethods($this->classReflection->getVisibilityLevel()) as $method) { $apiMethod = $this->classReflection->getReflectionFactory()->createFromReflection($method); if (!$this->classReflection->isDocumented() || $apiMethod->isDocumented()) { $methods[$method->getName()] = $apiMethod; } } return $methods; }
/** * Parses a token and adds it's value to the info array * * This method looks to the generator to get a token parser and then attempts to parse * a value using that token parser. * * @access private * @param string $token The token (type) we're parding * @param string $value The value (all content after the token itself) * @return void * @throws Exception In the event the parser does not validate the value */ private function parseToken($token, $value) { $token_parser = $this->generator->getTokenParser($token); if (!$token_parser) { return; } if (!$token_parser::validate($value)) { throw new Exception('Document: %s' . PHP_EOL . 'Error: Improperly formatted docblock token and value' . PHP_EOL . 'Token: @%s %s' . PHP_EOL . 'Line (~): %s' . PHP_EOL, $this->reflection->getFileName(), $token, $value, $this->reflection->getStartLine()); } if (!isset($this->info[$token])) { $this->info[$token] = array(); } $this->info[$token][] = $token_parser::parse($value); }
/** * Processes the parent reflection object. * * @param \TokenReflection\IReflection $parent Parent reflection object * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * * @return \TokenReflection\ReflectionElement * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided. */ protected function processParent(IReflection $parent, Stream $tokenStream) { if (!$parent instanceof ReflectionClass) { throw new Exception\ParseException($this, $tokenStream, 'The parent object has to be an instance of TokenReflection\\ReflectionClass.', Exception\ParseException::INVALID_PARENT); } $this->declaringClassName = $parent->getName(); if ($parent->isTrait()) { $this->declaringTraitName = $parent->getName(); } return parent::processParent($parent, $tokenStream); }
/** * Processes the parent reflection object. * * @param \TokenReflection\IReflection $parent Parent reflection object * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @return \TokenReflection\ReflectionElement * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided. */ protected function processParent(IReflection $parent, Stream $tokenStream) { if (!$parent instanceof ReflectionFunctionBase) { throw new Exception\ParseException($this, $tokenStream, 'The parent object has to be an instance of TokenReflection\\ReflectionFunctionBase.', Exception\ParseException::INVALID_PARENT); } // Declaring function name $this->declaringFunctionName = $parent->getName(); // Position $this->position = count($parent->getParameters()); // Declaring class name if ($parent instanceof ReflectionMethod) { $this->declaringClassName = $parent->getDeclaringClassName(); } return parent::processParent($parent, $tokenStream); }
/** * Find the appropriate docblock. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @param \TokenReflection\IReflection $parent Parent reflection * @return \TokenReflection\ReflectionElement */ protected function parseDocComment(Stream $tokenStream, IReflection $parent) { if ($this instanceof ReflectionParameter) { $this->docComment = new ReflectionAnnotation($this); return $this; } $position = $tokenStream->key(); if ($tokenStream->is(T_DOC_COMMENT, $position - 1)) { $value = $tokenStream->getTokenValue($position - 1); if (self::DOCBLOCK_TEMPLATE_END !== $value) { $this->docComment = new ReflectionAnnotation($this, $value); $this->startPosition--; } } elseif ($tokenStream->is(T_DOC_COMMENT, $position - 2)) { $value = $tokenStream->getTokenValue($position - 2); if (self::DOCBLOCK_TEMPLATE_END !== $value) { $this->docComment = new ReflectionAnnotation($this, $value); $this->startPosition -= 2; } } elseif ($tokenStream->is(T_COMMENT, $position - 1) && preg_match('~^' . preg_quote(self::DOCBLOCK_TEMPLATE_START, '~') . '~', $tokenStream->getTokenValue($position - 1))) { $this->docComment = new ReflectionAnnotation($this, $tokenStream->getTokenValue($position - 1)); $this->startPosition--; } elseif ($tokenStream->is(T_COMMENT, $position - 2) && preg_match('~^' . preg_quote(self::DOCBLOCK_TEMPLATE_START, '~') . '~', $tokenStream->getTokenValue($position - 2))) { $this->docComment = new ReflectionAnnotation($this, $tokenStream->getTokenValue($position - 2)); $this->startPosition -= 2; } if (null === $this->docComment) { $this->docComment = new ReflectionAnnotation($this); } if ($parent instanceof ReflectionElement) { $this->docComment->setTemplates($parent->getDocblockTemplates()); } return $this; }
/** * Processes the parent reflection object. * * @param \TokenReflection\IReflection $parent Parent reflection object * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @return \TokenReflection\ReflectionElement * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided. */ protected function processParent(IReflection $parent, Stream $tokenStream) { if ($parent instanceof ReflectionFileNamespace) { $this->namespaceName = $parent->getName(); $this->aliases = $parent->getNamespaceAliases(); } elseif ($parent instanceof ReflectionClass) { $this->declaringClassName = $parent->getName(); } else { throw new Exception\ParseException($this, $tokenStream, sprintf('Invalid parent reflection provided: "%s".', get_class($parent)), Exception\ParseException::INVALID_PARENT); } return parent::processParent($parent, $tokenStream); }
/** * Returns an exception description detail. * * @return string */ public function getDetail() { return null === $this->sender ? '' : sprintf('Thrown when working with "%s".', $this->sender->getPrettyName()); }
/** * Processes the parent reflection object. * * @param \TokenReflection\IReflection $parent Parent reflection object * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * * @return \TokenReflection\ReflectionElement * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided. */ protected function processParent(IReflection $parent, Stream $tokenStream) { if (!$parent instanceof ReflectionFileNamespace) { throw new Exception\ParseException($this, $tokenStream, 'The parent object has to be an instance of TokenReflection\\ReflectionFileNamespace.', Exception\ParseException::INVALID_PARENT); } $this->namespaceName = $parent->getName(); $this->aliases = $parent->getNamespaceAliases(); return parent::processParent($parent, $tokenStream); }
/** * Will store any given structure reflection * * @param IReflection $structureReflection Reflection instance to store in our cache * * @return null */ public function store(IReflection $structureReflection) { $filePath = $this->calculateCachePath($structureReflection->getName()); file_put_contents($filePath, serialize($structureReflection)); }
/** * Will test if the typehint of a parameter has been changed in a restrictive way and if a parameter has been added * * @param \TokenReflection\IReflection $structureReflection The current structure reflection to inspect * @param \TokenReflection\IReflectionMethod $currentMethod The current method reflection to inspect * @param \TokenReflection\IReflectionMethod $formerMethod The former method reflection to compare to * * @return boolean */ protected function didParametersChangeType(IReflection $structureReflection, IReflectionMethod $currentMethod, IReflectionMethod $formerMethod) { $formerParameters = $formerMethod->getParameters(); $currentParameters = $currentMethod->getParameters(); $parameterCount = count($currentParameters); for ($i = 0; $i < $parameterCount; $i++) { // if both methods have the parameter we compare types, otherwise we check for optionality if (isset($formerParameters[$i])) { // the parameter has been here before, but are the types consisten? if ($formerParameters[$i]->getOriginalTypeHint() !== $currentParameters[$i]->getOriginalTypeHint()) { $this->result->addReason(new Reason($structureReflection->getName(), $currentMethod->getName(), Reason::TYPEHINT_RESTRICTED, $this->mapper)); } } else { // the parameter has not been here before, is it optional? if (!$currentParameters[$i]->isDefaultValueAvailable()) { $this->result->addReason(new Reason($structureReflection->getName(), $currentMethod->getName(), Reason::PARAMETER_ADDED, $this->mapper)); } } } }
/** * Reduces a reference in a given context * * @access public * @param string $reference The reference to reduce * @param IReflection $context The reflection context for reduction * @return string The reduced reference */ public function reduce($reference, IReflection $context) { $member = NULL; if (strpos($reference, '::') !== FALSE) { list($reference, $member) = explode('::', $reference); } $reduction = $reference; if (!self::isStandardType($reference)) { $parts = explode('\\', $reduction); $reduction = '\\' . $reduction; $aliases = array_merge(['' => $context->getName()], $context->getNamespaceAliases()); foreach ($aliases as $alias => $namespace) { $namespace_parts = explode('\\', $namespace); if (count($parts) < count($namespace_parts)) { continue; } if ($namespace_parts == array_slice($parts, 0, count($namespace_parts))) { $reduction = implode('\\', !$alias ? array_slice($parts, count($namespace_parts)) : array_merge([$alias], array_slice($parts, count($namespace_parts)))); break; } } } return $member ? implode('::', [$reduction, $member]) : $reduction; }
/** * * @param ReflectionMethod $reflection * @return string */ protected static function getOperationReturns(IReflection $reflection) { $return = $reflection->getAnnotation("return"); return implode(",", array_values(is_array($return) ? $return : [])); }