composer update

This commit is contained in:
2020-05-10 09:29:56 +00:00
parent c6f807ebad
commit 8e93eececf
919 changed files with 11790 additions and 7005 deletions

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -14,7 +15,7 @@ namespace phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
use Webmozart\Assert\Assert;
use function vsprintf;
/**
* Object representing to description for a DocBlock.
@@ -59,15 +60,20 @@ class Description
/**
* Initializes a Description with its body (template) and a listing of the tags used in the body template.
*
* @param string $bodyTemplate
* @param Tag[] $tags
*/
public function __construct($bodyTemplate, array $tags = [])
public function __construct(string $bodyTemplate, array $tags = [])
{
Assert::string($bodyTemplate);
$this->bodyTemplate = $bodyTemplate;
$this->tags = $tags;
$this->tags = $tags;
}
/**
* Returns the body template.
*/
public function getBodyTemplate() : string
{
return $this->bodyTemplate;
}
/**
@@ -75,7 +81,7 @@ class Description
*
* @return Tag[]
*/
public function getTags()
public function getTags() : array
{
return $this->tags;
}
@@ -83,12 +89,8 @@ class Description
/**
* Renders this description as a string where the provided formatter will format the tags in the expected string
* format.
*
* @param Formatter|null $formatter
*
* @return string
*/
public function render(Formatter $formatter = null)
public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
$formatter = new PassthroughFormatter();
@@ -104,10 +106,8 @@ class Description
/**
* Returns a plain string representation of this description.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->render();
}

View File

@@ -1,18 +1,32 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function count;
use function explode;
use function implode;
use function ltrim;
use function min;
use function preg_split;
use function str_replace;
use function strlen;
use function strpos;
use function substr;
use function trim;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Creates a new Description object given a body of text.
@@ -38,8 +52,6 @@ class DescriptionFactory
/**
* Initializes this factory with the means to construct (inline) tags.
*
* @param TagFactory $tagFactory
*/
public function __construct(TagFactory $tagFactory)
{
@@ -48,27 +60,36 @@ class DescriptionFactory
/**
* Returns the parsed text of this description.
*
* @param string $contents
* @param TypeContext $context
*
* @return Description
*/
public function create($contents, TypeContext $context = null)
public function create(string $contents, ?TypeContext $context = null) : Description
{
list($text, $tags) = $this->parse($this->lex($contents), $context);
$tokens = $this->lex($contents);
$count = count($tokens);
$tagCount = 0;
$tags = [];
return new Description($text, $tags);
for ($i = 1; $i < $count; $i += 2) {
$tags[] = $this->tagFactory->create($tokens[$i], $context);
$tokens[$i] = '%' . ++$tagCount . '$s';
}
//In order to allow "literal" inline tags, the otherwise invalid
//sequence "{@}" is changed to "@", and "{}" is changed to "}".
//"%" is escaped to "%%" because of vsprintf.
//See unit tests for examples.
for ($i = 0; $i < $count; $i += 2) {
$tokens[$i] = str_replace(['{@}', '{}', '%'], ['@', '}', '%%'], $tokens[$i]);
}
return new Description(implode('', $tokens), $tags);
}
/**
* Strips the contents from superfluous whitespace and splits the description into a series of tokens.
*
* @param string $contents
*
* @return string[] A series of tokens of which the description text is composed.
*/
private function lex($contents)
private function lex(string $contents) : array
{
$contents = $this->removeSuperfluousStartingWhitespace($contents);
@@ -77,7 +98,7 @@ class DescriptionFactory
return [$contents];
}
return preg_split(
$parts = preg_split(
'/\{
# "{@}" is not a valid inline tag. This ensures that we do not treat it as one, but treat it literally.
(?!@\})
@@ -103,39 +124,12 @@ class DescriptionFactory
)
\}/Sux',
$contents,
null,
0,
PREG_SPLIT_DELIM_CAPTURE
);
}
Assert::isArray($parts);
/**
* Parses the stream of tokens in to a new set of tokens containing Tags.
*
* @param string[] $tokens
* @param TypeContext $context
*
* @return string[]|Tag[]
*/
private function parse($tokens, TypeContext $context)
{
$count = count($tokens);
$tagCount = 0;
$tags = [];
for ($i = 1; $i < $count; $i += 2) {
$tags[] = $this->tagFactory->create($tokens[$i], $context);
$tokens[$i] = '%' . ++$tagCount . '$s';
}
//In order to allow "literal" inline tags, the otherwise invalid
//sequence "{@}" is changed to "@", and "{}" is changed to "}".
//"%" is escaped to "%%" because of vsprintf.
//See unit tests for examples.
for ($i = 0; $i < $count; $i += 2) {
$tokens[$i] = str_replace(['{@}', '{}', '%'], ['@', '}', '%%'], $tokens[$i]);
}
return [implode('', $tokens), $tags];
return $parts;
}
/**
@@ -151,12 +145,8 @@ class DescriptionFactory
*
* If we do not normalize the indentation then we have superfluous whitespace on the second and subsequent
* lines and this may cause rendering issues when, for example, using a Markdown converter.
*
* @param string $contents
*
* @return string
*/
private function removeSuperfluousStartingWhitespace($contents)
private function removeSuperfluousStartingWhitespace(string $contents) : string
{
$lines = explode("\n", $contents);
@@ -168,9 +158,9 @@ class DescriptionFactory
// determine how many whitespace characters need to be stripped
$startingSpaceCount = 9999999;
for ($i = 1; $i < count($lines); $i++) {
for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) {
// lines with a no length do not count as they are not indented at all
if (strlen(trim($lines[$i])) === 0) {
if (trim($lines[$i]) === '') {
continue;
}
@@ -181,7 +171,7 @@ class DescriptionFactory
// strip the number of spaces from each line
if ($startingSpaceCount > 0) {
for ($i = 1; $i < count($lines); $i++) {
for ($i = 1, $iMax = count($lines); $i < $iMax; ++$i) {
$lines[$i] = substr($lines[$i], $startingSpaceCount);
}
}

View File

@@ -1,18 +1,28 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Tags\Example;
use function array_slice;
use function file;
use function getcwd;
use function implode;
use function is_readable;
use function rtrim;
use function sprintf;
use function trim;
use const DIRECTORY_SEPARATOR;
/**
* Class used to find an example file's location based on a given ExampleDescriptor.
@@ -27,18 +37,14 @@ class ExampleFinder
/**
* Attempts to find the example contents for the given descriptor.
*
* @param Example $example
*
* @return string
*/
public function find(Example $example)
public function find(Example $example) : string
{
$filename = $example->getFilePath();
$file = $this->getExampleFileContents($filename);
if (!$file) {
return "** File not found : {$filename} **";
return sprintf('** File not found : %s **', $filename);
}
return implode('', array_slice($file, $example->getStartingLine() - 1, $example->getLineCount()));
@@ -46,22 +52,16 @@ class ExampleFinder
/**
* Registers the project's root directory where an 'examples' folder can be expected.
*
* @param string $directory
*
* @return void
*/
public function setSourceDirectory($directory = '')
public function setSourceDirectory(string $directory = '') : void
{
$this->sourceDirectory = $directory;
}
/**
* Returns the project's root directory where an 'examples' folder can be expected.
*
* @return string
*/
public function getSourceDirectory()
public function getSourceDirectory() : string
{
return $this->sourceDirectory;
}
@@ -71,7 +71,7 @@ class ExampleFinder
*
* @param string[] $directories
*/
public function setExampleDirectories(array $directories)
public function setExampleDirectories(array $directories) : void
{
$this->exampleDirectories = $directories;
}
@@ -81,7 +81,7 @@ class ExampleFinder
*
* @return string[]
*/
public function getExampleDirectories()
public function getExampleDirectories() : array
{
return $this->exampleDirectories;
}
@@ -97,11 +97,9 @@ class ExampleFinder
* 3. Checks the 'examples' folder in the current working directory for examples
* 4. Checks the path relative to the current working directory for the given filename
*
* @param string $filename
*
* @return string|null
* @return string[] all lines of the example file
*/
private function getExampleFileContents($filename)
private function getExampleFileContents(string $filename) : ?array
{
$normalizedPath = null;
@@ -123,42 +121,31 @@ class ExampleFinder
}
}
return $normalizedPath && is_readable($normalizedPath) ? file($normalizedPath) : null;
$lines = $normalizedPath && is_readable($normalizedPath) ? file($normalizedPath) : false;
return $lines !== false ? $lines : null;
}
/**
* Get example filepath based on the example directory inside your project.
*
* @param string $file
*
* @return string
*/
private function getExamplePathFromExampleDirectory($file)
private function getExamplePathFromExampleDirectory(string $file) : string
{
return getcwd() . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $file;
}
/**
* Returns a path to the example file in the given directory..
*
* @param string $directory
* @param string $file
*
* @return string
*/
private function constructExamplePath($directory, $file)
private function constructExamplePath(string $directory, string $file) : string
{
return rtrim($directory, '\\/') . DIRECTORY_SEPARATOR . $file;
}
/**
* Get example filepath based on sourcecode.
*
* @param string $file
*
* @return string
*/
private function getExamplePathFromSource($file)
private function getExamplePathFromSource(string $file) : string
{
return sprintf(
'%s%s%s',

View File

@@ -1,19 +1,26 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock;
use Webmozart\Assert\Assert;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strlen;
use function wordwrap;
/**
* Converts a DocBlock back from an object to a complete DocComment including Asterisks.
@@ -30,33 +37,32 @@ class Serializer
protected $isFirstLineIndented = true;
/** @var int|null The max length of a line. */
protected $lineLength = null;
protected $lineLength;
/** @var DocBlock\Tags\Formatter A custom tag formatter. */
protected $tagFormatter = null;
/** @var Formatter A custom tag formatter. */
protected $tagFormatter;
/**
* Create a Serializer instance.
*
* @param int $indent The number of times the indent string is repeated.
* @param string $indentString The string to indent the comment with.
* @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
* @param DocBlock\Tags\Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
* @param int $indent The number of times the indent string is repeated.
* @param string $indentString The string to indent the comment with.
* @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
* @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
*/
public function __construct($indent = 0, $indentString = ' ', $indentFirstLine = true, $lineLength = null, $tagFormatter = null)
{
Assert::integer($indent);
Assert::string($indentString);
Assert::boolean($indentFirstLine);
Assert::nullOrInteger($lineLength);
Assert::nullOrIsInstanceOf($tagFormatter, 'phpDocumentor\Reflection\DocBlock\Tags\Formatter');
$this->indent = $indent;
$this->indentString = $indentString;
public function __construct(
int $indent = 0,
string $indentString = ' ',
bool $indentFirstLine = true,
?int $lineLength = null,
?Formatter $tagFormatter = null
) {
$this->indent = $indent;
$this->indentString = $indentString;
$this->isFirstLineIndented = $indentFirstLine;
$this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new DocBlock\Tags\Formatter\PassthroughFormatter();
$this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
}
/**
@@ -66,9 +72,9 @@ class Serializer
*
* @return string The serialized doc block.
*/
public function getDocComment(DocBlock $docblock)
public function getDocComment(DocBlock $docblock) : string
{
$indent = str_repeat($this->indentString, $this->indent);
$indent = str_repeat($this->indentString, $this->indent);
$firstIndent = $this->isFirstLineIndented ? $indent : '';
// 3 === strlen(' * ')
$wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null;
@@ -81,63 +87,49 @@ class Serializer
)
);
$comment = "{$firstIndent}/**\n";
$comment = $firstIndent . "/**\n";
if ($text) {
$comment .= "{$indent} * {$text}\n";
$comment .= "{$indent} *\n";
$comment .= $indent . ' * ' . $text . "\n";
$comment .= $indent . " *\n";
}
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
$comment .= $indent . ' */';
return $comment;
return $comment . $indent . ' */';
}
/**
* @param $indent
* @param $text
* @return mixed
*/
private function removeTrailingSpaces($indent, $text)
private function removeTrailingSpaces(string $indent, string $text) : string
{
return str_replace("\n{$indent} * \n", "\n{$indent} *\n", $text);
return str_replace(
sprintf("\n%s * \n", $indent),
sprintf("\n%s *\n", $indent),
$text
);
}
/**
* @param $indent
* @param $text
* @return mixed
*/
private function addAsterisksForEachLine($indent, $text)
private function addAsterisksForEachLine(string $indent, string $text) : string
{
return str_replace("\n", "\n{$indent} * ", $text);
return str_replace(
"\n",
sprintf("\n%s * ", $indent),
$text
);
}
/**
* @param DocBlock $docblock
* @param $wrapLength
* @return string
*/
private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, $wrapLength)
private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength) : string
{
$text = $docblock->getSummary() . ((string)$docblock->getDescription() ? "\n\n" . $docblock->getDescription()
$text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription()
: '');
if ($wrapLength !== null) {
$text = wordwrap($text, $wrapLength);
return $text;
}
return $text;
}
/**
* @param DocBlock $docblock
* @param $wrapLength
* @param $indent
* @param $comment
* @return string
*/
private function addTagBlock(DocBlock $docblock, $wrapLength, $indent, $comment)
private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment) : string
{
foreach ($docblock->getTags() as $tag) {
$tagText = $this->tagFormatter->format($tag);
@@ -145,9 +137,13 @@ class Serializer
$tagText = wordwrap($tagText, $wrapLength);
}
$tagText = str_replace("\n", "\n{$indent} * ", $tagText);
$tagText = str_replace(
"\n",
sprintf("\n%s * ", $indent),
$tagText
);
$comment .= "{$indent} * {$tagText}\n";
$comment .= sprintf("%s * %s\n", $indent, $tagText);
}
return $comment;

View File

@@ -1,22 +1,52 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use InvalidArgumentException;
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlock\Tags\Covers;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Link as LinkTag;
use phpDocumentor\Reflection\DocBlock\Tags\Method;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\DocBlock\Tags\Property;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\DocBlock\Tags\See as SeeTag;
use phpDocumentor\Reflection\DocBlock\Tags\Since;
use phpDocumentor\Reflection\DocBlock\Tags\Source;
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
use phpDocumentor\Reflection\DocBlock\Tags\Uses;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlock\Tags\Version;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use ReflectionMethod;
use ReflectionParameter;
use Webmozart\Assert\Assert;
use function array_merge;
use function array_slice;
use function call_user_func_array;
use function count;
use function get_class;
use function preg_match;
use function strpos;
use function trim;
/**
* Creates a Tag object given the contents of a tag.
@@ -38,41 +68,47 @@ use Webmozart\Assert\Assert;
final class StandardTagFactory implements TagFactory
{
/** PCRE regular expression matching a tag name. */
const REGEX_TAGNAME = '[\w\-\_\\\\]+';
public const REGEX_TAGNAME = '[\w\-\_\\\\:]+';
/**
* @var string[] An array with a tag as a key, and an FQCN to a class that handles it as an array value.
* @var array<class-string<Tag>> An array with a tag as a key, and an
* FQCN to a class that handles it as an array value.
*/
private $tagHandlerMappings = [
'author' => '\phpDocumentor\Reflection\DocBlock\Tags\Author',
'covers' => '\phpDocumentor\Reflection\DocBlock\Tags\Covers',
'deprecated' => '\phpDocumentor\Reflection\DocBlock\Tags\Deprecated',
'author' => Author::class,
'covers' => Covers::class,
'deprecated' => Deprecated::class,
// 'example' => '\phpDocumentor\Reflection\DocBlock\Tags\Example',
'link' => '\phpDocumentor\Reflection\DocBlock\Tags\Link',
'method' => '\phpDocumentor\Reflection\DocBlock\Tags\Method',
'param' => '\phpDocumentor\Reflection\DocBlock\Tags\Param',
'property-read' => '\phpDocumentor\Reflection\DocBlock\Tags\PropertyRead',
'property' => '\phpDocumentor\Reflection\DocBlock\Tags\Property',
'property-write' => '\phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite',
'return' => '\phpDocumentor\Reflection\DocBlock\Tags\Return_',
'see' => '\phpDocumentor\Reflection\DocBlock\Tags\See',
'since' => '\phpDocumentor\Reflection\DocBlock\Tags\Since',
'source' => '\phpDocumentor\Reflection\DocBlock\Tags\Source',
'throw' => '\phpDocumentor\Reflection\DocBlock\Tags\Throws',
'throws' => '\phpDocumentor\Reflection\DocBlock\Tags\Throws',
'uses' => '\phpDocumentor\Reflection\DocBlock\Tags\Uses',
'var' => '\phpDocumentor\Reflection\DocBlock\Tags\Var_',
'version' => '\phpDocumentor\Reflection\DocBlock\Tags\Version'
'link' => LinkTag::class,
'method' => Method::class,
'param' => Param::class,
'property-read' => PropertyRead::class,
'property' => Property::class,
'property-write' => PropertyWrite::class,
'return' => Return_::class,
'see' => SeeTag::class,
'since' => Since::class,
'source' => Source::class,
'throw' => Throws::class,
'throws' => Throws::class,
'uses' => Uses::class,
'var' => Var_::class,
'version' => Version::class,
];
/**
* @var \ReflectionParameter[][] a lazy-loading cache containing parameters for each tagHandler that has been used.
* @var array<class-string<Tag>> An array with a anotation s a key, and an
* FQCN to a class that handles it as an array value.
*/
private $annotationMappings = [];
/**
* @var ReflectionParameter[][] a lazy-loading cache containing parameters
* for each tagHandler that has been used.
*/
private $tagHandlerParameterCache = [];
/**
* @var FqsenResolver
*/
/** @var FqsenResolver */
private $fqsenResolver;
/**
@@ -87,12 +123,11 @@ final class StandardTagFactory implements TagFactory
* If no tag handlers are provided than the default list in the {@see self::$tagHandlerMappings} property
* is used.
*
* @param FqsenResolver $fqsenResolver
* @param string[] $tagHandlers
*
* @see self::registerTagHandler() to add a new tag handler to the existing default list.
*
* @param array<class-string<Tag>> $tagHandlers
*/
public function __construct(FqsenResolver $fqsenResolver, array $tagHandlers = null)
public function __construct(FqsenResolver $fqsenResolver, ?array $tagHandlers = null)
{
$this->fqsenResolver = $fqsenResolver;
if ($tagHandlers !== null) {
@@ -102,54 +137,38 @@ final class StandardTagFactory implements TagFactory
$this->addService($fqsenResolver, FqsenResolver::class);
}
/**
* {@inheritDoc}
*/
public function create($tagLine, TypeContext $context = null)
public function create(string $tagLine, ?TypeContext $context = null) : Tag
{
if (! $context) {
if (!$context) {
$context = new TypeContext('');
}
list($tagName, $tagBody) = $this->extractTagParts($tagLine);
[$tagName, $tagBody] = $this->extractTagParts($tagLine);
if ($tagBody !== '' && $tagBody[0] === '[') {
throw new \InvalidArgumentException(
'The tag "' . $tagLine . '" does not seem to be wellformed, please check it for errors'
);
}
return $this->createTag($tagBody, $tagName, $context);
return $this->createTag(trim($tagBody), $tagName, $context);
}
/**
* {@inheritDoc}
* @param mixed $value
*/
public function addParameter($name, $value)
public function addParameter(string $name, $value) : void
{
$this->serviceLocator[$name] = $value;
}
/**
* {@inheritDoc}
*/
public function addService($service, $alias = null)
public function addService(object $service, ?string $alias = null) : void
{
$this->serviceLocator[$alias ?: get_class($service)] = $service;
}
/**
* {@inheritDoc}
*/
public function registerTagHandler($tagName, $handler)
public function registerTagHandler(string $tagName, string $handler) : void
{
Assert::stringNotEmpty($tagName);
Assert::stringNotEmpty($handler);
Assert::classExists($handler);
Assert::implementsInterface($handler, StaticMethod::class);
if (strpos($tagName, '\\') && $tagName[0] !== '\\') {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'A namespaced tag must have a leading backslash as it must be fully qualified'
);
}
@@ -160,15 +179,13 @@ final class StandardTagFactory implements TagFactory
/**
* Extracts all components for a tag.
*
* @param string $tagLine
*
* @return string[]
*/
private function extractTagParts($tagLine)
private function extractTagParts(string $tagLine) : array
{
$matches = [];
if (! preg_match('/^@(' . self::REGEX_TAGNAME . ')(?:\s*([^\s].*)|$)/us', $tagLine, $matches)) {
throw new \InvalidArgumentException(
if (!preg_match('/^@(' . self::REGEX_TAGNAME . ')((?:[\s\(\{])\s*([^\s].*)|$)/us', $tagLine, $matches)) {
throw new InvalidArgumentException(
'The tag "' . $tagLine . '" does not seem to be wellformed, please check it for errors'
);
}
@@ -183,14 +200,8 @@ final class StandardTagFactory implements TagFactory
/**
* Creates a new tag object with the given name and body or returns null if the tag name was recognized but the
* body was invalid.
*
* @param string $body
* @param string $name
* @param TypeContext $context
*
* @return Tag|null
*/
private function createTag($body, $name, TypeContext $context)
private function createTag(string $body, string $name, TypeContext $context) : Tag
{
$handlerClassName = $this->findHandlerClassName($name, $context);
$arguments = $this->getArgumentsForParametersFromWiring(
@@ -198,28 +209,34 @@ final class StandardTagFactory implements TagFactory
$this->getServiceLocatorWithDynamicParameters($context, $name, $body)
);
return call_user_func_array([$handlerClassName, 'create'], $arguments);
try {
$callable = [$handlerClassName, 'create'];
Assert::isCallable($callable);
/** @phpstan-var callable(string): ?Tag $callable */
$tag = call_user_func_array($callable, $arguments);
return $tag ?? InvalidTag::create($body, $name);
} catch (InvalidArgumentException $e) {
return InvalidTag::create($body, $name)->withError($e);
}
}
/**
* Determines the Fully Qualified Class Name of the Factory or Tag (containing a Factory Method `create`).
*
* @param string $tagName
* @param TypeContext $context
*
* @return string
* @return class-string<Tag>
*/
private function findHandlerClassName($tagName, TypeContext $context)
private function findHandlerClassName(string $tagName, TypeContext $context) : string
{
$handlerClassName = Generic::class;
if (isset($this->tagHandlerMappings[$tagName])) {
$handlerClassName = $this->tagHandlerMappings[$tagName];
} elseif ($this->isAnnotation($tagName)) {
// TODO: Annotation support is planned for a later stage and as such is disabled for now
// $tagName = (string)$this->fqsenResolver->resolve($tagName, $context);
// if (isset($this->annotationMappings[$tagName])) {
// $handlerClassName = $this->annotationMappings[$tagName];
// }
$tagName = (string) $this->fqsenResolver->resolve($tagName, $context);
if (isset($this->annotationMappings[$tagName])) {
$handlerClassName = $this->annotationMappings[$tagName];
}
}
return $handlerClassName;
@@ -228,17 +245,22 @@ final class StandardTagFactory implements TagFactory
/**
* Retrieves the arguments that need to be passed to the Factory Method with the given Parameters.
*
* @param \ReflectionParameter[] $parameters
* @param mixed[] $locator
* @param ReflectionParameter[] $parameters
* @param mixed[] $locator
*
* @return mixed[] A series of values that can be passed to the Factory Method of the tag whose parameters
* is provided with this method.
*/
private function getArgumentsForParametersFromWiring($parameters, $locator)
private function getArgumentsForParametersFromWiring(array $parameters, array $locator) : array
{
$arguments = [];
foreach ($parameters as $index => $parameter) {
$typeHint = $parameter->getClass() ? $parameter->getClass()->getName() : null;
foreach ($parameters as $parameter) {
$class = $parameter->getClass();
$typeHint = null;
if ($class !== null) {
$typeHint = $class->getName();
}
if (isset($locator[$typeHint])) {
$arguments[] = $locator[$typeHint];
continue;
@@ -260,14 +282,12 @@ final class StandardTagFactory implements TagFactory
* Retrieves a series of ReflectionParameter objects for the static 'create' method of the given
* tag handler class name.
*
* @param string $handlerClassName
*
* @return \ReflectionParameter[]
* @return ReflectionParameter[]
*/
private function fetchParametersForHandlerFactoryMethod($handlerClassName)
private function fetchParametersForHandlerFactoryMethod(string $handlerClassName) : array
{
if (! isset($this->tagHandlerParameterCache[$handlerClassName])) {
$methodReflection = new \ReflectionMethod($handlerClassName, 'create');
if (!isset($this->tagHandlerParameterCache[$handlerClassName])) {
$methodReflection = new ReflectionMethod($handlerClassName, 'create');
$this->tagHandlerParameterCache[$handlerClassName] = $methodReflection->getParameters();
}
@@ -275,39 +295,39 @@ final class StandardTagFactory implements TagFactory
}
/**
* Returns a copy of this class' Service Locator with added dynamic parameters, such as the tag's name, body and
* Context.
* Returns a copy of this class' Service Locator with added dynamic parameters,
* such as the tag's name, body and Context.
*
* @param TypeContext $context The Context (namespace and aliasses) that may be passed and is used to resolve FQSENs.
* @param string $tagName The name of the tag that may be passed onto the factory method of the Tag class.
* @param string $tagBody The body of the tag that may be passed onto the factory method of the Tag class.
* @param TypeContext $context The Context (namespace and aliasses) that may be
* passed and is used to resolve FQSENs.
* @param string $tagName The name of the tag that may be
* passed onto the factory method of the Tag class.
* @param string $tagBody The body of the tag that may be
* passed onto the factory method of the Tag class.
*
* @return mixed[]
*/
private function getServiceLocatorWithDynamicParameters(TypeContext $context, $tagName, $tagBody)
{
$locator = array_merge(
private function getServiceLocatorWithDynamicParameters(
TypeContext $context,
string $tagName,
string $tagBody
) : array {
return array_merge(
$this->serviceLocator,
[
'name' => $tagName,
'body' => $tagBody,
TypeContext::class => $context
'name' => $tagName,
'body' => $tagBody,
TypeContext::class => $context,
]
);
return $locator;
}
/**
* Returns whether the given tag belongs to an annotation.
*
* @param string $tagContent
*
* @todo this method should be populated once we implement Annotation notation support.
*
* @return bool
*/
private function isAnnotation($tagContent)
private function isAnnotation(string $tagContent) : bool
{
// 1. Contains a namespace separator
// 2. Contains parenthesis

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -16,11 +17,16 @@ use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
interface Tag
{
public function getName();
public function getName() : string;
public static function create($body);
/**
* @return Tag|mixed Class that implements Tag
*
* @phpstan-return ?Tag
*/
public static function create(string $body);
public function render(Formatter $formatter = null);
public function render(?Formatter $formatter = null) : string;
public function __toString();
public function __toString() : string;
}

View File

@@ -1,17 +1,19 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use InvalidArgumentException;
use phpDocumentor\Reflection\Types\Context as TypeContext;
interface TagFactory
@@ -34,12 +36,20 @@ interface TagFactory
*
* These parameters are injected at the last moment and will override any existing parameter with those names.
*
* @param string $name
* @param mixed $value
*
* @return void
* @param mixed $value
*/
public function addParameter($name, $value);
public function addParameter(string $name, $value) : void;
/**
* Factory method responsible for instantiating the correct sub type.
*
* @param string $tagLine The text for this tag, including description.
*
* @return Tag A new tag object.
*
* @throws InvalidArgumentException If an invalid tag line was presented.
*/
public function create(string $tagLine, ?TypeContext $context = null) : Tag;
/**
* Registers a service with the Service Locator using the FQCN of the class or the alias, if provided.
@@ -49,45 +59,26 @@ interface TagFactory
*
* Because interfaces are regularly used as type-hints this method provides an alias parameter; if the FQCN of the
* interface is passed as alias then every time that interface is requested the provided service will be returned.
*
* @param object $service
* @param string $alias
*
* @return void
*/
public function addService($service);
/**
* Factory method responsible for instantiating the correct sub type.
*
* @param string $tagLine The text for this tag, including description.
* @param TypeContext $context
*
* @throws \InvalidArgumentException if an invalid tag line was presented.
*
* @return Tag A new tag object.
*/
public function create($tagLine, TypeContext $context = null);
public function addService(object $service) : void;
/**
* Registers a handler for tags.
*
* If you want to use your own tags then you can use this method to instruct the TagFactory to register the name
* of a tag with the FQCN of a 'Tag Handler'. The Tag handler should implement the {@see Tag} interface (and thus
* the create method).
* If you want to use your own tags then you can use this method to instruct the TagFactory
* to register the name of a tag with the FQCN of a 'Tag Handler'. The Tag handler should implement
* the {@see Tag} interface (and thus the create method).
*
* @param string $tagName Name of tag to register a handler for. When registering a namespaced tag, the full
* name, along with a prefixing slash MUST be provided.
* @param string $handler FQCN of handler.
* @param string $tagName Name of tag to register a handler for. When registering a namespaced
* tag, the full name, along with a prefixing slash MUST be provided.
* @param class-string<Tag> $handler FQCN of handler.
*
* @throws \InvalidArgumentException if the tag name is not a string
* @throws \InvalidArgumentException if the tag name is namespaced (contains backslashes) but does not start with
* a backslash
* @throws \InvalidArgumentException if the handler is not a string
* @throws \InvalidArgumentException if the handler is not an existing class
* @throws \InvalidArgumentException if the handler does not implement the {@see Tag} interface
*
* @return void
* @throws InvalidArgumentException If the tag name is not a string.
* @throws InvalidArgumentException If the tag name is namespaced (contains backslashes) but
* does not start with a backslash.
* @throws InvalidArgumentException If the handler is not a string.
* @throws InvalidArgumentException If the handler is not an existing class.
* @throws InvalidArgumentException If the handler does not implement the {@see Tag} interface.
*/
public function registerTagHandler($tagName, $handler);
public function registerTagHandler(string $tagName, string $handler) : void;
}

View File

@@ -1,18 +1,23 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use Webmozart\Assert\Assert;
use InvalidArgumentException;
use function filter_var;
use function preg_match;
use function trim;
use const FILTER_VALIDATE_EMAIL;
/**
* Reflection class for an {@}author tag in a Docblock.
@@ -23,23 +28,18 @@ final class Author extends BaseTag implements Factory\StaticMethod
protected $name = 'author';
/** @var string The name of the author */
private $authorName = '';
private $authorName;
/** @var string The email of the author */
private $authorEmail = '';
private $authorEmail;
/**
* Initializes this tag with the author name and e-mail.
*
* @param string $authorName
* @param string $authorEmail
*/
public function __construct($authorName, $authorEmail)
public function __construct(string $authorName, string $authorEmail)
{
Assert::string($authorName);
Assert::string($authorEmail);
if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('The author tag does not have a valid e-mail address');
throw new InvalidArgumentException('The author tag does not have a valid e-mail address');
}
$this->authorName = $authorName;
@@ -51,7 +51,7 @@ final class Author extends BaseTag implements Factory\StaticMethod
*
* @return string The author's name.
*/
public function getAuthorName()
public function getAuthorName() : string
{
return $this->authorName;
}
@@ -61,39 +61,31 @@ final class Author extends BaseTag implements Factory\StaticMethod
*
* @return string The author's email.
*/
public function getEmail()
public function getEmail() : string
{
return $this->authorEmail;
}
/**
* Returns this tag in string form.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->authorName . (strlen($this->authorEmail) ? ' <' . $this->authorEmail . '>' : '');
return $this->authorName . ($this->authorEmail !== '' ? ' <' . $this->authorEmail . '>' : '');
}
/**
* Attempts to create a new Author object based on †he tag body.
*
* @param string $body
*
* @return static
*/
public static function create($body)
public static function create(string $body) : ?self
{
Assert::string($body);
$splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches);
if (!$splitTagContent) {
return null;
}
$authorName = trim($matches[1]);
$email = isset($matches[2]) ? trim($matches[2]) : '';
$email = isset($matches[2]) ? trim($matches[2]) : '';
return new static($authorName, $email);
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -31,17 +32,17 @@ abstract class BaseTag implements DocBlock\Tag
*
* @return string The name of this tag.
*/
public function getName()
public function getName() : string
{
return $this->name;
}
public function getDescription()
public function getDescription() : ?Description
{
return $this->description;
}
public function render(Formatter $formatter = null)
public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
$formatter = new Formatter\PassthroughFormatter();

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -18,65 +19,59 @@ use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_split;
/**
* Reflection class for a @covers tag in a Docblock.
*/
final class Covers extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'covers';
/** @var Fqsen */
private $refers = null;
private $refers;
/**
* Initializes this tag.
*
* @param Fqsen $refers
* @param Description $description
*/
public function __construct(Fqsen $refers, Description $description = null)
public function __construct(Fqsen $refers, ?Description $description = null)
{
$this->refers = $refers;
$this->refers = $refers;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
DescriptionFactory $descriptionFactory = null,
FqsenResolver $resolver = null,
TypeContext $context = null
) {
Assert::string($body);
string $body,
?DescriptionFactory $descriptionFactory = null,
?FqsenResolver $resolver = null,
?TypeContext $context = null
) : self {
Assert::notEmpty($body);
Assert::notNull($descriptionFactory);
Assert::notNull($resolver);
$parts = preg_split('/\s+/Su', $body, 2);
Assert::isArray($parts);
return new static(
$resolver->resolve($parts[0], $context),
$descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
$descriptionFactory->create($parts[1] ?? '', $context)
);
}
/**
* Returns the structural element this tag refers to.
*
* @return Fqsen
*/
public function getReference()
public function getReference() : Fqsen
{
return $this->refers;
}
/**
* Returns a string representation of this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -16,19 +17,21 @@ use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
/**
* Reflection class for a {@}deprecated tag in a Docblock.
*/
final class Deprecated extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'deprecated';
/**
* PCRE regular expression matching a version vector.
* Assumes the "x" modifier.
*/
const REGEX_VECTOR = '(?:
public const REGEX_VECTOR = '(?:
# Normal release vectors.
\d\S*
|
@@ -40,23 +43,25 @@ final class Deprecated extends BaseTag implements Factory\StaticMethod
[^\s\:]+\:\s*\$[^\$]+\$
)';
/** @var string The version vector. */
private $version = '';
/** @var string|null The version vector. */
private $version;
public function __construct($version = null, Description $description = null)
public function __construct(?string $version = null, ?Description $description = null)
{
Assert::nullOrStringNotEmpty($version);
$this->version = $version;
$this->version = $version;
$this->description = $description;
}
/**
* @return static
*/
public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
{
Assert::nullOrString($body);
public static function create(
?string $body,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
if (empty($body)) {
return new static();
}
@@ -65,33 +70,31 @@ final class Deprecated extends BaseTag implements Factory\StaticMethod
if (!preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
return new static(
null,
null !== $descriptionFactory ? $descriptionFactory->create($body, $context) : null
$descriptionFactory !== null ? $descriptionFactory->create($body, $context) : null
);
}
Assert::notNull($descriptionFactory);
return new static(
$matches[1],
$descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
$descriptionFactory->create($matches[2] ?? '', $context)
);
}
/**
* Gets the version section of the tag.
*
* @return string
*/
public function getVersion()
public function getVersion() : ?string
{
return $this->version;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
return ($this->version ?? '') . ($this->description ? ' ' . $this->description->render() : '');
}
}

View File

@@ -1,103 +1,104 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use Webmozart\Assert\Assert;
use function array_key_exists;
use function preg_match;
use function rawurlencode;
use function str_replace;
use function strpos;
use function trim;
/**
* Reflection class for a {@}example tag in a Docblock.
*/
final class Example extends BaseTag
final class Example implements Tag, Factory\StaticMethod
{
/**
* @var string Path to a file to use as an example. May also be an absolute URI.
*/
/** @var string Path to a file to use as an example. May also be an absolute URI. */
private $filePath;
/**
* @var bool Whether the file path component represents an URI. This determines how the file portion
* appears at {@link getContent()}.
*/
private $isURI = false;
private $isURI;
/**
* @var int
*/
/** @var int */
private $startingLine;
/**
* @var int
*/
/** @var int */
private $lineCount;
public function __construct($filePath, $isURI, $startingLine, $lineCount, $description)
/** @var string|null */
private $content;
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content)
{
Assert::notEmpty($filePath);
Assert::integer($startingLine);
Assert::greaterThanEq($startingLine, 0);
Assert::greaterThanEq($lineCount, 0);
$this->filePath = $filePath;
$this->filePath = $filePath;
$this->startingLine = $startingLine;
$this->lineCount = $lineCount;
$this->name = 'example';
if ($description !== null) {
$this->description = trim($description);
$this->lineCount = $lineCount;
if ($content !== null) {
$this->content = trim($content);
}
$this->isURI = $isURI;
}
/**
* {@inheritdoc}
*/
public function getContent()
public function getContent() : string
{
if (null === $this->description) {
if ($this->content === null) {
$filePath = '"' . $this->filePath . '"';
if ($this->isURI) {
$filePath = $this->isUriRelative($this->filePath)
? str_replace('%2F', '/', rawurlencode($this->filePath))
:$this->filePath;
: $this->filePath;
}
return trim($filePath . ' ' . parent::getDescription());
return trim($filePath);
}
return $this->description;
return $this->content;
}
/**
* {@inheritdoc}
*/
public static function create($body)
public function getDescription() : ?string
{
return $this->content;
}
public static function create(string $body) : ?Tag
{
// File component: File path in quotes or File URI / Source information
if (! preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
if (!preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
return null;
}
$filePath = null;
$fileUri = null;
if ('' !== $matches[1]) {
if ($matches[1] !== '') {
$filePath = $matches[1];
} else {
$fileUri = $matches[2];
}
$startingLine = 1;
$lineCount = null;
$lineCount = 0;
$description = null;
if (array_key_exists(3, $matches)) {
@@ -105,9 +106,9 @@ final class Example extends BaseTag
// Starting line / Number of lines / Description
if (preg_match('/^([1-9]\d*)(?:\s+((?1))\s*)?(.*)$/sux', $matches[3], $contentMatches)) {
$startingLine = (int)$contentMatches[1];
$startingLine = (int) $contentMatches[1];
if (isset($contentMatches[2]) && $contentMatches[2] !== '') {
$lineCount = (int)$contentMatches[2];
$lineCount = (int) $contentMatches[2];
}
if (array_key_exists(3, $contentMatches)) {
@@ -117,7 +118,7 @@ final class Example extends BaseTag
}
return new static(
$filePath !== null?$filePath:$fileUri,
$filePath ?? ($fileUri ?? ''),
$fileUri !== null,
$startingLine,
$lineCount,
@@ -131,46 +132,48 @@ final class Example extends BaseTag
* @return string Path to a file to use as an example.
* May also be an absolute URI.
*/
public function getFilePath()
public function getFilePath() : string
{
return $this->filePath;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->filePath . ($this->description ? ' ' . $this->description : '');
return $this->filePath . ($this->content ? ' ' . $this->content : '');
}
/**
* Returns true if the provided URI is relative or contains a complete scheme (and thus is absolute).
*
* @param string $uri
*
* @return bool
*/
private function isUriRelative($uri)
private function isUriRelative(string $uri) : bool
{
return false === strpos($uri, ':');
return strpos($uri, ':') === false;
}
/**
* @return int
*/
public function getStartingLine()
public function getStartingLine() : int
{
return $this->startingLine;
}
/**
* @return int
*/
public function getLineCount()
public function getLineCount() : int
{
return $this->lineCount;
}
public function getName() : string
{
return 'example';
}
public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
$formatter = new Formatter\PassthroughFormatter();
}
return $formatter->format($this);
}
}

View File

@@ -1,18 +1,25 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
/**
* @deprecated This contract is totally covered by Tag contract. Every class using StaticMethod also use Tag
*/
interface StaticMethod
{
public static function create($body);
/**
* @return mixed
*/
public static function create(string $body);
}

View File

@@ -1,18 +0,0 @@
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
interface Strategy
{
public function create($body);
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,10 +19,6 @@ interface Formatter
{
/**
* Formats a tag into a string representation according to a specific format, such as Markdown.
*
* @param Tag $tag
*
* @return string
*/
public function format(Tag $tag);
public function format(Tag $tag) : string;
}

View File

@@ -1,13 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Jan Schneider <jan@horde.org>
* @copyright 2017 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -15,6 +15,9 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use function max;
use function str_repeat;
use function strlen;
class AlignFormatter implements Formatter
{
@@ -22,8 +25,6 @@ class AlignFormatter implements Formatter
protected $maxLen = 0;
/**
* Constructor.
*
* @param Tag[] $tags All tags that should later be aligned with the formatter.
*/
public function __construct(array $tags)
@@ -35,13 +36,14 @@ class AlignFormatter implements Formatter
/**
* Formats the given tag to return a simple plain text version.
*
* @param Tag $tag
*
* @return string
*/
public function format(Tag $tag)
public function format(Tag $tag) : string
{
return '@' . $tag->getName() . str_repeat(' ', $this->maxLen - strlen($tag->getName()) + 1) . (string)$tag;
return '@' . $tag->getName() .
str_repeat(
' ',
$this->maxLen - strlen($tag->getName()) + 1
) .
$tag;
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -14,18 +15,15 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use function trim;
class PassthroughFormatter implements Formatter
{
/**
* Formats the given tag to return a simple plain text version.
*
* @param Tag $tag
*
* @return string
*/
public function format(Tag $tag)
public function format(Tag $tag) : string
{
return trim('@' . $tag->getName() . ' ' . (string)$tag);
return trim('@' . $tag->getName() . ' ' . $tag);
}
}

View File

@@ -1,88 +1,79 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use InvalidArgumentException;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
/**
* Parses a tag definition for a DocBlock.
*/
class Generic extends BaseTag implements Factory\StaticMethod
final class Generic extends BaseTag implements Factory\StaticMethod
{
/**
* Parses a tag and populates the member variables.
*
* @param string $name Name of the tag.
* @param string $name Name of the tag.
* @param Description $description The contents of the given tag.
*/
public function __construct($name, Description $description = null)
public function __construct(string $name, ?Description $description = null)
{
$this->validateTagName($name);
$this->name = $name;
$this->name = $name;
$this->description = $description;
}
/**
* Creates a new tag that represents any unknown tag type.
*
* @param string $body
* @param string $name
* @param DescriptionFactory $descriptionFactory
* @param TypeContext $context
*
* @return static
*/
public static function create(
$body,
$name = '',
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
Assert::string($body);
string $body,
string $name = '',
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($name);
Assert::notNull($descriptionFactory);
$description = $descriptionFactory && $body !== "" ? $descriptionFactory->create($body, $context) : null;
$description = $body !== '' ? $descriptionFactory->create($body, $context) : null;
return new static($name, $description);
}
/**
* Returns the tag as a serialized string
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->description ? $this->description->render() : '');
return $this->description ? $this->description->render() : '';
}
/**
* Validates if the tag name matches the expected format, otherwise throws an exception.
*
* @param string $name
*
* @return void
*/
private function validateTagName($name)
private function validateTagName(string $name) : void
{
if (! preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) {
throw new \InvalidArgumentException(
if (!preg_match('/^' . StandardTagFactory::REGEX_TAGNAME . '$/u', $name)) {
throw new InvalidArgumentException(
'The tag name "' . $name . '" is not wellformed. Tags may only consist of letters, underscores, '
. 'hyphens and backslashes.'
);

View File

@@ -0,0 +1,133 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags;
use Closure;
use Exception;
use phpDocumentor\Reflection\DocBlock\Tag;
use ReflectionClass;
use ReflectionFunction;
use Throwable;
use function array_map;
use function array_walk_recursive;
use function get_class;
use function get_resource_type;
use function is_object;
use function is_resource;
use function sprintf;
/**
* This class represents an exception during the tag creation
*
* Since the internals of the library are relaying on the correct syntax of a docblock
* we cannot simply throw exceptions at all time because the exceptions will break the creation of a
* docklock. Just silently ignore the exceptions is not an option because the user as an issue to fix.
*
* This tag holds that error information until a using application is able to display it. The object wil just behave
* like any normal tag. So the normal application flow will not break.
*/
final class InvalidTag implements Tag
{
/** @var string */
private $name;
/** @var string */
private $body;
/** @var Throwable|null */
private $throwable;
private function __construct(string $name, string $body)
{
$this->name = $name;
$this->body = $body;
}
public function getException() : ?Throwable
{
return $this->throwable;
}
public function getName() : string
{
return $this->name;
}
public static function create(string $body, string $name = '') : self
{
return new self($name, $body);
}
public function withError(Throwable $exception) : self
{
$this->flattenExceptionBacktrace($exception);
$tag = new self($this->name, $this->body);
$tag->throwable = $exception;
return $tag;
}
/**
* Removes all complex types from backtrace
*
* Not all objects are serializable. So we need to remove them from the
* stored exception to be sure that we do not break existing library usage.
*/
private function flattenExceptionBacktrace(Throwable $exception) : void
{
$traceProperty = (new ReflectionClass(Exception::class))->getProperty('trace');
$traceProperty->setAccessible(true);
$flatten =
/** @param mixed $value */
static function (&$value) : void {
if ($value instanceof Closure) {
$closureReflection = new ReflectionFunction($value);
$value = sprintf(
'(Closure at %s:%s)',
$closureReflection->getFileName(),
$closureReflection->getStartLine()
);
} elseif (is_object($value)) {
$value = sprintf('object(%s)', get_class($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
}
};
do {
$trace = $exception->getTrace();
if (isset($trace[0]['args'])) {
$trace = array_map(
static function (array $call) use ($flatten) : array {
array_walk_recursive($call['args'], $flatten);
return $call;
},
$trace
);
}
$traceProperty->setValue($exception, $trace);
$exception = $exception->getPrevious();
} while ($exception !== null);
$traceProperty->setAccessible(false);
}
public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
$formatter = new Formatter\PassthroughFormatter();
}
return $formatter->format($this);
}
public function __toString() : string
{
return $this->body;
}
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* phpDocumentor
* This file is part of phpDocumentor.
*
* PHP Version 5.3
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Ben Selby <benmatselby@gmail.com>
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -16,61 +17,54 @@ use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_split;
/**
* Reflection class for a @link tag in a Docblock.
*/
final class Link extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'link';
/** @var string */
private $link = '';
private $link;
/**
* Initializes a link to a URL.
*
* @param string $link
* @param Description $description
*/
public function __construct($link, Description $description = null)
public function __construct(string $link, ?Description $description = null)
{
Assert::string($link);
$this->link = $link;
$this->link = $link;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
{
Assert::string($body);
public static function create(
string $body,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
Assert::isArray($parts);
$description = isset($parts[1]) ? $descriptionFactory->create($parts[1], $context) : null;
return new static($parts[0], $description);
}
/**
* Gets the link
*
* @return string
*/
public function getLink()
* Gets the link
*/
public function getLink() : string
{
return $this->link;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->link . ($this->description ? ' ' . $this->description->render() : '');
}

View File

@@ -1,53 +1,74 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use InvalidArgumentException;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\Void_;
use Webmozart\Assert\Assert;
use function array_keys;
use function explode;
use function implode;
use function is_string;
use function preg_match;
use function sort;
use function strpos;
use function substr;
use function trim;
use function var_export;
/**
* Reflection class for an {@}method in a Docblock.
*/
final class Method extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'method';
/** @var string */
private $methodName = '';
private $methodName;
/** @var string[] */
private $arguments = [];
/**
* @phpstan-var array<int, array{name: string, type: Type}>
* @var array<int, array<string, Type|string>>
*/
private $arguments;
/** @var bool */
private $isStatic = false;
private $isStatic;
/** @var Type */
private $returnType;
/**
* @param array<int, array<string, Type|string>> $arguments
*
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
*/
public function __construct(
$methodName,
string $methodName,
array $arguments = [],
Type $returnType = null,
$static = false,
Description $description = null
?Type $returnType = null,
bool $static = false,
?Description $description = null
) {
Assert::stringNotEmpty($methodName);
Assert::boolean($static);
if ($returnType === null) {
$returnType = new Void_();
@@ -60,17 +81,15 @@ final class Method extends BaseTag implements Factory\StaticMethod
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : ?self {
Assert::stringNotEmpty($body);
Assert::allNotNull([ $typeResolver, $descriptionFactory ]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
// 1. none or more whitespace
// 2. optionally the keyword "static" followed by whitespace
@@ -91,23 +110,19 @@ final class Method extends BaseTag implements Factory\StaticMethod
)?
# Return type
(?:
(
(
(?:[\w\|_\\\\]*\$this[\w\|_\\\\]*)
|
(?:
(?:[\w\|_\\\\]+)
# array notation
# array notation
(?:\[\])*
)*
)*+
)
\s+
)?
# Legacy method name (not captured)
(?:
[\w_]+\(\)\s+
)?
# Method name
([\w\|_\\\\]+)
([\w_]+)
# Arguments
(?:
\(([^\)]*)\)
@@ -122,9 +137,9 @@ final class Method extends BaseTag implements Factory\StaticMethod
return null;
}
list(, $static, $returnType, $methodName, $arguments, $description) = $matches;
[, $static, $returnType, $methodName, $argumentLines, $description] = $matches;
$static = $static === 'static';
$static = $static === 'static';
if ($returnType === '') {
$returnType = 'void';
@@ -133,26 +148,26 @@ final class Method extends BaseTag implements Factory\StaticMethod
$returnType = $typeResolver->resolve($returnType, $context);
$description = $descriptionFactory->create($description, $context);
if (is_string($arguments) && strlen($arguments) > 0) {
$arguments = explode(',', $arguments);
foreach ($arguments as &$argument) {
/** @phpstan-var array<int, array{name: string, type: Type}> $arguments */
$arguments = [];
if ($argumentLines !== '') {
$argumentsExploded = explode(',', $argumentLines);
foreach ($argumentsExploded as $argument) {
$argument = explode(' ', self::stripRestArg(trim($argument)), 2);
if ($argument[0][0] === '$') {
if (strpos($argument[0], '$') === 0) {
$argumentName = substr($argument[0], 1);
$argumentType = new Void_();
$argumentType = new Mixed_();
} else {
$argumentType = $typeResolver->resolve($argument[0], $context);
$argumentName = '';
if (isset($argument[1])) {
$argument[1] = self::stripRestArg($argument[1]);
$argument[1] = self::stripRestArg($argument[1]);
$argumentName = substr($argument[1], 1);
}
}
$argument = [ 'name' => $argumentName, 'type' => $argumentType];
$arguments[] = ['name' => $argumentName, 'type' => $argumentType];
}
} else {
$arguments = [];
}
return new static($methodName, $arguments, $returnType, $static, $description);
@@ -160,18 +175,18 @@ final class Method extends BaseTag implements Factory\StaticMethod
/**
* Retrieves the method name.
*
* @return string
*/
public function getMethodName()
public function getMethodName() : string
{
return $this->methodName;
}
/**
* @return string[]
* @return array<int, array<string, Type|string>>
*
* @phpstan-return array<int, array{name: string, type: Type}>
*/
public function getArguments()
public function getArguments() : array
{
return $this->arguments;
}
@@ -181,20 +196,17 @@ final class Method extends BaseTag implements Factory\StaticMethod
*
* @return bool TRUE if the method declaration is for a static method, FALSE otherwise.
*/
public function isStatic()
public function isStatic() : bool
{
return $this->isStatic;
}
/**
* @return Type
*/
public function getReturnType()
public function getReturnType() : Type
{
return $this->returnType;
}
public function __toString()
public function __toString() : string
{
$arguments = [];
foreach ($this->arguments as $argument) {
@@ -202,36 +214,47 @@ final class Method extends BaseTag implements Factory\StaticMethod
}
return trim(($this->isStatic() ? 'static ' : '')
. (string)$this->returnType . ' '
. (string) $this->returnType . ' '
. $this->methodName
. '(' . implode(', ', $arguments) . ')'
. ($this->description ? ' ' . $this->description->render() : ''));
}
private function filterArguments($arguments)
/**
* @param mixed[][]|string[] $arguments
*
* @return mixed[][]
*
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
* @phpstan-return array<int, array{name: string, type: Type}>
*/
private function filterArguments(array $arguments = []) : array
{
foreach ($arguments as &$argument) {
$result = [];
foreach ($arguments as $argument) {
if (is_string($argument)) {
$argument = [ 'name' => $argument ];
$argument = ['name' => $argument];
}
if (! isset($argument['type'])) {
$argument['type'] = new Void_();
if (!isset($argument['type'])) {
$argument['type'] = new Mixed_();
}
$keys = array_keys($argument);
sort($keys);
if ($keys !== [ 'name', 'type' ]) {
throw new \InvalidArgumentException(
if ($keys !== ['name', 'type']) {
throw new InvalidArgumentException(
'Arguments can only have the "name" and "type" fields, found: ' . var_export($keys, true)
);
}
$result[] = $argument;
}
return $arguments;
return $result;
}
private static function stripRestArg($argument)
private static function stripRestArg(string $argument) : string
{
if (strpos($argument, '...') === 0) {
$argument = trim(substr($argument, 3));

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,75 +19,77 @@ use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function array_shift;
use function array_unshift;
use function implode;
use function preg_split;
use function strpos;
use function substr;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Reflection class for the {@}param tag in a Docblock.
*/
final class Param extends BaseTag implements Factory\StaticMethod
final class Param extends TagWithType implements Factory\StaticMethod
{
/** @var string */
protected $name = 'param';
/** @var Type */
private $type;
/** @var string */
private $variableName = '';
/** @var string|null */
private $variableName;
/** @var bool determines whether this is a variadic argument */
private $isVariadic = false;
/**
* @param string $variableName
* @param Type $type
* @param bool $isVariadic
* @param Description $description
*/
public function __construct($variableName, Type $type = null, $isVariadic = false, Description $description = null)
{
Assert::string($variableName);
Assert::boolean($isVariadic);
private $isVariadic;
public function __construct(
?string $variableName,
?Type $type = null,
bool $isVariadic = false,
?Description $description = null
) {
$this->name = 'param';
$this->variableName = $variableName;
$this->type = $type;
$this->isVariadic = $isVariadic;
$this->description = $description;
$this->type = $type;
$this->isVariadic = $isVariadic;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
$type = null;
[$firstPart, $body] = self::extractTypeFromBody($body);
$type = null;
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
Assert::isArray($parts);
$variableName = '';
$isVariadic = false;
$isVariadic = false;
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
if ($firstPart && $firstPart[0] !== '$') {
$type = $typeResolver->resolve($firstPart, $context);
} else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
}
// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$' || substr($parts[0], 0, 4) === '...$')) {
if (isset($parts[0]) && (strpos($parts[0], '$') === 0 || strpos($parts[0], '...$') === 0)) {
$variableName = array_shift($parts);
array_shift($parts);
if (substr($variableName, 0, 3) === '...') {
$isVariadic = true;
Assert::notNull($variableName);
if (strpos($variableName, '...') === 0) {
$isVariadic = true;
$variableName = substr($variableName, 3);
}
if (substr($variableName, 0, 1) === '$') {
if (strpos($variableName, '$') === 0) {
$variableName = substr($variableName, 1);
}
}
@@ -98,44 +101,28 @@ final class Param extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*
* @return string
*/
public function getVariableName()
public function getVariableName() : ?string
{
return $this->variableName;
}
/**
* Returns the variable's type or null if unknown.
*
* @return Type|null
*/
public function getType()
{
return $this->type;
}
/**
* Returns whether this tag is variadic.
*
* @return boolean
*/
public function isVariadic()
public function isVariadic() : bool
{
return $this->isVariadic;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. ($this->isVariadic() ? '...' : '')
. '$' . $this->variableName
. ($this->description ? ' ' . $this->description : '');
. ($this->isVariadic() ? '...' : '')
. ($this->variableName !== null ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,65 +19,64 @@ use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function array_shift;
use function array_unshift;
use function implode;
use function preg_split;
use function strpos;
use function substr;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Reflection class for a {@}property tag in a Docblock.
*/
class Property extends BaseTag implements Factory\StaticMethod
final class Property extends TagWithType implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property';
/** @var string|null */
protected $variableName;
/** @var Type */
private $type;
/** @var string */
protected $variableName = '';
/**
* @param string $variableName
* @param Type $type
* @param Description $description
*/
public function __construct($variableName, Type $type = null, Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
Assert::string($variableName);
$this->name = 'property';
$this->variableName = $variableName;
$this->type = $type;
$this->description = $description;
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
$type = null;
[$firstPart, $body] = self::extractTypeFromBody($body);
$type = null;
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
Assert::isArray($parts);
$variableName = '';
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
if ($firstPart && $firstPart[0] !== '$') {
$type = $typeResolver->resolve($firstPart, $context);
} else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
}
// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$')) {
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
$variableName = array_shift($parts);
array_shift($parts);
if (substr($variableName, 0, 1) === '$') {
$variableName = substr($variableName, 1);
}
Assert::notNull($variableName);
$variableName = substr($variableName, 1);
}
$description = $descriptionFactory->create(implode('', $parts), $context);
@@ -86,33 +86,19 @@ class Property extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*
* @return string
*/
public function getVariableName()
public function getVariableName() : ?string
{
return $this->variableName;
}
/**
* Returns the variable's type or null if unknown.
*
* @return Type|null
*/
public function getType()
{
return $this->type;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->description ? ' ' . $this->description : '');
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,65 +19,64 @@ use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function array_shift;
use function array_unshift;
use function implode;
use function preg_split;
use function strpos;
use function substr;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Reflection class for a {@}property-read tag in a Docblock.
*/
class PropertyRead extends BaseTag implements Factory\StaticMethod
final class PropertyRead extends TagWithType implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property-read';
/** @var string|null */
protected $variableName;
/** @var Type */
private $type;
/** @var string */
protected $variableName = '';
/**
* @param string $variableName
* @param Type $type
* @param Description $description
*/
public function __construct($variableName, Type $type = null, Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
Assert::string($variableName);
$this->name = 'property-read';
$this->variableName = $variableName;
$this->type = $type;
$this->description = $description;
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
$type = null;
[$firstPart, $body] = self::extractTypeFromBody($body);
$type = null;
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
Assert::isArray($parts);
$variableName = '';
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
if ($firstPart && $firstPart[0] !== '$') {
$type = $typeResolver->resolve($firstPart, $context);
} else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
}
// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$')) {
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
$variableName = array_shift($parts);
array_shift($parts);
if (substr($variableName, 0, 1) === '$') {
$variableName = substr($variableName, 1);
}
Assert::notNull($variableName);
$variableName = substr($variableName, 1);
}
$description = $descriptionFactory->create(implode('', $parts), $context);
@@ -86,33 +86,19 @@ class PropertyRead extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*
* @return string
*/
public function getVariableName()
public function getVariableName() : ?string
{
return $this->variableName;
}
/**
* Returns the variable's type or null if unknown.
*
* @return Type|null
*/
public function getType()
{
return $this->type;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->description ? ' ' . $this->description : '');
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,65 +19,64 @@ use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function array_shift;
use function array_unshift;
use function implode;
use function preg_split;
use function strpos;
use function substr;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Reflection class for a {@}property-write tag in a Docblock.
*/
class PropertyWrite extends BaseTag implements Factory\StaticMethod
final class PropertyWrite extends TagWithType implements Factory\StaticMethod
{
/** @var string */
protected $name = 'property-write';
protected $variableName;
/** @var Type */
private $type;
/** @var string */
protected $variableName = '';
/**
* @param string $variableName
* @param Type $type
* @param Description $description
*/
public function __construct($variableName, Type $type = null, Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
Assert::string($variableName);
$this->name = 'property-write';
$this->variableName = $variableName;
$this->type = $type;
$this->description = $description;
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
$type = null;
[$firstPart, $body] = self::extractTypeFromBody($body);
$type = null;
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
Assert::isArray($parts);
$variableName = '';
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
if ($firstPart && $firstPart[0] !== '$') {
$type = $typeResolver->resolve($firstPart, $context);
} else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
}
// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$')) {
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
$variableName = array_shift($parts);
array_shift($parts);
if (substr($variableName, 0, 1) === '$') {
$variableName = substr($variableName, 1);
}
Assert::notNull($variableName);
$variableName = substr($variableName, 1);
}
$description = $descriptionFactory->create(implode('', $parts), $context);
@@ -86,33 +86,19 @@ class PropertyWrite extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*
* @return string
*/
public function getVariableName()
public function getVariableName() : ?string
{
return $this->variableName;
}
/**
* Returns the variable's type or null if unknown.
*
* @return Type|null
*/
public function getType()
{
return $this->type;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. '$' . $this->variableName
. ($this->description ? ' ' . $this->description : '');
. ($this->variableName ? '$' . $this->variableName : '')
. ($this->description ? ' ' . $this->description : '');
}
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2017 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;
@@ -15,18 +16,13 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;
use phpDocumentor\Reflection\Fqsen as RealFqsen;
/**
* Fqsen reference used by {@see phpDocumentor\Reflection\DocBlock\Tags\See}
* Fqsen reference used by {@see \phpDocumentor\Reflection\DocBlock\Tags\See}
*/
final class Fqsen implements Reference
{
/**
* @var RealFqsen
*/
/** @var RealFqsen */
private $fqsen;
/**
* Fqsen constructor.
*/
public function __construct(RealFqsen $fqsen)
{
$this->fqsen = $fqsen;
@@ -35,8 +31,8 @@ final class Fqsen implements Reference
/**
* @return string string representation of the referenced fqsen
*/
public function __toString()
public function __toString() : string
{
return (string)$this->fqsen;
return (string) $this->fqsen;
}
}

View File

@@ -1,21 +1,22 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2017 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;
/**
* Interface for references in {@see phpDocumentor\Reflection\DocBlock\Tags\See}
* Interface for references in {@see \phpDocumentor\Reflection\DocBlock\Tags\See}
*/
interface Reference
{
public function __toString();
public function __toString() : string;
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2017 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;
@@ -15,25 +16,20 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Reference;
use Webmozart\Assert\Assert;
/**
* Url reference used by {@see phpDocumentor\Reflection\DocBlock\Tags\See}
* Url reference used by {@see \phpDocumentor\Reflection\DocBlock\Tags\See}
*/
final class Url implements Reference
{
/**
* @var string
*/
/** @var string */
private $uri;
/**
* Url constructor.
*/
public function __construct($uri)
public function __construct(string $uri)
{
Assert::stringNotEmpty($uri);
$this->uri = $uri;
}
public function __toString()
public function __toString() : string
{
return $this->uri;
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -22,51 +23,34 @@ use Webmozart\Assert\Assert;
/**
* Reflection class for a {@}return tag in a Docblock.
*/
final class Return_ extends BaseTag implements Factory\StaticMethod
final class Return_ extends TagWithType implements Factory\StaticMethod
{
protected $name = 'return';
/** @var Type */
private $type;
public function __construct(Type $type, Description $description = null)
public function __construct(Type $type, ?Description $description = null)
{
$this->type = $type;
$this->name = 'return';
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
Assert::string($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
[$type, $description] = self::extractTypeFromBody($body);
$type = $typeResolver->resolve(isset($parts[0]) ? $parts[0] : '', $context);
$description = $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context);
$type = $typeResolver->resolve($type, $context);
$description = $descriptionFactory->create($description, $context);
return new static($type, $description);
}
/**
* Returns the type section of the variable.
*
* @return Type
*/
public function getType()
public function __toString() : string
{
return $this->type;
}
public function __toString()
{
return $this->type . ' ' . $this->description;
return ($this->type ?: 'mixed') . ' ' . (string) $this->description;
}
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -20,42 +21,40 @@ use phpDocumentor\Reflection\DocBlock\Tags\Reference\Url;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
use function preg_split;
/**
* Reflection class for an {@}see tag in a Docblock.
*/
class See extends BaseTag implements Factory\StaticMethod
final class See extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'see';
/** @var Reference */
protected $refers = null;
protected $refers;
/**
* Initializes this tag.
*
* @param Reference $refers
* @param Description $description
*/
public function __construct(Reference $refers, Description $description = null)
public function __construct(Reference $refers, ?Description $description = null)
{
$this->refers = $refers;
$this->refers = $refers;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
FqsenResolver $resolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
Assert::string($body);
Assert::allNotNull([$resolver, $descriptionFactory]);
string $body,
?FqsenResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
$parts = preg_split('/\s+/Su', $body, 2);
Assert::isArray($parts);
$description = isset($parts[1]) ? $descriptionFactory->create($parts[1], $context) : null;
// https://tools.ietf.org/html/rfc2396#section-3
@@ -63,25 +62,21 @@ class See extends BaseTag implements Factory\StaticMethod
return new static(new Url($parts[0]), $description);
}
return new static(new FqsenRef($resolver->resolve($parts[0], $context)), $description);
return new static(new FqsenRef($typeResolver->resolve($parts[0], $context)), $description);
}
/**
* Returns the ref of this tag.
*
* @return Reference
*/
public function getReference()
public function getReference() : Reference
{
return $this->refers;
}
/**
* Returns a string representation of this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -16,19 +17,21 @@ use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
/**
* Reflection class for a {@}since tag in a Docblock.
*/
final class Since extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'since';
/**
* PCRE regular expression matching a version vector.
* Assumes the "x" modifier.
*/
const REGEX_VECTOR = '(?:
public const REGEX_VECTOR = '(?:
# Normal release vectors.
\d\S*
|
@@ -40,10 +43,10 @@ final class Since extends BaseTag implements Factory\StaticMethod
[^\s\:]+\:\s*\$[^\$]+\$
)';
/** @var string The version vector. */
private $version = '';
/** @var string|null The version vector. */
private $version;
public function __construct($version = null, Description $description = null)
public function __construct(?string $version = null, ?Description $description = null)
{
Assert::nullOrStringNotEmpty($version);
@@ -51,44 +54,41 @@ final class Since extends BaseTag implements Factory\StaticMethod
$this->description = $description;
}
/**
* @return static
*/
public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
{
Assert::nullOrString($body);
public static function create(
?string $body,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : ?self {
if (empty($body)) {
return new static();
}
$matches = [];
if (! preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
if (!preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
return null;
}
Assert::notNull($descriptionFactory);
return new static(
$matches[1],
$descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
$descriptionFactory->create($matches[2] ?? '', $context)
);
}
/**
* Gets the version section of the tag.
*
* @return string
*/
public function getVersion()
public function getVersion() : ?string
{
return $this->version;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
return (string) $this->version . ($this->description ? ' ' . (string) $this->description : '');
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -16,6 +17,7 @@ use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
/**
* Reflection class for a {@}source tag in a Docblock.
@@ -26,26 +28,30 @@ final class Source extends BaseTag implements Factory\StaticMethod
protected $name = 'source';
/** @var int The starting line, relative to the structural element's location. */
private $startingLine = 1;
private $startingLine;
/** @var int|null The number of lines, relative to the starting line. NULL means "to the end". */
private $lineCount = null;
private $lineCount;
public function __construct($startingLine, $lineCount = null, Description $description = null)
/**
* @param int|string $startingLine should be a to int convertible value
* @param int|string|null $lineCount should be a to int convertible value
*/
public function __construct($startingLine, $lineCount = null, ?Description $description = null)
{
Assert::integerish($startingLine);
Assert::nullOrIntegerish($lineCount);
$this->startingLine = (int)$startingLine;
$this->lineCount = $lineCount !== null ? (int)$lineCount : null;
$this->startingLine = (int) $startingLine;
$this->lineCount = $lineCount !== null ? (int) $lineCount : null;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
{
public static function create(
string $body,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::notNull($descriptionFactory);
@@ -55,15 +61,15 @@ final class Source extends BaseTag implements Factory\StaticMethod
// Starting line / Number of lines / Description
if (preg_match('/^([1-9]\d*)\s*(?:((?1))\s+)?(.*)$/sux', $body, $matches)) {
$startingLine = (int)$matches[1];
$startingLine = (int) $matches[1];
if (isset($matches[2]) && $matches[2] !== '') {
$lineCount = (int)$matches[2];
$lineCount = (int) $matches[2];
}
$description = $matches[3];
}
return new static($startingLine, $lineCount, $descriptionFactory->create($description, $context));
return new static($startingLine, $lineCount, $descriptionFactory->create($description??'', $context));
}
/**
@@ -72,7 +78,7 @@ final class Source extends BaseTag implements Factory\StaticMethod
* @return int The starting line, relative to the structural element's
* location.
*/
public function getStartingLine()
public function getStartingLine() : int
{
return $this->startingLine;
}
@@ -83,15 +89,15 @@ final class Source extends BaseTag implements Factory\StaticMethod
* @return int|null The number of lines, relative to the starting line. NULL
* means "to the end".
*/
public function getLineCount()
public function getLineCount() : ?int
{
return $this->lineCount;
}
public function __toString()
public function __toString() : string
{
return $this->startingLine
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
. ($this->description ? ' ' . $this->description->render() : '');
. ($this->lineCount !== null ? ' ' . $this->lineCount : '')
. ($this->description ? ' ' . (string) $this->description : '');
}
}

View File

@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\Type;
use function in_array;
use function strlen;
use function substr;
use function trim;
abstract class TagWithType extends BaseTag
{
/** @var ?Type */
protected $type;
/**
* Returns the type section of the variable.
*/
public function getType() : ?Type
{
return $this->type;
}
/**
* @return string[]
*/
protected static function extractTypeFromBody(string $body) : array
{
$type = '';
$nestingLevel = 0;
for ($i = 0, $iMax = strlen($body); $i < $iMax; $i++) {
$character = $body[$i];
if ($nestingLevel === 0 && trim($character) === '') {
break;
}
$type .= $character;
if (in_array($character, ['<', '(', '[', '{'])) {
$nestingLevel++;
continue;
}
if (in_array($character, ['>', ')', ']', '}'])) {
$nestingLevel--;
continue;
}
}
$description = trim(substr($body, strlen($type)));
return [$type, $description];
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -22,51 +23,34 @@ use Webmozart\Assert\Assert;
/**
* Reflection class for a {@}throws tag in a Docblock.
*/
final class Throws extends BaseTag implements Factory\StaticMethod
final class Throws extends TagWithType implements Factory\StaticMethod
{
protected $name = 'throws';
/** @var Type */
private $type;
public function __construct(Type $type, Description $description = null)
public function __construct(Type $type, ?Description $description = null)
{
$this->name = 'throws';
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
Assert::string($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
[$type, $description] = self::extractTypeFromBody($body);
$type = $typeResolver->resolve(isset($parts[0]) ? $parts[0] : '', $context);
$description = $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context);
$type = $typeResolver->resolve($type, $context);
$description = $descriptionFactory->create($description, $context);
return new static($type, $description);
}
/**
* Returns the type section of the variable.
*
* @return Type
*/
public function getType()
public function __toString() : string
{
return $this->type;
}
public function __toString()
{
return $this->type . ' ' . $this->description;
return (string) $this->type . ' ' . (string) $this->description;
}
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -18,66 +19,60 @@ use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_split;
/**
* Reflection class for a {@}uses tag in a Docblock.
*/
final class Uses extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'uses';
/** @var Fqsen */
protected $refers = null;
protected $refers;
/**
* Initializes this tag.
*
* @param Fqsen $refers
* @param Description $description
*/
public function __construct(Fqsen $refers, Description $description = null)
public function __construct(Fqsen $refers, ?Description $description = null)
{
$this->refers = $refers;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
FqsenResolver $resolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
Assert::string($body);
Assert::allNotNull([$resolver, $descriptionFactory]);
string $body,
?FqsenResolver $resolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::notNull($resolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/\s+/Su', $body, 2);
Assert::isArray($parts);
Assert::allString($parts);
return new static(
$resolver->resolve($parts[0], $context),
$descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
$descriptionFactory->create($parts[1] ?? '', $context)
);
}
/**
* Returns the structural element this tag refers to.
*
* @return Fqsen
*/
public function getReference()
public function getReference() : Fqsen
{
return $this->refers;
}
/**
* Returns a string representation of this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->refers . ' ' . $this->description->render();
return $this->refers . ' ' . (string) $this->description;
}
}

View File

@@ -1,12 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
@@ -18,65 +19,65 @@ use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function array_shift;
use function array_unshift;
use function implode;
use function preg_split;
use function strpos;
use function substr;
use const PREG_SPLIT_DELIM_CAPTURE;
/**
* Reflection class for a {@}var tag in a Docblock.
*/
class Var_ extends BaseTag implements Factory\StaticMethod
final class Var_ extends TagWithType implements Factory\StaticMethod
{
/** @var string */
protected $name = 'var';
/** @var Type */
private $type;
/** @var string */
/** @var string|null */
protected $variableName = '';
/**
* @param string $variableName
* @param Type $type
* @param Description $description
*/
public function __construct($variableName, Type $type = null, Description $description = null)
public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null)
{
Assert::string($variableName);
$this->name = 'var';
$this->variableName = $variableName;
$this->type = $type;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
TypeResolver $typeResolver = null,
DescriptionFactory $descriptionFactory = null,
TypeContext $context = null
) {
string $body,
?TypeResolver $typeResolver = null,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : self {
Assert::stringNotEmpty($body);
Assert::allNotNull([$typeResolver, $descriptionFactory]);
Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
[$firstPart, $body] = self::extractTypeFromBody($body);
$parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
Assert::isArray($parts);
$type = null;
$variableName = '';
// if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context);
array_shift($parts);
if ($firstPart && $firstPart[0] !== '$') {
$type = $typeResolver->resolve($firstPart, $context);
} else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
}
// if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && (strlen($parts[0]) > 0) && ($parts[0][0] === '$')) {
if (isset($parts[0]) && strpos($parts[0], '$') === 0) {
$variableName = array_shift($parts);
array_shift($parts);
if (substr($variableName, 0, 1) === '$') {
$variableName = substr($variableName, 1);
}
Assert::notNull($variableName);
$variableName = substr($variableName, 1);
}
$description = $descriptionFactory->create(implode('', $parts), $context);
@@ -86,33 +87,19 @@ class Var_ extends BaseTag implements Factory\StaticMethod
/**
* Returns the variable's name.
*
* @return string
*/
public function getVariableName()
public function getVariableName() : ?string
{
return $this->variableName;
}
/**
* Returns the variable's type or null if unknown.
*
* @return Type|null
*/
public function getType()
{
return $this->type;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return ($this->type ? $this->type . ' ' : '')
. (empty($this->variableName) ? null : ('$' . $this->variableName))
. (empty($this->variableName) ? '' : '$' . $this->variableName)
. ($this->description ? ' ' . $this->description : '');
}
}

View File

@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);
/**
* phpDocumentor
* This file is part of phpDocumentor.
*
* PHP Version 5.3
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Vasil Rangelov <boen.robot@gmail.com>
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
@@ -16,19 +17,21 @@ use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use Webmozart\Assert\Assert;
use function preg_match;
/**
* Reflection class for a {@}version tag in a Docblock.
*/
final class Version extends BaseTag implements Factory\StaticMethod
{
/** @var string */
protected $name = 'version';
/**
* PCRE regular expression matching a version vector.
* Assumes the "x" modifier.
*/
const REGEX_VECTOR = '(?:
public const REGEX_VECTOR = '(?:
# Normal release vectors.
\d\S*
|
@@ -40,23 +43,22 @@ final class Version extends BaseTag implements Factory\StaticMethod
[^\s\:]+\:\s*\$[^\$]+\$
)';
/** @var string The version vector. */
private $version = '';
/** @var string|null The version vector. */
private $version;
public function __construct($version = null, Description $description = null)
public function __construct(?string $version = null, ?Description $description = null)
{
Assert::nullOrStringNotEmpty($version);
$this->version = $version;
$this->version = $version;
$this->description = $description;
}
/**
* @return static
*/
public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
{
Assert::nullOrString($body);
public static function create(
?string $body,
?DescriptionFactory $descriptionFactory = null,
?TypeContext $context = null
) : ?self {
if (empty($body)) {
return new static();
}
@@ -66,29 +68,31 @@ final class Version extends BaseTag implements Factory\StaticMethod
return null;
}
$description = null;
if ($descriptionFactory !== null) {
$description = $descriptionFactory->create($matches[2] ?? '', $context);
}
return new static(
$matches[1],
$descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
$description
);
}
/**
* Gets the version section of the tag.
*
* @return string
*/
public function getVersion()
public function getVersion() : ?string
{
return $this->version;
}
/**
* Returns a string representation for this tag.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
return ((string) $this->version) .
($this->description instanceof Description ? ' ' . $this->description->render() : '');
}
}